Re: [whatwg/dom] Allow subclassing of AbortController / AbortSignal (Issue #1162)

> I like the pattern but I struggle to see how it could apply to this considering we have two discrete objects which share a private API:
> 
> ```js
> class WatchSignal extends AbortSignal {
>   constructor(abort) {
>     super(abort)
>   } 
> }
> 
> class WatchController extends AbortController {
>   constructor() {
>     super(function (abort) { // What is even `abort`? Maybe userland doesn't care?
> 
>       // Presumably we need to return an AbortSignal instance
>       // to have AbortController assign this as the signal?
>       return new WatchSignal(abort)
>     })
>   } 
> }
> ```

It would probably make sense if it were just the controller itself, this also allows a subclass pair to share data between the controller and signal easily:

```js
const activePriorities = new WeakMap();

class PrioritySignal extends AbortSignal {
    #priorityController;

    constructor(priorityController) {
        // AbortSignal can access the usual internal slots of the controller
        super(priorityController);
        // We can access whatever shared things we need of the watchController here too
        this.#priorityController = priorityController;
    }

    get priority() {
        return activePriorities.get(this.#priorityController);
    }
}

class PriorityController extends AbortController {
    constructor(initialPriority = 1) {
        super(controller => {
             return new WatchSignal(controller);
        });
        activePriorities.set(this, initialPriority);
    }
}
```

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

Message ID: <whatwg/dom/issues/1162/1643001447@github.com>

Received on Thursday, 20 July 2023 02:10:15 UTC