From cfb02a0a9a59157e893b466af98ef5794ba480cb Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 10 Mar 2021 08:59:52 -0800 Subject: [PATCH] lib: implement AbortSignal.abort() Refs: https://github.com/whatwg/dom/pull/960 --- .eslintrc.js | 1 + lib/internal/abort_controller.js | 8 ++++++-- test/parallel/test-abortcontroller.js | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 291436cdc4f9b5..7574e1b4b6c7f7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -315,6 +315,7 @@ module.exports = { }, globals: { AbortController: 'readable', + AbortSignal: 'readable', Atomics: 'readable', BigInt: 'readable', BigInt64Array: 'readable', diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index 21414cde0e309e..5a16a56a4a7eb8 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -50,6 +50,10 @@ class AbortSignal extends EventTarget { aborted: this.aborted }, depth, options); } + + static abort() { + return createAbortSignal(true); + } } ObjectDefineProperties(AbortSignal.prototype, { @@ -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; } diff --git a/test/parallel/test-abortcontroller.js b/test/parallel/test-abortcontroller.js index 283aaa93fc7fca..2b36da332e44aa 100644 --- a/test/parallel/test-abortcontroller.js +++ b/test/parallel/test-abortcontroller.js @@ -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); +}