tl;dr: Basically I want to marry Angular's ngOnDestroy
with the Rxjs takeUntil()
operator. -- is that possible?
I have an Angular component that opens several Rxjs subscriptions. These need to be closed when the component is destroyed.
A simple solution for this would be:
class myComponent {
private subscriptionA;
private subscriptionB;
private subscriptionC;
constructor(
private serviceA: ServiceA,
private serviceB: ServiceB,
private serviceC: ServiceC) {}
ngOnInit() {
this.subscriptionA = this.serviceA.subscribe(...);
this.subscriptionB = this.serviceB.subscribe(...);
this.subscriptionC = this.serviceC.subscribe(...);
}
ngOnDestroy() {
this.subscriptionA.unsubscribe();
this.subscriptionB.unsubscribe();
this.subscriptionC.unsubscribe();
}
}
This works, but it's a bit redundant. I especially don't like that
- The unsubscribe()
is somewhere else, so you gotta remember that these are linked.
- The component state is polluted with the subscription.
I would much prefer using the takeUntil()
operator or something similar, to make it look like this:
class myComponent {
constructor(
private serviceA: ServiceA,
private serviceB: ServiceB,
private serviceC: ServiceC) {}
ngOnInit() {
const destroy = Observable.fromEvent(???).first();
this.subscriptionA = this.serviceA.subscribe(...).takeUntil(destroy);
this.subscriptionB = this.serviceB.subscribe(...).takeUntil(destroy);
this.subscriptionC = this.serviceC.subscribe(...).takeUntil(destroy);
}
}
Is there a destroy event or something similar that would let me use takeUntil()
or another way to simplify the component architecture like that?
I realize I could create an event myself in the constructor or something that gets triggered within ngOnDestroy()
but that would in the end not make things that much simpler to read.
Well, this comes down to what you mean by closing a subscription. There're basically two ways to do this:
takeWhile()
).It's good to know that these two aren't the same.
When using for example
takeWhile()
you make the operator sendcomplete
notification which is propagated to your observers. So if you define:Then when you complete the chain with eg.
takeWhile()
thedoWhatever()
function will be called.For example it could look like this:
After 3s all the complete callbacks will be called.
On the other hand when you unsubscribe you're saying that you're no longer interested in the items produced by the source Observable. However this doesn't mean the source has to complete. You just don't care any more.
This means that you can collect all
Subscription
s from.subscribe(...)
calls and unsubscribe all of them at once:Now after 3s delay nothing will be printed to console because we unsubscribed and no complete callback was invoked.
So what you want to use is up to you and your use-case. Just be aware that unsubscribing is not the same as completing even though I guess in your situation it doesn't really matter.
Using the
componentDestroyed()
function from the npm package ng2-rx-componentdestroyed is by far the easiest way to use takeUntil:Here's a version of
componentDestroyed()
to include directly in your code:If your component is directly tied to a route, you can avoid adding state by leveraging
Router
events withtakeUntil()
. That way, as soon as you navigate away from the component, it will clean up its subscriptions automatically for you.Note: This simple example does not take into account guarded routes or canceled route navigation. If there is a chance that one of the router events could be triggered but route navigation gets cancelled, you'll need to filter on the router events so it gets triggered at the appropriate point - for example, after the Route Guard check or once navigation is complete.
You could leverage a
ReplaySubject
for that: