UNPKG

ngx-decorate

Version:

Useful decorators for Angular 2 and above

81 lines 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const defineProp_1 = require("./defineProp"); const Lifecycle_1 = require("./Lifecycle"); const symbols_1 = require("./symbols"); /** @internal */ function ngOnDestroy() { //mock } /** @internal */ function ngOnInit() { // mock } /** @internal */ 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); defineProp_1.defineImmutable(this, 'newDestroy', value); return value; } get newInit() { const value = this.newFn(this.preInits, this.postInits, this.origInit); defineProp_1.defineImmutable(this, 'newInit', value); return value; } static for(proto) { if (!proto[symbols_1._hookMgr]) { defineProp_1.defineImmutable(proto, symbols_1._hookMgr, new HookManager(proto)); } return proto[symbols_1._hookMgr]; } add(lifecycle, hookFn) { this.hooks(lifecycle).add(hookFn); const type = Lifecycle_1.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); } } }; } } exports.HookManager = HookManager; defineProp_1.defineImmutable(HookManager.prototype, Symbol.toStringTag, 'HookManager'); //# sourceMappingURL=HookManager.js.map