From 8d4180419fccfddd5c13c1fe9adffde200928a2e Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 22 Jul 2020 12:54:14 +0300 Subject: [PATCH] feat: add removeOne method to SubscriptionService --- .../src/lib/services/subscription.service.ts | 16 +++++++++------ .../lib/tests/subscription.service.spec.ts | 20 ++++++++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/subscription.service.ts b/npm/ng-packs/packages/core/src/lib/services/subscription.service.ts index 81739d10ae..eadec95b61 100644 --- a/npm/ng-packs/packages/core/src/lib/services/subscription.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/subscription.service.ts @@ -15,6 +15,11 @@ export class SubscriptionService implements OnDestroy { this.subscription.unsubscribe(); } + removeOne(subscription: Subscription | undefined | null) { + if (!subscription) return; + this.subscription.remove(subscription); + } + reset() { this.subscription.unsubscribe(); this.subscription = new Subscription(); @@ -36,15 +41,14 @@ export class SubscriptionService implements OnDestroy { return subscription; } - unsubscribe(subscription: Subscription | undefined | null) { - if (!subscription) return; - this.subscription.remove(subscription); - subscription.unsubscribe(); - } - unsubscribeAll() { this.subscription.unsubscribe(); } + + unsubscribeOne(subscription: Subscription | undefined | null) { + this.removeOne(subscription); + subscription.unsubscribe(); + } } type Next = (value: T) => void; diff --git a/npm/ng-packs/packages/core/src/lib/tests/subscription.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/subscription.service.spec.ts index 95df285fb9..c803455083 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/subscription.service.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/subscription.service.spec.ts @@ -77,19 +77,33 @@ describe('SubscriptionService', () => { }); }); - describe('#unsubscribe', () => { + describe('#unsubscribeOne', () => { it('should unsubscribe from given subscription only', () => { const sub1 = service.subscribe(timer(1000), () => {}); const sub2 = service.subscribe(timer(1000), () => {}); expect(service.isClosed).toBe(false); - service.unsubscribe(sub1); + service.unsubscribeOne(sub1); expect(sub1.closed).toBe(true); expect(service.isClosed).toBe(false); - service.unsubscribe(sub2); + service.unsubscribeOne(sub2); expect(sub2.closed).toBe(true); expect(service.isClosed).toBe(false); }); }); + + describe('#removeOne', () => { + it('should remove given subscription from list of subscriptions', () => { + const sub1 = service.subscribe(timer(1000), () => {}); + const sub2 = service.subscribe(timer(1000), () => {}); + expect(service.isClosed).toBe(false); + + service.removeOne(sub1); + expect(sub1.closed).toBe(false); + expect(service.isClosed).toBe(false); + + sub1.unsubscribe(); + }); + }); });