Skip to content

Instantly share code, notes, and snippets.

@qmzik
Created July 11, 2021 12:23
Show Gist options
  • Save qmzik/65664ad3e1c6f9bcba1d63d0751bbc14 to your computer and use it in GitHub Desktop.
Save qmzik/65664ad3e1c6f9bcba1d63d0751bbc14 to your computer and use it in GitHub Desktop.
class SafeSubscriber<T> {
closed = false;
constructor(private destination: Partial<Observer<T>>) {}
next(value: T) {
if (!this.closed) {
this.destination.next?.(value); // Note the ?. check here.
}
}
complete() {
if (!this.closed) {
this.closed = true;
this.destination.complete?.(); // And here.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment