From b80e1eca26ef661e20aa1fd5674e583c5c0e485e Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 10 Mar 2022 17:02:38 +0100 Subject: [PATCH] Refactor store and load methods --- src/editor/index.js | 46 +++++++--------- src/editor/model/Editor.js | 67 ++++++++--------------- src/storage_manager/index.js | 49 ++++++++--------- src/storage_manager/model/LocalStorage.js | 37 +++---------- 4 files changed, 72 insertions(+), 127 deletions(-) diff --git a/src/editor/index.js b/src/editor/index.js index fbf4184f9..4b9c10a79 100644 --- a/src/editor/index.js +++ b/src/editor/index.js @@ -431,17 +431,24 @@ export default (config = {}, opts = {}) => { /** * Store data to the current storage. - * @param {Function} [resolve] Resolve callback function. - * @param {Function} [reject] Reject callback function. The error is passed as an argument. * @param {Object} [options] Storage options. + * @returns {Object} Stored data. * @example - * editor.store( - * () => console.log('Project stored'), - * (err) => console.error('Store error', err), - * ); + * const storedData = await editor.store(); */ - store(resolve, reject, options) { - return em.store(resolve, reject, options); + async store(options) { + return await em.store(options); + }, + + /** + * Load data from the current storage. + * @param {Object} [options] Storage options. + * @returns {Object} Loaded data. + * @example + * const data = await editor.load(); + */ + async load(options) { + return await em.load(options); }, /** @@ -455,25 +462,6 @@ export default (config = {}, opts = {}) => { return em.storeData(); }, - storeData() { - return em.storeData(); - }, - - /** - * Load data from the current storage. - * @param {Function} [resolve] Resolve callback function. - * @param {Function} [reject] Reject callback function. The error is passed as an argument. - * @param {Object} [options] Storage options. - * @example - * editor.load( - * () => console.log('Project loaded'), - * (err) => console.error('Load error', err), - * ); - */ - load(resolve, reject, options) { - return em.load(resolve, reject, options); - }, - /** * Load data from the JSON project * @param {Object} data Project to load @@ -484,6 +472,10 @@ export default (config = {}, opts = {}) => { return em.loadData(data); }, + storeData() { + return em.storeData(); + }, + loadData(data) { return em.loadData(data); }, diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index b11e23736..b50a191bb 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -143,8 +143,6 @@ export default class EditorModel extends Model { * @private */ loadOnStart() { - const sm = this.get('StorageManager'); - // In `onLoad`, the module will try to load the data from its configurations. this.get('toLoad').forEach(mdl => mdl.onLoad()); @@ -156,12 +154,15 @@ export default class EditorModel extends Model { }; // Defer for storage load events. - setTimeout(() => { - if (sm && sm.canAutoload()) { - this.load(postLoad, error => this.logError(error)); - } else { - postLoad(); + setTimeout(async () => { + if (this.get('StorageManager').canAutoload()) { + try { + await this.load(); + } catch (error) { + this.logError(error); + } } + postLoad(); }); // Create shallow editor. @@ -612,25 +613,24 @@ export default class EditorModel extends Model { } /** - * Store data to the current storage - * @param {Function} [resolve] Resolve callback function. The result is passed as an argument. - * @param {Function} [reject] Reject callback function. The error is passed as an argument. - * @param {Object} [options] Storage options. + * Store data to the current storage. * @private */ - store(resolve, reject, options) { - const sm = this.get('StorageManager'); - if (!sm) return; + async store(options) { + const data = this.storeData(); + await this.get('StorageManager').store(data, options); + this.set('changesCount', 0); + return data; + } - return sm.store( - this.storeData(), - res => { - resolve?.(res); - this.set('changesCount', 0); - }, - reject, - options - ); + /** + * Load data from the current storage. + * @private + */ + async load(options) { + const result = await this.get('StorageManager').load(options); + this.loadData(result); + return result; } storeData() { @@ -645,27 +645,6 @@ export default class EditorModel extends Model { return JSON.parse(JSON.stringify(result)); } - /** - * Load data from the current storage - * @param {Function} [resolve] Resolve callback function. The result is passed as an argument. - * @param {Function} [reject] Reject callback function. The error is passed as an argument. - * @param {Object} [options] Storage options. - * @private - */ - load(resolve, reject, options) { - const sm = this.get('StorageManager'); - if (!sm) return; - - return sm.load( - res => { - this.loadData(res); - resolve?.(res); - }, - reject, - options - ); - } - loadData(data = {}) { this.get('storables').forEach(module => module.load(data)); return data; diff --git a/src/storage_manager/index.js b/src/storage_manager/index.js index 401fef12e..e4ee2898a 100644 --- a/src/storage_manager/index.js +++ b/src/storage_manager/index.js @@ -215,32 +215,29 @@ export default () => { /** * Store data in the current storage. * @param {Object} data Data in key-value format, eg. `{ item1: value1, item2: value2 }` - * @param {Function} [resolve] Resolve callback function. - * @param {Function} [reject] Reject callback function. The error is passed as an argument. * @param {Object} [options] Storage options. + * @returns {Object} Stored data. * @example - * storageManager.store({item1: value1, item2: value2}); + * await storageManager.store({item1: value1, item2: value2}); * */ - async store(data, resolve, reject, options = {}) { + async store(data, options = {}) { const ev = 'store'; const st = this.getCurrentStorage(); const opts = { ...this.getCurrentOptons(), ...options }; this.onStart(ev, data); - if (!st) return null; - - const onResult = res => { - this.onAfter(ev, res); - resolve?.(res); - this.onEnd(ev, res); - }; + if (!st) return data; - const onError = err => { - reject?.(err); - this.onError(ev, err); - }; + try { + await st.store(data, opts); + this.onAfter(ev, data); + this.onEnd(ev, data); + } catch (error) { + this.onError(ev, error); + throw error; + } - return await st.store(data, onResult, onError, opts); + return data; }, /** @@ -253,28 +250,26 @@ export default () => { * // res -> {item1: value1, item2: value2} * }); * */ - async load(resolve, reject, options = {}) { + async load(options = {}) { const ev = 'load'; const st = this.getCurrentStorage(); const opts = { ...this.getCurrentOptons(), ...options }; let result = {}; this.onStart(ev); - if (!st) return resolve(result); + if (!st) return result; - const onResult = res => { + try { + const res = await st.load(opts); result = this.__clearKeys(res); this.onAfter(ev, result); - resolve?.(result); this.onEnd(ev, result); - }; - - const onError = err => { - reject?.(err); - this.onError(ev, err); - }; + } catch (error) { + this.onError(ev, error); + throw error; + } - return await st.load(onResult, onError, opts); + return result; }, /** diff --git a/src/storage_manager/model/LocalStorage.js b/src/storage_manager/model/LocalStorage.js index 2e6bcf0cc..a8f57e20f 100644 --- a/src/storage_manager/model/LocalStorage.js +++ b/src/storage_manager/model/LocalStorage.js @@ -1,47 +1,26 @@ import { Model } from 'common'; import { hasWin } from 'utils/mixins'; -const noLocalStorage = 'localStorage not available'; - export default class LocalStorage extends Model { - async store(data, resolve, reject, opts) { - try { - if (this.hasLocal(opts)) { - localStorage.setItem(opts.key, JSON.stringify(data)); - resolve(data); - } else { - reject(noLocalStorage); - } - } catch (error) { - reject(error); + async store(data, opts = {}) { + if (this.hasLocal(opts, true)) { + localStorage.setItem(opts.key, JSON.stringify(data)); } - - return data; } - async load(resolve, reject, opts) { + async load(opts = {}) { let result = {}; - try { - if (this.hasLocal(opts)) { - result = JSON.parse(localStorage.getItem(opts.key) || '{}'); - resolve(result); - } else { - reject(noLocalStorage); - } - } catch (error) { - reject(error); + if (this.hasLocal(opts, true)) { + result = JSON.parse(localStorage.getItem(opts.key) || '{}'); } return result; } - /** - * Check storage environment - * @private - * */ - hasLocal(opts = {}) { + hasLocal(opts = {}, thr) { if (opts.checkLocal && (!hasWin() || !localStorage)) { + if (thr) throw new Error('localStorage not available'); return false; }