Skip to content

Commit

Permalink
lib: implement AbortSignal.abort()
Browse files Browse the repository at this point in the history
Refs: whatwg/dom#960

PR-URL: #37693
Backport-PR-URL: #38386
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
jasnell authored and targos committed Apr 30, 2021
1 parent 4cd9f39 commit 448a6a2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,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 @@ -65,6 +65,15 @@ added: REPLACEME
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: REPLACEME
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);
}
}

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

defineEventHandler(AbortSignal.prototype, 'abort');

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

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 @@ -68,3 +68,8 @@ const { Event } = require('internal/event_target');
strictEqual(toString(ac), '[object AbortController]');
strictEqual(toString(ac.signal), '[object AbortSignal]');
}

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

0 comments on commit 448a6a2

Please sign in to comment.