How to unsubscribe from Observable if there is no object of type "Subscription"?

Michael

If I subscribe to an Observable, how can I unsubscribe from it, if there is no object of type "Subscription"?

If I have something like:

this.subscription = bla ... 

then I can unsubscribe from it as follows (in the ngOnDestroy()-method):

this.subscription.unsubscribe();

But what if I have something like this:

 ngOnInit() {

    this.isLoggedIn$ = this.authService.isLoggedIn();

    this.isLoggedIn$.subscribe(res => {
      if (res) {
        this.isLoggedIn = true;
      } 
      else {
        this.isLoggedIn = false;
      }
    });

  }

How can I unsubscribe from this? Do I even have to unsubscribe? If not, why not?

Danish Arora

There are 3 methods to unsubscribe an observable

  1. You can use above approach as this.subscription to assign subscribe for every subscribe and then unsubscribe each every explicitly. (It should be avoided)

  2. You can use takWhile pipe by using the example below:

    private isAlive = true;
    
    ngOnInit() {
    
      this.isLoggedIn$ = this.authService.isLoggedIn();
    
      this.subscription = this.isLoggedIn$
       .pipe(takeWhile(() => this.alive))
       .subscribe(res => {
        if (res) {
          this.isLoggedIn = true;
        } 
        else {
          this.isLoggedIn = false;
        }
      });
    
    }
    
    
    ngOnDestroy() {
       console.log('[takeWhile] ngOnDestory');
       this.alive = false;
    }
    
  3. Use takeUntil operator:

    private unsubscribe: Subject<void> = new Subject();
    
    ngOnInit() {
    
      this.isLoggedIn$ = this.authService.isLoggedIn();
    
      this.subscription = this.isLoggedIn$
       .pipe(takeUntil(this.unsubscribe))
       .subscribe(res => {
        if (res) {
          this.isLoggedIn = true;
        } 
        else {
          this.isLoggedIn = false;
        }
      });
    }
    
    ngOnDestroy() {
      this.unsubscribe.next();
      this.unsubscribe.complete();
    }
    

I Hope This helped!!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Angular/RxJs When should I unsubscribe from `Subscription`

From Dev

how to unsubscribe from WeakSubscribe in the MvvmCross

From Dev

How to unsubscribe from WCF service

From Dev

How to manage observable subscription for dependent observables?

From Dev

How to unsubscribe from the collection?

From Dev

How to unsubscribe from an Observable when using an Android library

From Dev

Atmosphere: How to unsubscribe from a broadcast

From Dev

Create an Observable you can unsubscribe from in RxCpp

From Dev

How to create an Observable<Type> from an Observable<List<Type>>

From Dev

How hook observable starting on delayed subscription in rxjava

From Dev

How to get Subscription type owned items from play service in Android?

From Dev

When to unsubscribe a subscription

From Dev

How to get Subscription ARN from Amazon SNS in order to unsubscribe from it

From Dev

F#: unsubscribe from an FSharp.Control.Reactive.Observable

From Dev

RxJava: How to extract object from observable?

From Dev

How to unsubscribe from an RxJS 5 observable?

From Dev

How to return variable from observable into another observable type

From Dev

Do I have to unsubscribe from completed observable?

From Dev

Do I have to unsubscribe from completed observable?

From Dev

RxJava: How to extract object from observable?

From Dev

Not receiving any results from subscription to Rx Observable

From Dev

how to unsubscribe from WeakSubscribe in the MvvmCross

From Dev

How to unsubscribe from WCF service

From Dev

Create an Observable you can unsubscribe from in RxCpp

From Dev

How do I unsubscribe from the subscription set by Iron Router's waitOn?

From Dev

RxJava: Unsubscribe async observable from within another async production

From Dev

How to get Subscription ARN from Amazon SNS in order to unsubscribe from it

From Dev

Subscription to Observable

From Dev

When to unsubscribe from `route.params` observable

Related Related

  1. 1

    Angular/RxJs When should I unsubscribe from `Subscription`

  2. 2

    how to unsubscribe from WeakSubscribe in the MvvmCross

  3. 3

    How to unsubscribe from WCF service

  4. 4

    How to manage observable subscription for dependent observables?

  5. 5

    How to unsubscribe from the collection?

  6. 6

    How to unsubscribe from an Observable when using an Android library

  7. 7

    Atmosphere: How to unsubscribe from a broadcast

  8. 8

    Create an Observable you can unsubscribe from in RxCpp

  9. 9

    How to create an Observable<Type> from an Observable<List<Type>>

  10. 10

    How hook observable starting on delayed subscription in rxjava

  11. 11

    How to get Subscription type owned items from play service in Android?

  12. 12

    When to unsubscribe a subscription

  13. 13

    How to get Subscription ARN from Amazon SNS in order to unsubscribe from it

  14. 14

    F#: unsubscribe from an FSharp.Control.Reactive.Observable

  15. 15

    RxJava: How to extract object from observable?

  16. 16

    How to unsubscribe from an RxJS 5 observable?

  17. 17

    How to return variable from observable into another observable type

  18. 18

    Do I have to unsubscribe from completed observable?

  19. 19

    Do I have to unsubscribe from completed observable?

  20. 20

    RxJava: How to extract object from observable?

  21. 21

    Not receiving any results from subscription to Rx Observable

  22. 22

    how to unsubscribe from WeakSubscribe in the MvvmCross

  23. 23

    How to unsubscribe from WCF service

  24. 24

    Create an Observable you can unsubscribe from in RxCpp

  25. 25

    How do I unsubscribe from the subscription set by Iron Router's waitOn?

  26. 26

    RxJava: Unsubscribe async observable from within another async production

  27. 27

    How to get Subscription ARN from Amazon SNS in order to unsubscribe from it

  28. 28

    Subscription to Observable

  29. 29

    When to unsubscribe from `route.params` observable

HotTag

Archive