-
Notifications
You must be signed in to change notification settings - Fork 30k
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 <[email protected]> PR-URL: #33527 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
- 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.