From 0b2c9096cb088f5d5d9b90f870164546ae37315a Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 22 Jul 2020 14:06:25 +0300 Subject: [PATCH] feat: rename subscription service methods --- .../src/lib/services/subscription.service.ts | 38 +++++++++---------- .../lib/tests/subscription.service.spec.ts | 34 ++++++++--------- 2 files changed, 36 insertions(+), 36 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 eadec95b61..9296585d2a 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 @@ -11,27 +11,13 @@ export class SubscriptionService implements OnDestroy { return this.subscription.closed; } - ngOnDestroy(): void { - this.subscription.unsubscribe(); - } - - removeOne(subscription: Subscription | undefined | null) { - if (!subscription) return; - this.subscription.remove(subscription); - } - - reset() { - this.subscription.unsubscribe(); - this.subscription = new Subscription(); - } - - subscribe( + addOne( source$: Observable, next?: (value: T) => void, error?: (error: any) => void, ): Subscription; - subscribe(source$: Observable, observer?: PartialObserver): Subscription; - subscribe( + addOne(source$: Observable, observer?: PartialObserver): Subscription; + addOne( source$: Observable, nextOrObserver?: PartialObserver | Next, error?: (error: any) => void, @@ -41,14 +27,28 @@ export class SubscriptionService implements OnDestroy { return subscription; } - unsubscribeAll() { + closeAll() { this.subscription.unsubscribe(); } - unsubscribeOne(subscription: Subscription | undefined | null) { + closeOne(subscription: Subscription | undefined | null) { this.removeOne(subscription); subscription.unsubscribe(); } + + ngOnDestroy(): void { + this.subscription.unsubscribe(); + } + + removeOne(subscription: Subscription | undefined | null) { + if (!subscription) return; + this.subscription.remove(subscription); + } + + reset() { + this.subscription.unsubscribe(); + this.subscription = new Subscription(); + } } 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 c803455083..d0c8c4754f 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 @@ -12,11 +12,11 @@ describe('SubscriptionService', () => { service['subscription'].unsubscribe(); }); - describe('#subscribe', () => { + describe('#addOne', () => { it('should subscribe to given observable with next and error functions and return the Subscription instance', () => { const next = jest.fn(); const error = jest.fn(); - const subscription = service.subscribe(of(null), next, error); + const subscription = service.addOne(of(null), next, error); expect(subscription).toBeInstanceOf(Subscription); expect(next).toHaveBeenCalledWith(null); expect(next).toHaveBeenCalledTimes(1); @@ -25,7 +25,7 @@ describe('SubscriptionService', () => { it('should subscribe to given observable with observer and return the Subscription instance', () => { const observer = { next: jest.fn(), complete: jest.fn() }; - const subscription = service.subscribe(of(null), observer); + const subscription = service.addOne(of(null), observer); expect(subscription).toBeInstanceOf(Subscription); expect(observer.next).toHaveBeenCalledWith(null); expect(observer.next).toHaveBeenCalledTimes(1); @@ -35,7 +35,7 @@ describe('SubscriptionService', () => { describe('#isClosed', () => { it('should return true if subscriptions are alive and false if not', () => { - service.subscribe(timer(1000), () => {}); + service.addOne(timer(1000), () => {}); expect(service.isClosed).toBe(false); service['subscription'].unsubscribe(); @@ -43,16 +43,16 @@ describe('SubscriptionService', () => { }); }); - describe('#unsubscribeAll', () => { + describe('#closeAll', () => { it('should close all subscriptions and the parent subscription', () => { - const sub1 = service.subscribe(timer(1000), () => {}); - const sub2 = service.subscribe(timer(1000), () => {}); + const sub1 = service.addOne(timer(1000), () => {}); + const sub2 = service.addOne(timer(1000), () => {}); expect(sub1.closed).toBe(false); expect(sub2.closed).toBe(false); expect(service.isClosed).toBe(false); - service.unsubscribeAll(); + service.closeAll(); expect(sub1.closed).toBe(true); expect(sub2.closed).toBe(true); @@ -62,8 +62,8 @@ describe('SubscriptionService', () => { describe('#reset', () => { it('should close all subscriptions but not the parent subscription', () => { - const sub1 = service.subscribe(timer(1000), () => {}); - const sub2 = service.subscribe(timer(1000), () => {}); + const sub1 = service.addOne(timer(1000), () => {}); + const sub2 = service.addOne(timer(1000), () => {}); expect(sub1.closed).toBe(false); expect(sub2.closed).toBe(false); @@ -77,17 +77,17 @@ describe('SubscriptionService', () => { }); }); - describe('#unsubscribeOne', () => { + describe('#closeOne', () => { it('should unsubscribe from given subscription only', () => { - const sub1 = service.subscribe(timer(1000), () => {}); - const sub2 = service.subscribe(timer(1000), () => {}); + const sub1 = service.addOne(timer(1000), () => {}); + const sub2 = service.addOne(timer(1000), () => {}); expect(service.isClosed).toBe(false); - service.unsubscribeOne(sub1); + service.closeOne(sub1); expect(sub1.closed).toBe(true); expect(service.isClosed).toBe(false); - service.unsubscribeOne(sub2); + service.closeOne(sub2); expect(sub2.closed).toBe(true); expect(service.isClosed).toBe(false); }); @@ -95,8 +95,8 @@ describe('SubscriptionService', () => { describe('#removeOne', () => { it('should remove given subscription from list of subscriptions', () => { - const sub1 = service.subscribe(timer(1000), () => {}); - const sub2 = service.subscribe(timer(1000), () => {}); + const sub1 = service.addOne(timer(1000), () => {}); + const sub2 = service.addOne(timer(1000), () => {}); expect(service.isClosed).toBe(false); service.removeOne(sub1);