Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Angular/RxJs When should I unsubscribe from `Subscription`

// You don't need to have bunch of subscriptions and unsubscribe manually.
// Use Subject and takeUntil combo to handle subscriptions like a boss:


import { Subject } from "rxjs"
import { takeUntil } from "rxjs/operators"

export class ViewRouteComponent implements OnInit, OnDestroy {
  componentDestroyed$: Subject<boolean> = new Subject()

  constructor(private titleService: TitleService) {}

  ngOnInit() {
    this.titleService.emitter1$
      .pipe(takeUntil(this.componentDestroyed$))
      .subscribe((data: any) => { /* ... do something 1 */ })

    this.titleService.emitter2$
      .pipe(takeUntil(this.componentDestroyed$))
      .subscribe((data: any) => { /* ... do something 2 */ })

    //...

    this.titleService.emitterN$
      .pipe(takeUntil(this.componentDestroyed$))
      .subscribe((data: any) => { /* ... do something N */ })
  }

  ngOnDestroy() {
    this.componentDestroyed$.next(true)
    this.componentDestroyed$.complete()
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: An unhandled exception occurred: Schematic "Module" not found in collection "@schematics/angular". 
Typescript :: how to install tsu 
Typescript :: isolate digits in large number cpp 
Typescript :: object map of the http parameters mutually exclusive with fromString 
Typescript :: This method can provide higher level of accuarcy in cost estimation based on the given historical data 
Typescript :: typeorm where in example 
Typescript :: how to implement read more and readless in angular 
Typescript :: nativescript display image from web 
Typescript :: apply function to all elements with a class name 
Typescript :: which of the following are elements associated with the html table layout? 
Typescript :: How to check that tuple A contains all elements of tuple B python? 
Typescript :: python arbitrary arguments *args mcqs 
Typescript :: nestjs fail on unknown properties 
Typescript :: dots are displaying only when trying to fetch records html template 
Typescript :: loading assets in ionic react 
Typescript :: graphql?query={__schema{types{name,fields{name}}}} 
Typescript :: return type depends on input typescript 
Typescript :: npm run multiple scripts sequentially 
Typescript :: how to read web page in type script 
Typescript :: show number with atelast 23 disgits before decmal angular 
Typescript :: Count pets the types of pets in a columns 
Typescript :: Laravel 8 working with subdomain routing but its not working 
Typescript :: 3 parts of apptitude 
Typescript :: INFO: This is taking longer than usual. You might need to provide the dependency resolver with stricter constraints to reduce runtime. 
Typescript :: distance between two lat long points google maps api 
Typescript :: find number of digits that changed after addition of 1 
Cpp :: hello world c++ 
Cpp :: qstring mid 
Cpp :: is javascript for websites only 
Cpp :: c++ read console input 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =