Created
December 3, 2019 11:19
-
-
Save kring/bab730a5696d5afde49c705d99ee3488 to your computer and use it in GitHub Desktop.
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
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