-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: initial experimental AbortController implementation
AbortController impl based very closely on: https://github.com/mysticatea/abort-controller Marked experimental. Not currently used by any of the existing promise apis. Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #33527 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
- Loading branch information
Showing
14 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict'; | ||
|
||
// Modeled very closely on the AbortController implementation | ||
// in https://github.com/mysticatea/abort-controller (MIT license) | ||
|
||
const { | ||
Object, | ||
Symbol, | ||
} = primordials; | ||
|
||
const { | ||
EventTarget, | ||
Event | ||
} = require('internal/event_target'); | ||
const { | ||
customInspectSymbol, | ||
emitExperimentalWarning | ||
} = require('internal/util'); | ||
const { inspect } = require('internal/util/inspect'); | ||
|
||
const kAborted = Symbol('kAborted'); | ||
|
||
function customInspect(self, obj, depth, options) { | ||
if (depth < 0) | ||
return self; | ||
|
||
const opts = Object.assign({}, options, { | ||
depth: options.depth === null ? null : options.depth - 1 | ||
}); | ||
|
||
return `${self.constructor.name} ${inspect(obj, opts)}`; | ||
} | ||
|
||
class AbortSignal extends EventTarget { | ||
get aborted() { return !!this[kAborted]; } | ||
|
||
[customInspectSymbol](depth, options) { | ||
return customInspect(this, { | ||
aborted: this.aborted | ||
}, depth, options); | ||
} | ||
} | ||
|
||
Object.defineProperties(AbortSignal.prototype, { | ||
aborted: { enumerable: true } | ||
}); | ||
|
||
function abortSignal(signal) { | ||
if (signal[kAborted]) return; | ||
signal[kAborted] = true; | ||
const event = new Event('abort'); | ||
if (typeof signal.onabort === 'function') { | ||
signal.onabort(event); | ||
} | ||
signal.dispatchEvent(event); | ||
} | ||
|
||
class AbortController { | ||
#signal = new AbortSignal(); | ||
|
||
constructor() { | ||
emitExperimentalWarning('AbortController'); | ||
} | ||
|
||
get signal() { return this.#signal; } | ||
abort() { abortSignal(this.#signal); } | ||
|
||
[customInspectSymbol](depth, options) { | ||
return customInspect(this, { | ||
signal: this.signal | ||
}, depth, options); | ||
} | ||
} | ||
|
||
Object.defineProperties(AbortController.prototype, { | ||
signal: { enumerable: true }, | ||
abort: { enumerable: true } | ||
}); | ||
|
||
module.exports = { | ||
AbortController, | ||
AbortSignal, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Flags: --no-warnings --experimental-abortcontroller | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const { ok, strictEqual } = require('assert'); | ||
|
||
{ | ||
const ac = new AbortController(); | ||
ok(ac.signal); | ||
ac.signal.onabort = common.mustCall((event) => { | ||
ok(event); | ||
strictEqual(event.type, 'abort'); | ||
}); | ||
ac.signal.addEventListener('abort', common.mustCall((event) => { | ||
ok(event); | ||
strictEqual(event.type, 'abort'); | ||
}), { once: true }); | ||
ac.abort(); | ||
ac.abort(); | ||
ok(ac.signal.aborted); | ||
} |
Oops, something went wrong.