Browse Source

Refactor StorageManager

pull/4223/head
Artur Arseniev 5 years ago
parent
commit
7fd7faf796
  1. 7
      src/common/module.js
  2. 172
      src/storage_manager/index.js

7
src/common/module.js

@ -1,5 +1,5 @@
import { isString, isElement } from 'underscore'; import { isString, isElement } from 'underscore';
import { createId, isDef } from 'utils/mixins'; import { createId, deepMerge, isDef } from 'utils/mixins';
export default { export default {
getConfig(name) { getConfig(name) {
@ -63,10 +63,7 @@ export default {
}, },
__initConfig(def = {}, conf = {}) { __initConfig(def = {}, conf = {}) {
this.config = { this.config = deepMerge(def, conf);
...def,
...conf,
};
this.em = this.config.em; this.em = this.config.em;
this.cls = []; this.cls = [];
}, },

172
src/storage_manager/index.js

@ -53,6 +53,7 @@
import defaults from './config/config'; import defaults from './config/config';
import LocalStorage from './model/LocalStorage'; import LocalStorage from './model/LocalStorage';
import RemoteStorage from './model/RemoteStorage'; import RemoteStorage from './model/RemoteStorage';
import Module from 'common/module';
import { deepMerge } from 'utils/mixins'; import { deepMerge } from 'utils/mixins';
import { isEmpty, isFunction } from 'underscore'; import { isEmpty, isFunction } from 'underscore';
@ -65,135 +66,127 @@ const STORAGE_LOCAL = 'local';
const STORAGE_REMOTE = 'remote'; const STORAGE_REMOTE = 'remote';
export default () => { export default () => {
var c = {};
let em;
var storages = {};
var defaultStorages = {};
return { return {
name: 'StorageManager', ...Module,
init(config = {}) { name: 'StorageManager',
c = deepMerge(defaults, config);
em = c.em;
if (c._disable) c.type = 0;
defaultStorages[STORAGE_REMOTE] = new RemoteStorage(c);
defaultStorages[STORAGE_LOCAL] = new LocalStorage(c);
c.currentStorage = c.type;
this.loadDefaultProviders().setCurrent(c.type);
return this;
},
/** /**
* Get configuration object * Get configuration object
* @name getConfig
* @function
* @return {Object} * @return {Object}
* */ */
getConfig() {
return c; /**
* Initialize module. Automatically called with a new instance of the editor
* @param {Object} config Configurations
* @private
*/
init(config = {}) {
this.__initConfig(defaults, config);
const c = this.getConfig();
if (c._disable) c.type = 0;
this.storages = {};
this.add(STORAGE_LOCAL, new LocalStorage(c));
this.add(STORAGE_REMOTE, new RemoteStorage(c));
this.setCurrent(c.type);
return this;
}, },
/** /**
* Checks if autosave is enabled * Check if autosave is enabled.
* @return {Boolean} * @returns {Boolean}
* */ * */
isAutosave() { isAutosave() {
return !!c.autosave; return !!this.getConfig().autosave;
}, },
/** /**
* Set autosave value * Set autosave value.
* @param {Boolean} v * @param {Boolean} value
* @return {this}
* */ * */
setAutosave(v) { setAutosave(value) {
c.autosave = !!v; this.getConfig().autosave = !!value;
return this; return this;
}, },
/** /**
* Returns number of steps required before trigger autosave * Returns number of steps required before trigger autosave.
* @return {number} * @returns {Number}
* */ * */
getStepsBeforeSave() { getStepsBeforeSave() {
return c.stepsBeforeSave; return this.getConfig().stepsBeforeSave;
}, },
/** /**
* Set steps required before trigger autosave * Set steps required before trigger autosave.
* @param {number} v * @param {Number} value
* @return {this}
* */ * */
setStepsBeforeSave(v) { setStepsBeforeSave(value) {
c.stepsBeforeSave = v; this.getConfig().stepsBeforeSave = value;
return this; return this;
}, },
/** /**
* Add new storage * Add new storage.
* @param {string} id Storage ID * @param {String} type Storage type
* @param {Object} storage Storage wrapper * @param {Object} storage Storage definition
* @param {Function} storage.load Load method * @param {Function} storage.load Load method
* @param {Function} storage.store Store method * @param {Function} storage.store Store method
* @return {this}
* @example * @example
* storageManager.add('local2', { * storageManager.add('local2', {
* load: function(keys, clb, clbErr) { * async load(storageOptions) {
* var res = {}; * // ...
* for (var i = 0, len = keys.length; i < len; i++){ * },
* var v = localStorage.getItem(keys[i]); * async store(data, storageOptions) {
* if(v) res[keys[i]] = v; * // ...
* }
* clb(res); // might be called inside some async method
* // In case of errors...
* // clbErr('Went something wrong');
* }, * },
* store: function(data, clb, clbErr) {
* for(var key in data)
* localStorage.setItem(key, data[key]);
* clb(); // might be called inside some async method
* }
* }); * });
* */ * */
add(id, storage) { add(type, storage) {
storages[id] = storage; this.storages[type] = storage;
return this; return this;
}, },
/** /**
* Returns storage by id * Return storage by type.
* @param {string} id Storage ID * @param {String} type Storage type
* @return {Object|null} * @returns {Object|null}
* */ * */
get(id) { get(type) {
return storages[id] || null; return this.storages[type] || null;
}, },
/** /**
* Returns all storages * Get all storages.
* @return {Array} * @returns {Object}
* */ * */
getStorages() { getStorages() {
return storages; return this.storages;
}, },
/** /**
* Returns current storage type * Get current storage type.
* @return {string} * @returns {String}
* */ * */
getCurrent() { getCurrent() {
return c.currentStorage; return this.getConfig().currentStorage;
}, },
/** /**
* Set current storage type * Set current storage type.
* @param {string} id Storage ID * @param {String} type Storage type
* @return {this}
* */ * */
setCurrent(id) { setCurrent(type) {
c.currentStorage = id; this.getConfig().currentStorage = type;
return this; return this;
}, },
getCurrentStorage() {
return this.get(this.getCurrent());
},
/** /**
* Store data in the current storage. * Store data in the current storage.
* @param {Object} data Project data. * @param {Object} data Project data.
@ -255,6 +248,7 @@ export default () => {
}, },
__askRecovery() { __askRecovery() {
const { em } = this;
const recovery = this.getRecovery(); const recovery = this.getRecovery();
return new Promise((res, rej) => { return new Promise((res, rej) => {
@ -305,15 +299,10 @@ export default () => {
return result; return result;
}, },
/**
* Restore key names
* @param {Object} data
* @returns {Object}
* @private
*/
__clearKeys(data = {}) { __clearKeys(data = {}) {
const config = this.getConfig();
const reg = new RegExp(`^${config.id}`);
const result = {}; const result = {};
const reg = new RegExp('^' + c.id + '');
for (let itemKey in data) { for (let itemKey in data) {
const itemKeyR = itemKey.replace(reg, ''); const itemKeyR = itemKey.replace(reg, '');
@ -323,24 +312,6 @@ export default () => {
return result; return result;
}, },
/**
* Load default storages
* @return {this}
* @private
* */
loadDefaultProviders() {
for (var id in defaultStorages) this.add(id, defaultStorages[id]);
return this;
},
/**
* Get current storage
* @return {Storage}
* */
getCurrentStorage() {
return this.get(this.getCurrent());
},
getCurrentOptons(type) { getCurrentOptons(type) {
const config = this.getConfig(); const config = this.getConfig();
const current = type || this.getCurrent(); const current = type || this.getCurrent();
@ -352,6 +323,7 @@ export default () => {
* @private * @private
*/ */
onStart(ctx, data) { onStart(ctx, data) {
const { em } = this;
if (em) { if (em) {
em.trigger(eventStart); em.trigger(eventStart);
ctx && em.trigger(`${eventStart}:${ctx}`, data); ctx && em.trigger(`${eventStart}:${ctx}`, data);
@ -363,6 +335,7 @@ export default () => {
* @private * @private
*/ */
onAfter(ctx, data) { onAfter(ctx, data) {
const { em } = this;
if (em) { if (em) {
em.trigger(eventAfter); em.trigger(eventAfter);
em.trigger(`${eventAfter}:${ctx}`, data); em.trigger(`${eventAfter}:${ctx}`, data);
@ -375,6 +348,7 @@ export default () => {
* @private * @private
*/ */
onEnd(ctx, data) { onEnd(ctx, data) {
const { em } = this;
if (em) { if (em) {
em.trigger(eventEnd); em.trigger(eventEnd);
ctx && em.trigger(`${eventEnd}:${ctx}`, data); ctx && em.trigger(`${eventEnd}:${ctx}`, data);
@ -386,6 +360,7 @@ export default () => {
* @private * @private
*/ */
onError(ctx, data) { onError(ctx, data) {
const { em } = this;
if (em) { if (em) {
em.trigger(eventError, data); em.trigger(eventError, data);
ctx && em.trigger(`${eventError}:${ctx}`, data); ctx && em.trigger(`${eventError}:${ctx}`, data);
@ -404,7 +379,8 @@ export default () => {
}, },
destroy() { destroy() {
[c, em, storages, defaultStorages].forEach(i => (i = {})); this.__destroy();
this.storages = {};
}, },
}; };
}; };

Loading…
Cancel
Save