Skip to content

Commit

Permalink
lib: implement AbortSignal.abort()
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Mar 10, 2021
1 parent 853086f commit cfb02a0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
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
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) {
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 @@ -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);
}

0 comments on commit cfb02a0

Please sign in to comment.