Skip to content

Instantly share code, notes, and snippets.

@NickStrupat
Created June 28, 2019 16:04
Show Gist options
  • Save NickStrupat/aad2611ab9b8d9c772a34c37e2f234c3 to your computer and use it in GitHub Desktop.
Save NickStrupat/aad2611ab9b8d9c772a34c37e2f234c3 to your computer and use it in GitHub Desktop.
class for pushing in observable subscriptions in order and then unsubscribing from then in reverse order
import { Subscription } from "rxjs";
export class Subscriptions {
private readonly subscriptions = new Array<Subscription>();
push(subscription: Subscription): void {
this.subscriptions.push(subscription);
}
unsubscribeAll(): void {
// start unsubscribing from last to first
// if unsubscribe throws, don't pop it so whatever catches the exception can retry unsubscribe by calling this method again
while (this.subscriptions.length > 0) {
let subscription = this.subscriptions[this.subscriptions.length - 1];
subscription.unsubscribe();
this.subscriptions.pop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment