Skip to content
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

How does one register *what* to dispose? #156

Closed
benlesh opened this issue May 23, 2023 · 2 comments
Closed

How does one register *what* to dispose? #156

benlesh opened this issue May 23, 2023 · 2 comments

Comments

@benlesh
Copy link

benlesh commented May 23, 2023

This seems like it's only half a spec. While it might unify implicit disposal of resources, it doesn't do anything to address how those resources should be registered for disposal. It also means that everyone is going to be implementing their own disposables, ad hoc, so they can be used with this using statement. It would be so much better, in my opinion, if a consistent API was available for people to register teardowns and get a disposable.

Even worse, how are people going to tie this to the only "universal" cancellation mechanism, AbortSignal?

I'm concerned we're building a mess here instead of a cohesive developer experience.

From my experience with RxJS:

  1. Sometimes users want implicit disposal
  2. Sometimes users what explicit disposal (That is disposal WITH cancellation)
  3. Frequently users will need to register one or more things that need to happen during disposal
  4. Occasionally code will try to register something for disposal on something that's already disposed

As best I can tell, this proposal handles number 1 only?

@bakkot
Copy link

bakkot commented May 23, 2023

It also means that everyone is going to be implementing their own disposables, ad hoc, so they can be used with this using statement. It would be so much better, in my opinion, if a consistent API was available for people to register teardowns and get a disposable.

I am confused about what distinction you are drawing between "implementing their own disposables, ad hoc" vs "a consistent API was available for people to register teardowns".

Do you just mean that you want it to be easy for users of something which is not disposable to set up disposal? That's offered by DisposableStack. From the readme:

using stack = new DisposableStack();
const reader = stack.adopt(createReader(), reader => reader.releaseLock());

or

using stack = new DisposableStack();
console.log("enter");
stack.defer(() => console.log("exit"));

If that's not what you're thinking of, can you give an example of the sort of thing you mean?

(DisposableStack will let you register multiple things to get disposed, which sounds like it's an answer to your "users will need to register one or more things that need to happen during disposal" point.)

Even worse, how are people going to tie this to the only "universal" cancellation mechanism, AbortSignal?

I want to have a cancel-on-dispose AbortController, which sounds like an answer to this question? That would let you do

using controller = new AbortController.AutoAbort();
let pages = await Promise.all(urls.map(url => fetch(url, { signal: controller.signal }));
// automatically cancels outstanding requests if any request fails

It will need to be pursued in HTML, though; I've been waiting for implementations of this proposal to begin before I start pushing the work there.

@benlesh
Copy link
Author

benlesh commented May 23, 2023

Hmm.. maybe I missed some of this because I was looking on my phone. I'll close this issue until I re-read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants