Browse Source

Refactor destroy

pull/5642/head
Artur Arseniev 2 years ago
parent
commit
770fab53e9
  1. 21
      src/abstract/Module.ts
  2. 17
      src/trait_manager/index.ts

21
src/abstract/Module.ts

@ -1,5 +1,5 @@
import { isElement, isUndefined, isString } from 'underscore'; import { isElement, isUndefined, isString } from 'underscore';
import { Collection, View } from '../common'; import { Collection, Debounced, Model, View } from '../common';
import { EditorConfigKeys } from '../editor/config/config'; import { EditorConfigKeys } from '../editor/config/config';
import EditorModel from '../editor/model/Editor'; import EditorModel from '../editor/model/Editor';
import { ProjectData } from '../storage_manager/model/IStorage'; import { ProjectData } from '../storage_manager/model/IStorage';
@ -39,7 +39,10 @@ export default abstract class Module<T extends ModuleConfig = ModuleConfig> impl
private _em: EditorModel; private _em: EditorModel;
private _config: T & { pStylePrefix?: string }; private _config: T & { pStylePrefix?: string };
private _name: string; private _name: string;
debounced: Debounced[] = [];
collections: Collection[] = [];
cls: any[] = []; cls: any[] = [];
state?: Model;
events: any; events: any;
model?: any; model?: any;
view?: any; view?: any;
@ -67,7 +70,6 @@ export default abstract class Module<T extends ModuleConfig = ModuleConfig> impl
return this._config; return this._config;
} }
abstract destroy(): void;
render(opts?: any): HTMLElement | JQuery<HTMLElement> | void {} render(opts?: any): HTMLElement | JQuery<HTMLElement> | void {}
postLoad(key: any): void {} postLoad(key: any): void {}
@ -88,6 +90,21 @@ export default abstract class Module<T extends ModuleConfig = ModuleConfig> impl
postRender?(view: any): void; postRender?(view: any): void;
destroy() {
this.__destroy();
}
__destroy() {
this.view?.remove();
this.state?.stopListening();
this.state?.clear();
this.debounced.forEach(d => d.cancel());
this.collections.forEach(c => {
c.stopListening();
c.reset();
});
}
/** /**
* Move the main DOM element of the module. * Move the main DOM element of the module.
* To execute only post editor render (in postRender) * To execute only post editor render (in postRender)

17
src/trait_manager/index.ts

@ -59,12 +59,12 @@ export default class TraitManager extends Module<TraitManagerConfigModule> {
ppfx && (config.stylePrefix = `${ppfx}${config.stylePrefix}`); ppfx && (config.stylePrefix = `${ppfx}${config.stylePrefix}`);
const upAll = debounce(() => this.__upSel(), 0); const upAll = debounce(() => this.__upSel(), 0);
state.listenTo(em, 'component:toggled', upAll);
const update = debounce(() => this.__onUp(), 0); const update = debounce(() => this.__onUp(), 0);
state.listenTo(em, 'component:toggled', upAll);
state.listenTo(em, 'trait:update', update); state.listenTo(em, 'trait:update', update);
return this; this.debounced = [upAll, update];
this.collections = [this.categories as any];
} }
/** /**
@ -148,17 +148,6 @@ export default class TraitManager extends Module<TraitManagerConfigModule> {
return view.el; return view.el;
} }
destroy() {
const colls = [this.categories];
colls.forEach(c => {
c.stopListening();
c.reset();
});
this.state.stopListening();
this.state.clear();
this.view?.remove();
}
postRender() { postRender() {
this.__appendTo(); this.__appendTo();
} }

Loading…
Cancel
Save