-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A subclass that overrides DisposableStack#dispose must also override DisposableStack#[Symbol.dispose] #231
Comments
If Symbol.dispose was instead a getter on the prototype, or was defined as a function that called this.dispose(), that would alleviate the issue. We'd want to document in that case that users should override |
In an earlier draft of the proposal, |
In the meantime, a potential workaround that matches the current spec behavior would be to do the following: class NotifyingStack extends DisposableStack {
dispose() {
const wasDisposed = this.disposed;
super.dispose();
if (!wasDisposed) {
notify();
}
}
static { this.prototype[Symbol.dispose] = this.prototype.dispose; }
} |
Also, is there a reason you are not just using class NotifyingStack extends DisposableStack {
constructor(notify) {
this.defer(notify);
}
} |
Ha, that's elegant! The actual use case I was evaluating was |
Consider code like:
By my reading of https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack.prototype-@@dispose this code is buggy,
notify
won't be called when NotifyingStack.prototype[Symbol.dispose] is called, only when its dispose method is called. Such a subclass would need to also update NotifyingStack.prototype[Symbol.dispose]. This could be a potential footgun in the API.Likewise AsyncDisposableStack and its disposeAsync, and Symbol.asyncDispose fields.
The text was updated successfully, but these errors were encountered: