Skip to content

Instantly share code, notes, and snippets.

@imkrish imkrish/unsubscribe-01.ts
Last active Jun 16, 2019

Embed
What would you like to do?
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
export class RxJSUtil {
static unsubscribe = (subscriptions: Subscription[]) =>
subscriptions.forEach(subscription => subscription.unsubscribe());
}
@Component({
selector: 'nx-tutorial-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {
@Selector(CustomersState.SelectedCustomer)
private selectedCustomer$: Observable<Customer>;
@Selector(OrdersState.NewOrder)
private newOrder$: Observable<Order>;
private subscriptions: Subscription[];
constructor() {
this.subscriptions = [];
}
ngOnInit() {
this.subscriptions.push(
this.selectedCustomer$.subscribe(customer => {
// Do something
}),
this.newOrder$.subscribe(order => {
// Do something
})
);
}
ngOnDestroy() {
RxJSUtil.unsubscribe(this.subscriptions);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.