From 835a1b4e73cf3556005091536b18c1d71c2abc3d Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 11 Mar 2022 17:36:53 +0100 Subject: [PATCH] Refactor load/store in storageManager --- src/storage_manager/index.js | 40 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/storage_manager/index.js b/src/storage_manager/index.js index 7065feaf1..3c371a1e1 100644 --- a/src/storage_manager/index.js +++ b/src/storage_manager/index.js @@ -222,23 +222,10 @@ export default () => { * await storageManager.store(data); * */ async store(data, options = {}) { - const ev = 'store'; const st = this.getCurrentStorage(); const opts = { ...this.getCurrentOptons(), ...options }; - this.onStart(ev, data); - - if (!st) return data; - - try { - await st.store(data, opts); - this.onAfter(ev, data); - this.onEnd(ev, data); - } catch (error) { - this.onError(ev, error); - throw error; - } - return data; + return await this.__exec(st, opts, data); }, /** @@ -250,17 +237,30 @@ export default () => { * editor.loadProjectData(data); * */ async load(options = {}) { - const ev = 'load'; const st = this.getCurrentStorage(); const opts = { ...this.getCurrentOptons(), ...options }; - let result = {}; - this.onStart(ev); - if (!st) return result; + return await this.__exec(st, opts); + }, + + async __exec(storage, opts, data) { + const ev = data ? 'store' : 'load'; + let result; + + this.onStart(ev, data); + + if (!storage) { + return data || {}; + } try { - const res = await st.load(opts); - result = this.__clearKeys(res); + if (data) { + await storage.store(data, opts); + result = data; + } else { + result = await storage.load(opts); + result = this.__clearKeys(result); + } this.onAfter(ev, result); this.onEnd(ev, result); } catch (error) {