87

How does one cancel a subscription in Angular2? RxJS seems to have a dispose method, but I can't figure out how to access it. So I have code that has access to an EventEmitter and subscribes to it, like this:

var mySubscription = someEventEmitter.subscribe(
    (val) => {
        console.log('Received:', val);
    },
    (err) => {
        console.log('Received error:', err);
    },
    () => {
        console.log('Completed');
    }
);

How can I use mySubscription to cancel the subscription?

Michael Oryl
  • 20,856
  • 14
  • 77
  • 117
  • 2
    FYI - if you're going to do Reactive things, use a Subject, rather than Angular's EventEmitter - there's no guarantee it will remain a superclass of Subject. Use EventEmitter for @Output events only. – robwormald Dec 24 '15 at 11:04

9 Answers9

134

Are you looking to unsubscribe?

mySubscription.unsubscribe();
kendaleiv
  • 5,793
  • 3
  • 28
  • 38
  • 4
    Good lord. I swore I tried that already. I looked at the RxJS source and that seemed to be what it was. I must have had a different error causing me issues. Thanks. – Michael Oryl Dec 23 '15 at 20:17
  • 1
    this works well but I am curious of what's the type of `mySubscription` in TypeScript. I want to avoid writing `mySubscription: any` in my class. – paradite Oct 30 '16 at 15:47
  • 12
    @paradite `import { Subscription } from "rxjs";` and for unsubscription `if (!mySubscription.closed) { mySubscription.unsubscribe(); }`. – Llyle Dec 03 '16 at 00:34
  • 9
    `import { Subscription } from 'rxjs/Subscription';` will help keep your package size down – paul Jun 07 '17 at 13:47
51

I thought I put in my two cents too. I use this pattern:

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

@Component({
    selector: 'my-component',
    templateUrl: 'my.component.html'
})
export class MyComponent implements OnInit, OnDestroy {

    private subscriptions: Array<Subscription> = [];

    public ngOnInit(): void {
        this.subscriptions.push(this.someService.change.subscribe(() => {
            [...]
        }));

        this.subscriptions.push(this.someOtherService.select.subscribe(() => {
            [...]
        }));
    }

    public ngOnDestroy(): void {
        this.subscriptions.forEach((subscription: Subscription) => {
            subscription.unsubscribe();
        });
    }
}

EDIT

I read the documentation the other day and found a more recommended pattern:

ReactiveX/RxJS/Subscription

Pros:

It manages the new subscriptions internally and adds some neat checks. Would prefer this method in the feature :).

Cons:

It isn't 100% clear what the code flow is and how subscriptions are affected. Nor is it clear (just from looking at the code) how it deals with closed subscriptions and if all subscriptions are getting closed if unsubscribe is called.

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

@Component({
    selector: 'my-component',
    templateUrl: 'my.component.html'
})
export class MyComponent implements OnInit, OnDestroy {

    private subscription: Subscription = new Subscription();

    public ngOnInit(): void {
        this.subscription.add(this.someService.change.subscribe(() => {
            [...]
        }));

        this.subscription.add(this.someOtherService.select.subscribe(() => {
            [...]
        }));
    }

    public ngOnDestroy(): void {
        /*
         * magic kicks in here: All subscriptions which were added
         * with "subscription.add" are canceled too!
         */
        this.subscription.unsubscribe();
    }
}
Nightking
  • 1,910
  • 1
  • 18
  • 22
  • 2
    I use this too, it scales nicely the more subscriptions you have in a component. – Dennis Hackethal Feb 22 '17 at 22:47
  • 1
    No "magic" - it's by design: "Subscriptions can also be put together, so that a call to an unsubscribe() of one Subscription may unsubscribe multiple Subscriptions. You can do this by "adding" one subscription into another:" https://github.com/ReactiveX/rxjs/blob/master/doc/subscription.md – Meetai.com May 18 '17 at 06:50
  • There is another clear approach you could be interested in. Using `takeWhile` operator. Approach described here: http://brianflove.com/2016/12/11/anguar-2-unsubscribe-observables/ – vdshb Jun 15 '17 at 22:16
  • Check out an [alternative solution](https://stackoverflow.com/a/41177163/968003), which is using takeUntil. There is a rich and interesting discussion, considering different options. – Alex Klaus Sep 06 '17 at 06:18
8

EDIT: This does not apply to RxJS 5, which is what angular2 is using.

I would have thought you are looking for the dispose method on Disposable.

the subscribe method returns a Disposable (link)

I can't seem to find it more explicitly in the docs, but this works (jsbin):

var observable = Rx.Observable.interval(100);

var subscription = observable.subscribe(function(value) {
   console.log(value);
});

setTimeout(function() {
  subscription.dispose();           
}, 1000)

Weirdly, unsubscribe seems to be working for you while it's not working for me...

Niklas Fasching
  • 1,326
  • 11
  • 15
4

Far too many different explanations of unsubscribe on Observables for ng2, took me ages to find the right answer. Below is a working example (I was trying to throttle mousemove).

import {Injectable, OnDestroy} from "@angular/core";
import {Subscription} from "rxjs";

@Injectable()
export class MyClass implements OnDestroy {
  
  mouseSubscription: Subscription; //Set a variable for your subscription
  
  myFunct() {
    // I'm trying to throttle mousemove
    const eachSecond$ = Observable.timer(0, 1000);
    const mouseMove$ = Observable.fromEvent<MouseEvent>(document, 'mousemove');
    const mouseMoveEachSecond$ = mouseMove$.sample(eachSecond$);
    
    this.mouseSubscription = mouseMoveEachSecond$.subscribe(() => this.doSomethingElse());
  }

  doSomethingElse() {
    console.log("mouse moved");
  }
  
  stopNow() {
    this.mouseSubscription.unsubscribe();
  }
  
  ngOnDestroy() {
    this.mouseSubscription.unsubscribe();
  }
  
}
Joe Keene
  • 2,175
  • 21
  • 27
2
ngOnDestroy(){
   mySubscription.unsubscribe();
}

Prefer unsubscribing rxjs unsubscribe's while destroying the component i.e., removing from DOM for avoiding unecessary memory leaks

2

I prefer personally to use a Subject to close all subscriptions a component might have at the destroy life cycle step which can be achieved this way :

import { Component} from '@angular/core';
import { Subject } from "rxjs/Rx";

@Component({
  selector:    'some-class-app',
  templateUrl: './someClass.component.html',
  providers:   []
})

export class SomeClass {  

  private ngUnsubscribe: Subject<void> = new Subject<void>(); //This subject will tell every subscriptions to stop when the component is destroyed.

  //**********
  constructor() {}

  ngOnInit() {

    this.http.post( "SomeUrl.com", {}, null ).map( response => {

      console.log( "Yay." );

    }).takeUntil( this.ngUnsubscribe ).subscribe(); //This is where you tell the subscription to stop whenever the component will be destroyed.
  }

  ngOnDestroy() {

    //This is where we close any active subscription.
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
Jahrenski
  • 171
  • 4
  • 20
2

The recommended approach is to use RxJS operators such as the takeUntil operator. Below is the code snippet showing how to use it :-

import { Component, OnInit, OnDestroy } from '@angular/core';
import { interval, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent implements OnInit, OnDestroy {
    private ngUnsubscribe = new Subject();

    constructor() { }

    ngOnInit() {
        var observable1 = interval(1000);
        var observable2 = interval(2000);

        observable1.pipe(takeUntil(this.ngUnsubscribe)).subscribe(x => console.log('observable1: ' + x));
        observable2.pipe(takeUntil(this.ngUnsubscribe)).subscribe(x => console.log('observable2: ' + x));
    }

    ngOnDestroy() {
        this.ngUnsubscribe.next();
        this.ngUnsubscribe.complete();
    }
}

You can find a detailed explanation of the topic here

Yatharth Varshney
  • 1,973
  • 20
  • 22
1

Use

if(mySubscription){
  mySubscription.unsubscribe();
}
Anshul
  • 9,312
  • 11
  • 57
  • 74
-1
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { SomeAPIService } from '../some_file/someAPIService.service.ts

@Component({
  templateUrl: './your_Page.html',
  styleUrls: ['./your_Styles.scss']
})

export class (your class) implements OnInit, OnDestroy {
   // This is a subject variable at it simplest form 
     private unsubscribe$ = new Subject<void>();

     constructor (private someAPIService : SomeAPIService) {}
   
     ngOnit(): void { 
       this.someAPIService.getTODOlist({id:1})
        .pipe(takeUntil(this.unsubscribe$))
         .subscribe((value: SomeVariable) => {
         // What ever value you need is SomeVariable
      },)
    }


     ngOnDestroy(): void {
    // clears all, page subscriptions 
      this.unsubscribe$.next();
      this.unsubscribe$.complete();
     }
`}
J Crain
  • 1
  • 1