Browse Source

Refactor store and load methods

pull/4223/head
Artur Arseniev 5 years ago
parent
commit
b80e1eca26
  1. 46
      src/editor/index.js
  2. 67
      src/editor/model/Editor.js
  3. 49
      src/storage_manager/index.js
  4. 37
      src/storage_manager/model/LocalStorage.js

46
src/editor/index.js

@ -431,17 +431,24 @@ export default (config = {}, opts = {}) => {
/** /**
* Store data to the current storage. * 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. * @param {Object} [options] Storage options.
* @returns {Object} Stored data.
* @example * @example
* editor.store( * const storedData = await editor.store();
* () => console.log('Project stored'),
* (err) => console.error('Store error', err),
* );
*/ */
store(resolve, reject, options) { async store(options) {
return em.store(resolve, reject, 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(); 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 * Load data from the JSON project
* @param {Object} data Project to load * @param {Object} data Project to load
@ -484,6 +472,10 @@ export default (config = {}, opts = {}) => {
return em.loadData(data); return em.loadData(data);
}, },
storeData() {
return em.storeData();
},
loadData(data) { loadData(data) {
return em.loadData(data); return em.loadData(data);
}, },

67
src/editor/model/Editor.js

@ -143,8 +143,6 @@ export default class EditorModel extends Model {
* @private * @private
*/ */
loadOnStart() { loadOnStart() {
const sm = this.get('StorageManager');
// In `onLoad`, the module will try to load the data from its configurations. // In `onLoad`, the module will try to load the data from its configurations.
this.get('toLoad').forEach(mdl => mdl.onLoad()); this.get('toLoad').forEach(mdl => mdl.onLoad());
@ -156,12 +154,15 @@ export default class EditorModel extends Model {
}; };
// Defer for storage load events. // Defer for storage load events.
setTimeout(() => { setTimeout(async () => {
if (sm && sm.canAutoload()) { if (this.get('StorageManager').canAutoload()) {
this.load(postLoad, error => this.logError(error)); try {
} else { await this.load();
postLoad(); } catch (error) {
this.logError(error);
}
} }
postLoad();
}); });
// Create shallow editor. // Create shallow editor.
@ -612,25 +613,24 @@ export default class EditorModel extends Model {
} }
/** /**
* Store data to the current storage * 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.
* @private * @private
*/ */
store(resolve, reject, options) { async store(options) {
const sm = this.get('StorageManager'); const data = this.storeData();
if (!sm) return; await this.get('StorageManager').store(data, options);
this.set('changesCount', 0);
return data;
}
return sm.store( /**
this.storeData(), * Load data from the current storage.
res => { * @private
resolve?.(res); */
this.set('changesCount', 0); async load(options) {
}, const result = await this.get('StorageManager').load(options);
reject, this.loadData(result);
options return result;
);
} }
storeData() { storeData() {
@ -645,27 +645,6 @@ export default class EditorModel extends Model {
return JSON.parse(JSON.stringify(result)); 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 = {}) { loadData(data = {}) {
this.get('storables').forEach(module => module.load(data)); this.get('storables').forEach(module => module.load(data));
return data; return data;

49
src/storage_manager/index.js

@ -215,32 +215,29 @@ export default () => {
/** /**
* Store data in the current storage. * Store data in the current storage.
* @param {Object} data Data in key-value format, eg. `{ item1: value1, item2: value2 }` * @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. * @param {Object} [options] Storage options.
* @returns {Object} Stored data.
* @example * @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 ev = 'store';
const st = this.getCurrentStorage(); const st = this.getCurrentStorage();
const opts = { ...this.getCurrentOptons(), ...options }; const opts = { ...this.getCurrentOptons(), ...options };
this.onStart(ev, data); this.onStart(ev, data);
if (!st) return null; if (!st) return data;
const onResult = res => {
this.onAfter(ev, res);
resolve?.(res);
this.onEnd(ev, res);
};
const onError = err => { try {
reject?.(err); await st.store(data, opts);
this.onError(ev, err); 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} * // res -> {item1: value1, item2: value2}
* }); * });
* */ * */
async load(resolve, reject, options = {}) { async load(options = {}) {
const ev = 'load'; const ev = 'load';
const st = this.getCurrentStorage(); const st = this.getCurrentStorage();
const opts = { ...this.getCurrentOptons(), ...options }; const opts = { ...this.getCurrentOptons(), ...options };
let result = {}; let result = {};
this.onStart(ev); 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); result = this.__clearKeys(res);
this.onAfter(ev, result); this.onAfter(ev, result);
resolve?.(result);
this.onEnd(ev, result); this.onEnd(ev, result);
}; } catch (error) {
this.onError(ev, error);
const onError = err => { throw error;
reject?.(err); }
this.onError(ev, err);
};
return await st.load(onResult, onError, opts); return result;
}, },
/** /**

37
src/storage_manager/model/LocalStorage.js

@ -1,47 +1,26 @@
import { Model } from 'common'; import { Model } from 'common';
import { hasWin } from 'utils/mixins'; import { hasWin } from 'utils/mixins';
const noLocalStorage = 'localStorage not available';
export default class LocalStorage extends Model { export default class LocalStorage extends Model {
async store(data, resolve, reject, opts) { async store(data, opts = {}) {
try { if (this.hasLocal(opts, true)) {
if (this.hasLocal(opts)) { localStorage.setItem(opts.key, JSON.stringify(data));
localStorage.setItem(opts.key, JSON.stringify(data));
resolve(data);
} else {
reject(noLocalStorage);
}
} catch (error) {
reject(error);
} }
return data;
} }
async load(resolve, reject, opts) { async load(opts = {}) {
let result = {}; let result = {};
try { if (this.hasLocal(opts, true)) {
if (this.hasLocal(opts)) { result = JSON.parse(localStorage.getItem(opts.key) || '{}');
result = JSON.parse(localStorage.getItem(opts.key) || '{}');
resolve(result);
} else {
reject(noLocalStorage);
}
} catch (error) {
reject(error);
} }
return result; return result;
} }
/** hasLocal(opts = {}, thr) {
* Check storage environment
* @private
* */
hasLocal(opts = {}) {
if (opts.checkLocal && (!hasWin() || !localStorage)) { if (opts.checkLocal && (!hasWin() || !localStorage)) {
if (thr) throw new Error('localStorage not available');
return false; return false;
} }

Loading…
Cancel
Save