Skip to content

Instantly share code, notes, and snippets.

@kring
Created December 3, 2019 11:19
Show Gist options
  • Save kring/bab730a5696d5afde49c705d99ee3488 to your computer and use it in GitHub Desktop.
Save kring/bab730a5696d5afde49c705d99ee3488 to your computer and use it in GitHub Desktop.
class ShortLived {
@observable
private isDisposed = false;
constructor(readonly longLived) {
this.longLived = longLived;
}
@computed({ keepAlive: true })
get bar() {
if (this.isDisposed) {
throw new Error("Disposed");
}
return this.longLived.foo * 2;
}
dispose() {
this.isDisposed = true;
// Re-evaluate bar so it removes itself from all observables it
// previously observed.
try {
this.bar;
} catch (e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment