UNPKG

ngx-decorate

Version:

Useful decorators for Angular 2 and above

78 lines 2.41 kB
import { defineImmutable } from './defineProp'; import { toAbridged } from './Lifecycle'; import { _hookMgr } from './symbols'; /** @internal */ function ngOnDestroy() { //mock } /** @internal */ function ngOnInit() { // mock } /** @internal */ export class HookManager { constructor(proto) { this.proto = proto; this.postDestroys = new Set(); this.postInits = new Set(); this.preDestroys = new Set(); this.preInits = new Set(); this.origInit = proto.ngOnInit || ngOnInit; this.origDestroy = proto.ngOnDestroy || ngOnDestroy; } get newDestroy() { const value = this.newFn(this.preDestroys, this.postDestroys, this.origDestroy); defineImmutable(this, 'newDestroy', value); return value; } get newInit() { const value = this.newFn(this.preInits, this.postInits, this.origInit); defineImmutable(this, 'newInit', value); return value; } static for(proto) { if (!proto[_hookMgr]) { defineImmutable(proto, _hookMgr, new HookManager(proto)); } return proto[_hookMgr]; } add(lifecycle, hookFn) { this.hooks(lifecycle).add(hookFn); const type = toAbridged(lifecycle); if (type === 1 /* DESTROY */) { this.proto.ngOnDestroy = this.preDestroys.size || this.postDestroys.size ? this.newDestroy : this.origDestroy; } else { this.proto.ngOnInit = this.preInits.size || this.postInits.size ? this.newInit : this.origInit; } } hooks(lifecycle) { switch (lifecycle) { case 0 /* PRE_INIT */: return this.preInits; case 1 /* PRE_DESTROY */: return this.preDestroys; case 2 /* POST_INIT */: return this.postInits; default: return this.postDestroys; } } newFn(pres, posts, orig) { return function () { try { for (const fn of pres) { fn(this); } orig.apply(this, arguments); } finally { for (const fn of posts) { fn(this); } } }; } } defineImmutable(HookManager.prototype, Symbol.toStringTag, 'HookManager'); //# sourceMappingURL=HookManager.js.map