Re: [whatwg/dom] Expose an `aborted` promise on AbortSignal? (#946)

I haven't been following much of the subsequent discussion. However, my main comments on the OP are around the listener removal.

In particular, my understanding is that the proposal is _not_ equivalent to the OP's

```js
resource.on('close', () => signal.removeEventListener(listener));
```

example, but instead something more like

```js
// This code probably leaks the signal, but I think a more complicated version could avoid that...
const registry = new FinalizationRegistry(() => {
  signal.removeEventListener(listener);
});
registry.register(resource);
```

because the web platform (and really Node as well) don't have a uniform `'close'` event for indicating when a resource is disposed. Is this understanding correct? If so, is this divergence problematic?

This might be the first time the web platform uses GC observation in such a way? But, this seems like a reasonable use of GC observation: it's using GC observation to help other things get GCed, not to do important program logic. So I'm not too worried there.

Another angle is, how does this contrast with weak listeners? My impression (but not a very confident one) is that this proposal actually has two parts: weak listeners, plus sugar to bundle together the `aborted` check and the weak listener attachment. That is, it could be written as

```js
function aborted(signal, resource) {
  if (signal.aborted) {
    return Promise.resolve();
  }
  
  else {
    return new Promise(resolve => {
      // hypothetical method
      signal.addWeakEventListener('abort', resolve, { fireOnlyIfStillAlive: resource });
    });
  }
}
```

I think this `addWeakListener(..., { fireOnlyIfStillAlive })` function was discussed in https://github.com/whatwg/dom/issues/243 some years ago. This is different from the notion of weak listeners discussed in https://v8.dev/features/weak-references, which is roughly that the _listener itself_ should not be kept alive by the EventTarget. I'm not sure which category https://github.com/nodejs/node/pull/36607 falls into?

Anyway, my instinct is that if we want a feature like this, it'd be best to spec both the high-level helper, and the lower-level building block (like a weak listener).

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/946#issuecomment-773591295

Received on Thursday, 4 February 2021 20:42:29 UTC