@flywine93/ngx-autounsubscrb
Version:
Angular 9+ automatically unsubscribe to the RXJS decorator, It is lightweight and practical!!
118 lines (111 loc) • 3.93 kB
JavaScript
import { __values } from 'tslib';
var IvyKey = {
dir: 'ɵdir',
cmp: 'ɵcmp',
prov: 'ɵprov' // Serivce
};
// Determines whether the target is a function
var isFun = function (target) { return typeof target === 'function'; };
var defaultOptions = {
blackList: [],
checkArrVar: false
};
/**
* unsubscribe variable
* @param member public and private member or temp variable
*/
function unsubscribe(variable, options) {
var e_1, _a;
if (variable) {
if (isFun(variable.unsubscribe)) {
variable.unsubscribe();
return;
}
// if variable is array type,will check and unsubscribe
if (options.checkArrVar && Array.isArray(variable)) {
try {
for (var variable_1 = __values(variable), variable_1_1 = variable_1.next(); !variable_1_1.done; variable_1_1 = variable_1.next()) {
var ele = variable_1_1.value;
if (ele && isFun(ele.unsubscribe)) {
ele.unsubscribe();
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (variable_1_1 && !variable_1_1.done && (_a = variable_1.return)) _a.call(variable_1);
}
finally { if (e_1) throw e_1.error; }
}
}
}
}
/**
* unsubscribe member variable and temp variable when destroy.
* @param options options
* 1.blackList---Variables in blacklist are not cancelled
*/
function AutoUnsubscrb(options) {
if (options === void 0) { options = {}; }
return function (target) {
var original = target.prototype['ngOnDestroy']; // cache original ngOnDestroy function
if (!isFun(original)) {
throw new Error(target.name + " is using @AutoUnsubscrb but does not implement OnDestroy");
}
options = Object.assign(defaultOptions, options);
target.prototype.autoAddList = []; // Used to cache objects added through MAutoAdd
target.prototype.ngOnDestroy = function ngOnDestroy() {
var e_2, _a;
if (isFun(original)) {
original.apply(this, arguments); // Preserve the original function
}
// unsubscribe public and private member variable
for (var propName in this) {
if (options.blackList.includes(propName)) {
continue;
}
unsubscribe(this[propName], options);
}
try {
// unsubscribe temp variable
for (var _b = __values(this.autoAddList), _c = _b.next(); !_c.done; _c = _b.next()) {
var sub = _c.value;
unsubscribe(sub, options);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
};
// Ivy
if (target.prototype.constructor[IvyKey.cmp]) {
target.prototype.constructor[IvyKey.cmp].onDestroy = target.prototype.ngOnDestroy;
}
else if (target.prototype.constructor[IvyKey.dir]) {
target.prototype.constructor[IvyKey.dir].onDestroy = target.prototype.ngOnDestroy;
}
};
}
/**
* Manually add temporary variables and unsubscribe when destroy.
* @param target Class this keyword
* @param subscrb Temp variable
*/
function MAutoAdd(target, subscrb) {
if (target && target.autoAddList) {
target.autoAddList.push(subscrb);
}
}
/*
* Public API Surface of ngx-autounsubscrb
*/
/**
* Generated bundle index. Do not edit.
*/
export { AutoUnsubscrb, MAutoAdd, unsubscribe };
//# sourceMappingURL=flywine93-ngx-autounsubscrb.js.map