webidl
webidl copied to clipboard
Add a `disposer` keyword for disposal methods
What problem are you trying to solve?
https://github.com/tc39/proposal-explicit-resource-management is stage 3, with at least V8 starting to ship. Standards should be able to integrate with that with their existing disposer methods.
What solutions exist today?
Many standards have methods like .close(), .abort(), and so on.
How would you solve it?
Add a disposer keyword that interfaces can use to alias a method to Symbol.dispose, and a similar async_disposer for Symbol.asyncDispose.
- All parameters must be optional.
async_disposers must return promises.
For a few examples:
[Exposed=(Window,Worker)]
interface WebSocket : EventTarget {
// ...
disposer undefined close(optional [Clamp] unsigned short code, optional USVString reason);
// ...
};
interface mixin ReadableStreamGenericReader {
readonly attribute Promise<undefined> closed;
async_disposer Promise<undefined> cancel(optional any reason);
};
[Exposed=(Window,Worker)]
interface IDBTransaction : EventTarget {
// ...
undefined commit();
disposer undefined abort();
// ...
};
Anything else?
No response
From the POV of an implementor in Node.js and Cloudflare Workers, I'd definitely be +1 on this and see this as a prereq for adding dispose/async-dispose to various web platform standards.
Why not have a single keyword and make the type depend on the return type? async_ seems rather redundant with Promise.
Why not have a single keyword...
That works for me also. I'm less concerned with the specific syntax than I am with having an ability to declare that an object is disposable.
I think that maybe the one other place this specific proposal probably breaks down is that it assumes the disposer method is based around a public API, at least as shown in the examples above. This might not be the case and the only disposer method exposed might be the Symbol.dispose and Symbol.asyncDispose properties. So, perhaps instead of marking a specific method as the disposer -- or in addition to it -- we should have a standalone disposer; attribute... e.g.
interface Foo {
disposer; // Means that there is a [Symbol.dispose] property on the interface
async_disposer; // Means that there is a [Symbol.asyncDispose] property on the interface
}
interface Bar {
disposer undefined close(); // Means that the close method is also exposed as [Symbol.dispose]
async_disposer Promise<undefined> asyncClose(); // Means that the asyncClose() method is exposed as [Symbol.asyncDispose]
}
Makes things a bit more complicated, of course, so I'm not sure if this is the right way to go or not, but if there is no otherwise exposed method to annotate (as in the Foo example above) then async_disposer would be necessary to differentiate.
Why not have a single keyword and make the type depend on the return type?
async_seems rather redundant withPromise.
I'm neutral either way. I just want to be able to do stuff like using {signal} = new AbortController(), so anything that accomplishes that goal (and isn't horribly inefficient) is good by me. 🙂
Here's an attempt to get something started: https://github.com/whatwg/webidl/pull/1488
It covers only the stand-alone attributes at the moment, e.g.
interface Foo {
disposer;
async_disposer;
}