ngx-decorate
Version:
Useful decorators for Angular 2 and above
110 lines • 3.97 kB
JavaScript
import * as tslib_1 from "tslib";
import { defineImmutable } from './defineProp';
import { toAbridged } from './Lifecycle';
import { _hookMgr } from './symbols';
/** @internal */
function ngOnDestroy() {
//mock
}
/** @internal */
function ngOnInit() {
// mock
}
/** @internal */
var HookManager = /** @class */ (function () {
function HookManager(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;
}
Object.defineProperty(HookManager.prototype, "newDestroy", {
get: function () {
var value = this.newFn(this.preDestroys, this.postDestroys, this.origDestroy);
defineImmutable(this, 'newDestroy', value);
return value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(HookManager.prototype, "newInit", {
get: function () {
var value = this.newFn(this.preInits, this.postInits, this.origInit);
defineImmutable(this, 'newInit', value);
return value;
},
enumerable: true,
configurable: true
});
HookManager.for = function (proto) {
if (!proto[_hookMgr]) {
defineImmutable(proto, _hookMgr, new HookManager(proto));
}
return proto[_hookMgr];
};
HookManager.prototype.add = function (lifecycle, hookFn) {
this.hooks(lifecycle).add(hookFn);
var 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;
}
};
HookManager.prototype.hooks = function (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;
}
};
HookManager.prototype.newFn = function (pres, posts, orig) {
return function () {
var e_1, _a, e_2, _b;
try {
try {
for (var pres_1 = tslib_1.__values(pres), pres_1_1 = pres_1.next(); !pres_1_1.done; pres_1_1 = pres_1.next()) {
var fn = pres_1_1.value;
fn(this);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (pres_1_1 && !pres_1_1.done && (_a = pres_1.return)) _a.call(pres_1);
}
finally { if (e_1) throw e_1.error; }
}
orig.apply(this, arguments);
}
finally {
try {
for (var posts_1 = tslib_1.__values(posts), posts_1_1 = posts_1.next(); !posts_1_1.done; posts_1_1 = posts_1.next()) {
var fn = posts_1_1.value;
fn(this);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (posts_1_1 && !posts_1_1.done && (_b = posts_1.return)) _b.call(posts_1);
}
finally { if (e_2) throw e_2.error; }
}
}
};
};
return HookManager;
}());
export { HookManager };
defineImmutable(HookManager.prototype, Symbol.toStringTag, 'HookManager');
//# sourceMappingURL=HookManager.js.map