diff options
author | James M Snell <jasnell@gmail.com> | 2020-05-23 04:11:14 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2020-06-05 22:21:47 +0300 |
commit | 74ca960aac4600953caacd5f250b11a90bd70ee7 (patch) | |
tree | 241afade93b93c034e23339896e7386f32c3b3e9 /lib/internal/abort_controller.js | |
parent | 3e2a3007107b7a100794f4e4adbde19263fc7464 (diff) |
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: https://github.com/nodejs/node/pull/33527
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib/internal/abort_controller.js')
-rw-r--r-- | lib/internal/abort_controller.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js new file mode 100644 index 00000000000..b4ba6dd4f87 --- /dev/null +++ b/lib/internal/abort_controller.js @@ -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, +}; |