Unsubscribing Multiple Subscription from Different Functions

Gray Singh

i have multiple functions that have subscriptions. First i have it on the ngOnInit(), then i have another function called onSelectVillain(). So my question is this possible that you could just use this.subscription.unsubscribe(). Or should i make declare another subscription?

subscription: Subscription;

    ngOnInit() {
      this.subscription = this.heroService.getHeroes()
                       .subscribe(
                         heroes => this.heroes = heroes,
                         error =>  this.errorMessage = <any>error);
    }

    onSelectVillain(event) {
      this.subscription = this.villainService.getVillains()
                       .subscribe(
                         .....
    }

    ngOnDestroy(){
     this.subscription.unsubscribe()
    }
Jay

It would be cleaner to use a seperate subscription - and if you use the same field you would never (manually) unsubscribe from the first subscription. Also if you don't want to clutter your component with lots of fields, that simply hold subscription references I would recommend to use a pattern, that involves using just one Subject, that is triggered at ngOnDestroy and before each subscribe you would use takeUntil. So your code could look like this:

private ngUnsubscribe = new Subject();

ngOnInit() {
  this.heroService.getHeroes()
                  .takeUntil(this.ngUnsubscribe)
                  .subscribe(
                     heroes => this.heroes = heroes,
                     error =>  this.errorMessage = <any>error);
}

onSelectVillain(event) {
  this.villainService.getVillains()
                     .takeUntil(this.ngUnsubscribe)
                     .subscribe(
                     .....
}

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

See this for further reference.

Note that subscriptions that are "finite", so where a complete state is called, don't neccessarily need to get unsubscribed manually. This is probably a good reference point for that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Call multiple python functions from different directories

Windows Service not stopping, stuck at unsubscribing ews subscription

Unsubscribing from Observables inside services?

Unsubscribing from Firebase Realtime Database

Unsubscribing from a ReplaySubject returned asObservable();

Immediately unsubscribing from RxJS Observable

Unsubscribing from valuechanged doesnt work

Unsubscribing from events on page change

Angular6 rxjs unsubscribing to timer subscription OnDestroy does not work?

How to attach ACR from different Subscription to AKS?

Accessing Azure Storage services from a different subscription

Different page from different functions

Multiple functions with different sub-type functions

Azure functions: Multiple functions with different authentication

Unsubscribing from RxJava2/RxAndroid PublishSubject

Unsubscribing from the event in delegate vs function

Unsubscribing from queryparammap - has this become necessary?

Unsubscribing from an rx.Single in RxJava

Issue when unsubscribing from onSnapshot listener?

How to Run SSRS Subscription multiple times with different parameters?

Terraform, azurem create container app with image from a different subscription

Two different outputs from functions

This subscription isrestricted from provisioning MySQL server in this regionPlease choose a different region with service,subscription limit issue typ

How to deploy multiple Microsoft azure functions from different projects (solutions) to same resource group

C++ multiple callback member functions from different classes without std and boost

Firebase Cloud Functions JavaScript - How to get multiple data from different tables in one function

How do you add multiple arguments from different functions into one function containing all the parameters?

calling different functions from different divs

Python - Rply assign multiple different rules to multiple different functions