lib: implement AbortSignal.abort()
Refs: https://github.com/whatwg/dom/pull/960 PR-URL: https://github.com/nodejs/node/pull/37693 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
parent
8e8dea36cc
commit
263bf4e2e7
@ -315,6 +315,7 @@ module.exports = {
|
||||
},
|
||||
globals: {
|
||||
AbortController: 'readable',
|
||||
AbortSignal: 'readable',
|
||||
Atomics: 'readable',
|
||||
BigInt: 'readable',
|
||||
BigInt64Array: 'readable',
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user