In my Angular app, I have a BaseComponent
class that includes the implementation of destroy$: Subject<void>
for observables. Whenever I subscribe to a service, I find myself repeatedly writing:
this.myService.loadData()
.pipe(takeUntil(this.destroy$))
.subscribe(data => this.userData = data);
I am looking for a way to avoid the repetitive use of .pipe(takeUntil(this.destroy$))
and am considering creating an extension method for observables to handle takeUntil
. For instance, I would like to move .pipe(takeUntil(this.destroy$))
to a custom method called ifAlive
so that it could then be used like this:
this.myService.loadData()
.ifAlive()
.subscribe(...);
Any suggestions or ideas on how to achieve this?