If I am ever working with rxjs
, I normally, in my component file, keep track of all of my subscriptions by pushing said subscriptions into a Subscription
array:
private subcriptions: Subscription[];
this.subscriptions.push(this.myObservableOne.subscribe(...));
this.subscriptions.push(this.myObservableTwo.subscribe(...));
public ngOnDestroy(): void {
this.subscriptions.forEach(s => s.unsubscribe());
}
Now, I am running into something that I thought was interesting. If I instead need to subscribe to an Observable from a service:
this.myService.myObservable.subscribe(...)
Who owns the subscription? Will my component own it, or will the service own it? In other words, will my service need to implement ngOnDestroy
to unsubscribe from that Observable?
I feel like doing: this.subscriptions.push(this.myService.myObservable.subscribe(...));
might work but I am not entirely sure.