I have an implementation, that automatically unsubscribes from Observables when the component is destroyed using takeUntil
. But it's annoying to implement the same code in many components.
I want to know if this could be simplified
(I can't use async
pipe because I need the emitted values in the Typescript component)
Here is my current implementation:
export class Component implements OnDestroy {
_dstr = new Subject();
data$: Observable<any> = this.store.select(Selector.getData);
constructor(
private store: Store<State>,
) {
this.store.pipe(
select(Selector.getOtherData),
takeUntil(this._dstr),
).subscribe(data => {
console.log('here is my data!', data)
});
}
public ngOnDestroy(): void {
this._dstr.next();
this._dstr.complete();
}
}