Browse Source

feat: add removeOne method to SubscriptionService

pull/4838/head
Arman Ozak 6 years ago
parent
commit
8d4180419f
  1. 16
      npm/ng-packs/packages/core/src/lib/services/subscription.service.ts
  2. 20
      npm/ng-packs/packages/core/src/lib/tests/subscription.service.spec.ts

16
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<T> = (value: T) => void;

20
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();
});
});
});

Loading…
Cancel
Save