Browse Source

Move Storage manager to TS

pull/4830/head
Artur Arseniev 4 years ago
parent
commit
42969a162b
  1. 96
      src/storage_manager/index.ts
  2. 4
      src/storage_manager/model/IStorage.ts

96
src/storage_manager/index.js → src/storage_manager/index.ts

@ -51,11 +51,13 @@
* @module Storage * @module Storage
*/ */
import Module from '../abstract/moduleLegacy'; import { isEmpty, isFunction } from 'underscore';
import defaults from './config/config'; import { Module } from '../abstract';
import defaults, { StorageManagerConfig } from './config/config';
import LocalStorage from './model/LocalStorage'; import LocalStorage from './model/LocalStorage';
import RemoteStorage from './model/RemoteStorage'; import RemoteStorage from './model/RemoteStorage';
import { isEmpty, isFunction } from 'underscore'; import EditorModel from '../editor/model/Editor';
import IStorage, { StorageOptions, ProjectData } from './model/IStorage';
const eventStart = 'storage:start'; const eventStart = 'storage:start';
const eventAfter = 'storage:after'; const eventAfter = 'storage:after';
@ -65,8 +67,20 @@ const eventError = 'storage:error';
const STORAGE_LOCAL = 'local'; const STORAGE_LOCAL = 'local';
const STORAGE_REMOTE = 'remote'; const STORAGE_REMOTE = 'remote';
export default class StorageManager extends Module { export default class StorageManager extends Module<
name = 'StorageManager'; StorageManagerConfig & { name?: string; _disable?: boolean; currentStorage?: string }
> {
storages: { [key: string]: IStorage } = {};
constructor(em: EditorModel) {
super(em, 'StorageManager', defaults);
const { config } = this;
if (config._disable) config.type = '';
this.storages = {};
this.add(STORAGE_LOCAL, new LocalStorage());
this.add(STORAGE_REMOTE, new RemoteStorage());
this.setCurrent(config.type!);
}
/** /**
* Get configuration object * Get configuration object
@ -75,36 +89,20 @@ export default class StorageManager extends Module {
* @return {Object} * @return {Object}
*/ */
/**
* 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;
}
/** /**
* Check if autosave is enabled. * Check if autosave is enabled.
* @returns {Boolean} * @returns {Boolean}
* */ * */
isAutosave() { isAutosave() {
return !!this.getConfig().autosave; return !!this.config.autosave;
} }
/** /**
* Set autosave value. * Set autosave value.
* @param {Boolean} value * @param {Boolean} value
* */ * */
setAutosave(value) { setAutosave(value: boolean) {
this.getConfig().autosave = !!value; this.config.autosave = !!value;
return this; return this;
} }
@ -113,15 +111,15 @@ export default class StorageManager extends Module {
* @returns {Number} * @returns {Number}
* */ * */
getStepsBeforeSave() { getStepsBeforeSave() {
return this.getConfig().stepsBeforeSave; return this.config.stepsBeforeSave!;
} }
/** /**
* Set steps required before trigger autosave. * Set steps required before trigger autosave.
* @param {Number} value * @param {Number} value
* */ * */
setStepsBeforeSave(value) { setStepsBeforeSave(value: number) {
this.getConfig().stepsBeforeSave = value; this.config.stepsBeforeSave = value;
return this; return this;
} }
@ -141,7 +139,8 @@ export default class StorageManager extends Module {
* }, * },
* }); * });
* */ * */
add(type, storage) { add<T extends StorageOptions>(type: string, storage: IStorage<T>) {
// @ts-ignore
this.storages[type] = storage; this.storages[type] = storage;
return this; return this;
} }
@ -151,8 +150,8 @@ export default class StorageManager extends Module {
* @param {String} type Storage type * @param {String} type Storage type
* @returns {Object|null} * @returns {Object|null}
* */ * */
get(type) { get<T extends StorageOptions>(type: string): IStorage<T> | undefined {
return this.storages[type] || null; return this.storages[type];
} }
/** /**
@ -168,14 +167,14 @@ export default class StorageManager extends Module {
* @returns {String} * @returns {String}
* */ * */
getCurrent() { getCurrent() {
return this.getConfig().currentStorage; return this.config.currentStorage!;
} }
/** /**
* Set current storage type. * Set current storage type.
* @param {String} type Storage type * @param {String} type Storage type
* */ * */
setCurrent(type) { setCurrent(type: string) {
this.getConfig().currentStorage = type; this.getConfig().currentStorage = type;
return this; return this;
} }
@ -189,7 +188,7 @@ export default class StorageManager extends Module {
* @param {String} type Storage type * @param {String} type Storage type
* @returns {Object} * @returns {Object}
* */ * */
getStorageOptions(type) { getStorageOptions(type: string) {
return this.getCurrentOptions(type); return this.getCurrentOptions(type);
} }
@ -202,14 +201,14 @@ export default class StorageManager extends Module {
* const data = editor.getProjectData(); * const data = editor.getProjectData();
* await storageManager.store(data); * await storageManager.store(data);
* */ * */
async store(data, options = {}) { async store(data: ProjectData, options: StorageOptions = {}) {
const st = this.getCurrentStorage(); const st = this.getCurrentStorage();
const opts = { ...this.getCurrentOptions(), ...options }; const opts = { ...this.getCurrentOptions(), ...options };
const recovery = this.getRecoveryStorage(); const recovery = this.getRecoveryStorage();
const recoveryOpts = this.getCurrentOptions(STORAGE_LOCAL); const recoveryOpts = this.getCurrentOptions(STORAGE_LOCAL);
try { try {
await this.__exec(st, opts, data); await this.__exec(st!, opts, data);
recovery && (await this.__exec(recovery, recoveryOpts, {})); recovery && (await this.__exec(recovery, recoveryOpts, {}));
} catch (error) { } catch (error) {
if (recovery) { if (recovery) {
@ -234,7 +233,7 @@ export default class StorageManager extends Module {
const st = this.getCurrentStorage(); const st = this.getCurrentStorage();
const opts = { ...this.getCurrentOptions(), ...options }; const opts = { ...this.getCurrentOptions(), ...options };
const recoveryStorage = this.getRecoveryStorage(); const recoveryStorage = this.getRecoveryStorage();
let result; let result: ProjectData = {};
if (recoveryStorage) { if (recoveryStorage) {
const recoveryData = await this.__exec(recoveryStorage, this.getCurrentOptions(STORAGE_LOCAL)); const recoveryData = await this.__exec(recoveryStorage, this.getCurrentOptions(STORAGE_LOCAL));
@ -247,7 +246,7 @@ export default class StorageManager extends Module {
} }
if (!result) { if (!result) {
result = await this.__exec(st, opts); result = await this.__exec(st!, opts);
} }
return result || {}; return result || {};
@ -261,13 +260,13 @@ export default class StorageManager extends Module {
if (isFunction(recovery)) { if (isFunction(recovery)) {
recovery(res, rej, em?.getEditor()); recovery(res, rej, em?.getEditor());
} else { } else {
confirm(em?.t('storageManager.recover')) ? res() : rej(); confirm(em?.t('storageManager.recover')) ? res(null) : rej();
} }
}); });
} }
getRecovery() { getRecovery() {
return this.getConfig().recovery; return this.config.recovery;
} }
getRecoveryStorage() { getRecoveryStorage() {
@ -275,7 +274,7 @@ export default class StorageManager extends Module {
return recovery && this.getCurrent() === STORAGE_REMOTE && this.get(STORAGE_LOCAL); return recovery && this.getCurrent() === STORAGE_REMOTE && this.get(STORAGE_LOCAL);
} }
async __exec(storage, opts, data) { async __exec(storage: IStorage, opts: StorageOptions, data?: ProjectData) {
const ev = data ? 'store' : 'load'; const ev = data ? 'store' : 'load';
const { onStore, onLoad } = this.getConfig(); const { onStore, onLoad } = this.getConfig();
let result; let result;
@ -310,10 +309,10 @@ export default class StorageManager extends Module {
return result; return result;
} }
__clearKeys(data = {}) { __clearKeys(data: ProjectData = {}) {
const config = this.getConfig(); const config = this.getConfig();
const reg = new RegExp(`^${config.id}`); const reg = new RegExp(`^${config.id}`);
const result = {}; const result: ProjectData = {};
for (let itemKey in data) { for (let itemKey in data) {
const itemKeyR = itemKey.replace(reg, ''); const itemKeyR = itemKey.replace(reg, '');
@ -323,7 +322,7 @@ export default class StorageManager extends Module {
return result; return result;
} }
getCurrentOptions(type) { getCurrentOptions(type?: string): StorageOptions {
const config = this.getConfig(); const config = this.getConfig();
const current = type || this.getCurrent(); const current = type || this.getCurrent();
return config.options[current] || {}; return config.options[current] || {};
@ -333,7 +332,7 @@ export default class StorageManager extends Module {
* On start callback * On start callback
* @private * @private
*/ */
onStart(ctx, data) { onStart(ctx: string, data?: ProjectData) {
const { em } = this; const { em } = this;
if (em) { if (em) {
em.trigger(eventStart); em.trigger(eventStart);
@ -345,7 +344,7 @@ export default class StorageManager extends Module {
* On after callback (before passing data to the callback) * On after callback (before passing data to the callback)
* @private * @private
*/ */
onAfter(ctx, data) { onAfter(ctx: string, data: ProjectData) {
const { em } = this; const { em } = this;
if (em) { if (em) {
em.trigger(eventAfter); em.trigger(eventAfter);
@ -358,7 +357,7 @@ export default class StorageManager extends Module {
* On end callback * On end callback
* @private * @private
*/ */
onEnd(ctx, data) { onEnd(ctx: string, data: ProjectData) {
const { em } = this; const { em } = this;
if (em) { if (em) {
em.trigger(eventEnd); em.trigger(eventEnd);
@ -370,7 +369,7 @@ export default class StorageManager extends Module {
* On error callback * On error callback
* @private * @private
*/ */
onError(ctx, data) { onError(ctx: string, data: any) {
const { em } = this; const { em } = this;
if (em) { if (em) {
em.trigger(eventError, data); em.trigger(eventError, data);
@ -386,11 +385,10 @@ export default class StorageManager extends Module {
* */ * */
canAutoload() { canAutoload() {
const storage = this.getCurrentStorage(); const storage = this.getCurrentStorage();
return storage && this.getConfig().autoload; return !!storage && !!this.config.autoload;
} }
destroy() { destroy() {
this.__destroy();
this.storages = {}; this.storages = {};
} }
} }

4
src/storage_manager/model/IStorage.ts

@ -7,7 +7,7 @@ export interface ProjectData {
} }
export default interface IStorage<T extends StorageOptions = {}> { export default interface IStorage<T extends StorageOptions = {}> {
load: (options?: T) => Promise<ProjectData>; load: (options: T) => Promise<ProjectData>;
store: (data: ProjectData, options?: T) => Promise<any>; store: (data: ProjectData, options: T) => Promise<any>;
[key: string]: any; [key: string]: any;
} }

Loading…
Cancel
Save