@flywine93/ngx-autounsubscrb
Version:
Angular 9+ automatically unsubscribe to the RXJS decorator, It is lightweight and practical!!
93 lines (87 loc) • 2.92 kB
JavaScript
const IvyKey = {
dir: 'ɵdir',
cmp: 'ɵcmp',
prov: 'ɵprov' // Serivce
};
// Determines whether the target is a function
const isFun = (target) => typeof target === 'function';
const defaultOptions = {
blackList: [],
checkArrVar: false
};
/**
* unsubscribe variable
* @param member public and private member or temp variable
*/
function unsubscribe(variable, options) {
if (variable) {
if (isFun(variable.unsubscribe)) {
variable.unsubscribe();
return;
}
// if variable is array type,will check and unsubscribe
if (options.checkArrVar && Array.isArray(variable)) {
for (const ele of variable) {
if (ele && isFun(ele.unsubscribe)) {
ele.unsubscribe();
}
}
}
}
}
/**
* unsubscribe member variable and temp variable when destroy.
* @param options options
* 1.blackList---Variables in blacklist are not cancelled
*/
function AutoUnsubscrb(options = {}) {
return function (target) {
const 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() {
if (isFun(original)) {
original.apply(this, arguments); // Preserve the original function
}
// unsubscribe public and private member variable
for (const propName in this) {
if (options.blackList.includes(propName)) {
continue;
}
unsubscribe(this[propName], options);
}
// unsubscribe temp variable
for (const sub of this.autoAddList) {
unsubscribe(sub, options);
}
};
// 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