From 14074a83cfba84a4421db0b3cfcd1402ec8e3fa7 Mon Sep 17 00:00:00 2001 From: rameez rami Date: Sun, 18 Jul 2021 02:51:27 +0530 Subject: [PATCH] Typescript Interface added for params + Only unsubscribe if not already stopped 1. Interface added for the AutoUnsubscribe function params 2. Only unsubscribe if the subscription is not already stopped or closed. --- src/auto-unsubscribe.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/auto-unsubscribe.ts b/src/auto-unsubscribe.ts index 49a35c0..c893515 100644 --- a/src/auto-unsubscribe.ts +++ b/src/auto-unsubscribe.ts @@ -1,8 +1,9 @@ -const isFunction = fn => typeof fn === "function"; +const isFunction = fn => typeof fn === 'function'; const doUnsubscribe = subscription => { subscription && isFunction(subscription.unsubscribe) && + (subscription.isStopped === false || subscription.closed === false) && subscription.unsubscribe(); }; @@ -11,11 +12,17 @@ const doUnsubscribeIfArray = subscriptionsArray => { subscriptionsArray.forEach(doUnsubscribe); }; +interface AutoUnsubParamType { + blackList?: string[]; + arrayName?: string; + event?: string; +} + export function AutoUnsubscribe({ blackList = [], - arrayName = "", - event = "ngOnDestroy" -} = {}) { + arrayName = '', + event = 'ngOnDestroy' +}: AutoUnsubParamType = {}) { return function(constructor: Function) { const original = constructor.prototype[event];