-
Notifications
You must be signed in to change notification settings - Fork 1k
Add [Symbol.dispose] alias to Disposable class #1203
Comments
While the general ideas are related, it's not clear to me that (in practice) this would actually help much. In particular, In general, |
Sure, and yeah we have a lot of Disposables that live a long time. it('should do stuff', () => {
using injector = createInjector();
const item = injector.get(UnitUnderTest);
// test
}); vs. it('should do stuff', () => {
const injector = createInjector();
const item = injector.get(UnitUnderTest);
// test
injector.dispose();
}); It would also be convenient in occasional functions that create short lived versions of classes that are disposable, just to do some processing, get a result, and return the result. function f() {
using someDisposable = new SomeDisposable();
return someDisposable.someComputation();
} vs. function f() {
const someDisposable = new SomeDisposable();
const result = someDisposable.someComputation();
someDisposable.dispose();
return result;
} Neither of these cases actually need the |
Thanks for those use cases. Presumably Closure Compiler will want to transpile |
I've been using standard disposables with The first examples you see are pretty trivial, looking like: async function getAnswer() {
using eventSource = constructEventSource();
using handler = new SourceHandler(eventSource);
return handler.getAnswer();
} // `using` syntax means we can count on handler and eventSource to be disposed of, even if the function throws But with DisposableStack, you can reliably make objects that compose other disposables: class Answerer {
constructor() {
using localDisposables = new DisposableStack();
const eventSource = localDisposables.use(constructEventSource());
const handler = localDisposables.use(new SourceHandler(eventSource);
this.handler = handler;
// Here's the key point: we're moving the block-scoped disposables into
// this.disposables. After the move, localDisposables is empty and already
// disposed, and the returned Answerer is now responsible for eventSource
// and handler, and they'll be disposed when it is.
//
// But if the constructor throws an error, all the resources it created before
// it threw will still be disposed of, because they're tracked by localDisposables
// which owns them up until the end of the constructor.
this.disposables = localDisposables.move();
}
[Symbol.dispose]() {
this.disposables[Symbol.dispose]();
}
} |
I think the change should be something more like: if (typeof Symbol?.dispose === 'symbol') {
goog.Disposable.prototype[Symbol.dispose] = function() {
this.dispose();
};
} That way, if a subclass overrides |
This issue is being closed as a preparation step before archiving the repository. See the README for more detail. |
With explicit resource management being added to the language in the near future (supported in TypeScript 5.2), it would be nice to be able to use the
using
keyword with closure-library Disposables.I think this would be as simple as this change:
I can make this change (with some actual comments) if no one objects.
The text was updated successfully, but these errors were encountered: