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

lib: implement AbortSignal.abort() #37693

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ module.exports = {
},
globals: {
AbortController: 'readable',
AbortSignal: 'readable',
Atomics: 'readable',
BigInt: 'readable',
BigInt64Array: 'readable',
Expand Down
9 changes: 9 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ added: v15.0.0
The `AbortSignal` is used to notify observers when the
`abortController.abort()` method is called.

#### Static method: `AbortSignal.abort()`
<!-- YAML
added: REPLACEME
-->

* Returns: {AbortSignal}

Returns a new already aborted `AbortSignal`.

#### Event: `'abort'`
<!-- YAML
added: v15.0.0
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class AbortSignal extends EventTarget {
aborted: this.aborted
}, depth, options);
}

static abort() {
return createAbortSignal(true);
}
}

ObjectDefineProperties(AbortSignal.prototype, {
Expand All @@ -65,10 +69,10 @@ ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {

defineEventHandler(AbortSignal.prototype, 'abort');

function createAbortSignal() {
function createAbortSignal(aborted = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this approach rather than doing new AbortSignalesque?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new AbortSignal() is not permitted by the spec.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, we ended up doing this and not the "fake parameter in constructor" approach.

const signal = new EventTarget();
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
signal[kAborted] = false;
signal[kAborted] = aborted;
return signal;
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Last update:

- common: https://github.com/web-platform-tests/wpt/tree/bb97a68974/common
- console: https://github.com/web-platform-tests/wpt/tree/3b1f72e99a/console
- dom/abort: https://github.com/web-platform-tests/wpt/tree/625e1310ce/dom/abort
- dom/abort: https://github.com/web-platform-tests/wpt/tree/1728d198c9/dom/abort
- encoding: https://github.com/web-platform-tests/wpt/tree/35f70910d3/encoding
- FileAPI: https://github.com/web-platform-tests/wpt/tree/3b279420d4/FileAPI
- hr-time: https://github.com/web-platform-tests/wpt/tree/9910784394/hr-time
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/wpt/dom/abort/event.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ test(t => {
controller.abort();
}, "the abort event should have the right properties");

test(t => {
const signal = AbortSignal.abort();
assert_true(signal.aborted);
}, "the AbortSignal.abort() static returns an already aborted signal");

done();
2 changes: 1 addition & 1 deletion test/fixtures/wpt/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"path": "console"
},
"dom/abort": {
"commit": "625e1310ce19e9dde25b01f9eda0452c6ec274da",
"commit": "1728d198c92834d92f7f399ef35e7823d5bfa0e4",
"path": "dom/abort"
},
"encoding": {
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-abortcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ const { ok, strictEqual, throws } = require('assert');
strictEqual(toString(ac), '[object AbortController]');
strictEqual(toString(ac.signal), '[object AbortSignal]');
}

{
const signal = AbortSignal.abort();
ok(signal.aborted);
}