Easy Unsubscription for Your Observables

SubscriptionService is a utility service to provide an easy unsubscription from RxJS observables in Angular components and directives. Please see why you should unsubscribe from observables on instance destruction.

Getting Started

You have to provide the SubscriptionService at component or directive level, because it is not provided in root and it works in sync with component/directive lifecycle. Only after then you can inject and start using it.

  1. import { SubscriptionService } from '@abp/ng.core';
  2. @Component({
  3. /* class metadata here */
  4. providers: [SubscriptionService],
  5. })
  6. class DemoComponent {
  7. count$ = interval(1000);
  8. constructor(private subscription: SubscriptionService) {
  9. this.subscription.addOne(this.count$, console.log);
  10. }
  11. }

The values emitted by the count$ will be logged until the component is destroyed. You will not have to unsubscribe manually.

Please do not try to use a singleton SubscriptionService. It simply will not work.

Usage

How to Subscribe to Observables

You can pass a next function and an error function.

  1. @Component({
  2. /* class metadata here */
  3. providers: [SubscriptionService],
  4. })
  5. class DemoComponent implements OnInit {
  6. constructor(private subscription: SubscriptionService) {}
  7. ngOnInit() {
  8. const source$ = interval(1000);
  9. const nextFn = value => console.log(value * 2);
  10. const errorFn = error => {
  11. console.error(error);
  12. return of(null);
  13. };
  14. this.subscription.addOne(source$, nextFn, errorFn);
  15. }
  16. }

Or, you can pass an observer.

  1. @Component({
  2. /* class metadata here */
  3. providers: [SubscriptionService],
  4. })
  5. class DemoComponent implements OnInit {
  6. constructor(private subscription: SubscriptionService) {}
  7. ngOnInit() {
  8. const source$ = interval(1000);
  9. const observer = {
  10. next: value => console.log(value * 2),
  11. complete: () => console.log('DONE'),
  12. };
  13. this.subscription.addOne(source$, observer);
  14. }
  15. }

The addOne method returns the individual subscription, so that you may use it later on. Please see topics below for details.

How to Unsubscribe Before Instance Destruction

There are two ways to do that. If you are not going to subscribe again, you may use the closeAll method.

  1. @Component({
  2. /* class metadata here */
  3. providers: [SubscriptionService],
  4. })
  5. class DemoComponent implements OnInit {
  6. constructor(private subscription: SubscriptionService) {}
  7. ngOnInit() {
  8. this.subscription.addOne(interval(1000), console.log);
  9. }
  10. onSomeEvent() {
  11. this.subscription.closeAll();
  12. }
  13. }

This will clear all subscriptions, but you will not be able to subscribe again. If you are planning to add another subscription, you may use the reset method instead.

  1. @Component({
  2. /* class metadata here */
  3. providers: [SubscriptionService],
  4. })
  5. class DemoComponent implements OnInit {
  6. constructor(private subscription: SubscriptionService) {}
  7. ngOnInit() {
  8. this.subscription.addOne(interval(1000), console.log);
  9. }
  10. onSomeEvent() {
  11. this.subscription.reset();
  12. this.subscription.addOne(interval(1000), console.warn);
  13. }
  14. }

How to Unsubscribe From a Single Subscription

Sometimes, you may need to unsubscribe from a particular subscription but leave others alive. In such a case, you may use the closeOne method.

  1. @Component({
  2. /* class metadata here */
  3. providers: [SubscriptionService],
  4. })
  5. class DemoComponent implements OnInit {
  6. countSubscription: Subscription;
  7. constructor(private subscription: SubscriptionService) {}
  8. ngOnInit() {
  9. this.countSubscription = this.subscription.addOne(
  10. interval(1000),
  11. console.log
  12. );
  13. }
  14. onSomeEvent() {
  15. this.subscription.closeOne(this.countSubscription);
  16. console.log(this.countSubscription.closed); // true
  17. }
  18. }

How to Remove a Single Subscription From Tracked Subscriptions

You may want to take control of a particular subscription. In such a case, you may use the removeOne method to remove it from tracked subscriptions.

  1. @Component({
  2. /* class metadata here */
  3. providers: [SubscriptionService],
  4. })
  5. class DemoComponent implements OnInit {
  6. countSubscription: Subscription;
  7. constructor(private subscription: SubscriptionService) {}
  8. ngOnInit() {
  9. this.countSubscription = this.subscription.addOne(
  10. interval(1000),
  11. console.log
  12. );
  13. }
  14. onSomeEvent() {
  15. this.subscription.removeOne(this.countSubscription);
  16. console.log(this.countSubscription.closed); // false
  17. }
  18. }

How to Check If Unsubscribed From All

Please use isClosed getter to check if closeAll was called before.

  1. @Component({
  2. /* class metadata here */
  3. providers: [SubscriptionService],
  4. })
  5. class DemoComponent implements OnInit {
  6. constructor(private subscription: SubscriptionService) {}
  7. ngOnInit() {
  8. this.subscription.addOne(interval(1000), console.log);
  9. }
  10. onSomeEvent() {
  11. console.log(this.subscription.isClosed); // false
  12. }
  13. }

What’s Next?