In Angular, is this a valid way of checking whether an observable subscription is open before attempting to close it? It seems to produce the correct behavior. That is, it only closes subscriptions that are open. What I am wondering is whether there is some more "correct" way of doing it in Angular.
import { Component, OnUpdate, OnDestroy } from '@angular/core';
import { AngularFirestore } from 'angularfire2/firestore';
import { VoteService } from '../../services/vote.service';
export class Foo implements OnUpdate, OnDestroy {
private voteSubscription;
constructor(private afs: AngularFirestore,public voteService: VoteService) {}
ngOnUpdate() {
/* Lots of complicated things happen here which result in voteSubscription
being opened and closed based on external conditions including but not
limited to auth state. Bottom line is that sometimes the subscription
is active and sometimes it's not. */
if ( CONSTANTLY_CHANGING_INPUT_CONDITIONS ) {
this.voteSubscription = this.voteService.getUserVotes().subscribe();
} else {
if (this.voteSubscription) {
this.voteSubscription.unsubscribe();
}
}
}
ngOnDestroy() {
/* Avoid producing a console error if we try to close a subscription that is
already closed. */
if (this.voteSubscription) {
this.voteSubscription.unsubscribe();
}
}
}