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