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

Proposal AbortSignal.prototype.filter(compare) #1280

Open
mariusGundersen opened this issue Apr 18, 2024 · 4 comments
Open

Proposal AbortSignal.prototype.filter(compare) #1280

mariusGundersen opened this issue Apr 18, 2024 · 4 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: aborting AbortController and AbortSignal

Comments

@mariusGundersen
Copy link

What problem are you trying to solve?

There is now an AbortSignal.any([a, b, c]) that makes it possible to combine multiple signals, but there is currently no way to split a an AbortSignal. I therefore propose the instance method AbortSignal.prototype.filter(compare), which would return a new AbortSignal that only triggers if the reason matches some condition. For example:

function(signal){
  const timeoutSignal = signal.filter(reason => reason instanceof DOMException && reason.name === 'TimeoutError'); // only when the reason is a TimeoutError
}

The above example creates a signal that will only trigger if the reason matches that of AbortSignal.timeout(ms). If the reason doesn't match, then the returned signal will never trigger (since abort signals only ever trigger once).

What solutions exist today?

It is fairly easy to implement this today as a separate function (to not pollute the prototype):

function filterAbortReason(signal, compare) {
    if (signal.aborted && compare(signal.reason)) return AbortSignal.abort(signal.reason);
    const abortController = new AbortController();
    signal.addEventListener('abort', () => {
        if (compare(signal.reason)) {
            abortController.abort(signal.reason);
        }
    });
    return abortController.signal;
}

How would you solve it?

The above function could be placed on the AbortSignal prototype:

AbortSignal.prototype.filter = function(compare) {
    if (this.aborted && compare(this.reason)) return AbortSignal.abort(this.reason);
    const abortController = new AbortController();
    this.addEventListener('abort', () => {
        if (compare(this.reason)) {
            abortController.abort(this.reason);
        }
    });
    return abortController.signal;
}

Anything else?

No response

@mariusGundersen mariusGundersen added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Apr 18, 2024
@annevk annevk added the topic: aborting AbortController and AbortSignal label Apr 18, 2024
@annevk
Copy link
Member

annevk commented Apr 18, 2024

With proposals like this it helps to have some information about adoption in libraries, code in the wild, other languages with signals, questions on Stack Overflow, etc. I.e., a bit more data that demonstrates this is worth investing time in.

@domenic
Copy link
Member

domenic commented Apr 19, 2024

I don't think we should unilaterally add error filtering to AbortSignal; doing this for, e.g., Promise first seems more reasonable.

@mariusGundersen
Copy link
Author

Do you mean adding filter to the Promise prototype? I didn't think AbortSignal inherited anything from promises? Or just copy the details of a Promise filter to AbortSignal later?

@domenic
Copy link
Member

domenic commented Apr 21, 2024

I mean, we have multiple primitives in the platform which forward errors to users. We shouldn't add a filtering method to just one. So yes, something similar to

Or just copy the details of a Promise filter to AbortSignal later?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: aborting AbortController and AbortSignal
Development

No branches or pull requests

3 participants