Browse Source

feat: rename subscription service methods

pull/4838/head
Arman Ozak 6 years ago
parent
commit
0b2c9096cb
  1. 38
      npm/ng-packs/packages/core/src/lib/services/subscription.service.ts
  2. 34
      npm/ng-packs/packages/core/src/lib/tests/subscription.service.spec.ts

38
npm/ng-packs/packages/core/src/lib/services/subscription.service.ts

@ -11,27 +11,13 @@ export class SubscriptionService implements OnDestroy {
return this.subscription.closed; return this.subscription.closed;
} }
ngOnDestroy(): void { addOne<T extends unknown>(
this.subscription.unsubscribe();
}
removeOne(subscription: Subscription | undefined | null) {
if (!subscription) return;
this.subscription.remove(subscription);
}
reset() {
this.subscription.unsubscribe();
this.subscription = new Subscription();
}
subscribe<T extends unknown>(
source$: Observable<T>, source$: Observable<T>,
next?: (value: T) => void, next?: (value: T) => void,
error?: (error: any) => void, error?: (error: any) => void,
): Subscription; ): Subscription;
subscribe<T extends unknown>(source$: Observable<T>, observer?: PartialObserver<T>): Subscription; addOne<T extends unknown>(source$: Observable<T>, observer?: PartialObserver<T>): Subscription;
subscribe<T extends unknown>( addOne<T extends unknown>(
source$: Observable<T>, source$: Observable<T>,
nextOrObserver?: PartialObserver<T> | Next<T>, nextOrObserver?: PartialObserver<T> | Next<T>,
error?: (error: any) => void, error?: (error: any) => void,
@ -41,14 +27,28 @@ export class SubscriptionService implements OnDestroy {
return subscription; return subscription;
} }
unsubscribeAll() { closeAll() {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
} }
unsubscribeOne(subscription: Subscription | undefined | null) { closeOne(subscription: Subscription | undefined | null) {
this.removeOne(subscription); this.removeOne(subscription);
subscription.unsubscribe(); 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<T> = (value: T) => void; type Next<T> = (value: T) => void;

34
npm/ng-packs/packages/core/src/lib/tests/subscription.service.spec.ts

@ -12,11 +12,11 @@ describe('SubscriptionService', () => {
service['subscription'].unsubscribe(); service['subscription'].unsubscribe();
}); });
describe('#subscribe', () => { describe('#addOne', () => {
it('should subscribe to given observable with next and error functions and return the Subscription instance', () => { it('should subscribe to given observable with next and error functions and return the Subscription instance', () => {
const next = jest.fn(); const next = jest.fn();
const error = 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(subscription).toBeInstanceOf(Subscription);
expect(next).toHaveBeenCalledWith(null); expect(next).toHaveBeenCalledWith(null);
expect(next).toHaveBeenCalledTimes(1); expect(next).toHaveBeenCalledTimes(1);
@ -25,7 +25,7 @@ describe('SubscriptionService', () => {
it('should subscribe to given observable with observer and return the Subscription instance', () => { it('should subscribe to given observable with observer and return the Subscription instance', () => {
const observer = { next: jest.fn(), complete: jest.fn() }; 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(subscription).toBeInstanceOf(Subscription);
expect(observer.next).toHaveBeenCalledWith(null); expect(observer.next).toHaveBeenCalledWith(null);
expect(observer.next).toHaveBeenCalledTimes(1); expect(observer.next).toHaveBeenCalledTimes(1);
@ -35,7 +35,7 @@ describe('SubscriptionService', () => {
describe('#isClosed', () => { describe('#isClosed', () => {
it('should return true if subscriptions are alive and false if not', () => { 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); expect(service.isClosed).toBe(false);
service['subscription'].unsubscribe(); service['subscription'].unsubscribe();
@ -43,16 +43,16 @@ describe('SubscriptionService', () => {
}); });
}); });
describe('#unsubscribeAll', () => { describe('#closeAll', () => {
it('should close all subscriptions and the parent subscription', () => { it('should close all subscriptions and the parent subscription', () => {
const sub1 = service.subscribe(timer(1000), () => {}); const sub1 = service.addOne(timer(1000), () => {});
const sub2 = service.subscribe(timer(1000), () => {}); const sub2 = service.addOne(timer(1000), () => {});
expect(sub1.closed).toBe(false); expect(sub1.closed).toBe(false);
expect(sub2.closed).toBe(false); expect(sub2.closed).toBe(false);
expect(service.isClosed).toBe(false); expect(service.isClosed).toBe(false);
service.unsubscribeAll(); service.closeAll();
expect(sub1.closed).toBe(true); expect(sub1.closed).toBe(true);
expect(sub2.closed).toBe(true); expect(sub2.closed).toBe(true);
@ -62,8 +62,8 @@ describe('SubscriptionService', () => {
describe('#reset', () => { describe('#reset', () => {
it('should close all subscriptions but not the parent subscription', () => { it('should close all subscriptions but not the parent subscription', () => {
const sub1 = service.subscribe(timer(1000), () => {}); const sub1 = service.addOne(timer(1000), () => {});
const sub2 = service.subscribe(timer(1000), () => {}); const sub2 = service.addOne(timer(1000), () => {});
expect(sub1.closed).toBe(false); expect(sub1.closed).toBe(false);
expect(sub2.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', () => { it('should unsubscribe from given subscription only', () => {
const sub1 = service.subscribe(timer(1000), () => {}); const sub1 = service.addOne(timer(1000), () => {});
const sub2 = service.subscribe(timer(1000), () => {}); const sub2 = service.addOne(timer(1000), () => {});
expect(service.isClosed).toBe(false); expect(service.isClosed).toBe(false);
service.unsubscribeOne(sub1); service.closeOne(sub1);
expect(sub1.closed).toBe(true); expect(sub1.closed).toBe(true);
expect(service.isClosed).toBe(false); expect(service.isClosed).toBe(false);
service.unsubscribeOne(sub2); service.closeOne(sub2);
expect(sub2.closed).toBe(true); expect(sub2.closed).toBe(true);
expect(service.isClosed).toBe(false); expect(service.isClosed).toBe(false);
}); });
@ -95,8 +95,8 @@ describe('SubscriptionService', () => {
describe('#removeOne', () => { describe('#removeOne', () => {
it('should remove given subscription from list of subscriptions', () => { it('should remove given subscription from list of subscriptions', () => {
const sub1 = service.subscribe(timer(1000), () => {}); const sub1 = service.addOne(timer(1000), () => {});
const sub2 = service.subscribe(timer(1000), () => {}); const sub2 = service.addOne(timer(1000), () => {});
expect(service.isClosed).toBe(false); expect(service.isClosed).toBe(false);
service.removeOne(sub1); service.removeOne(sub1);

Loading…
Cancel
Save