Created
June 28, 2019 16:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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