diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-07-19 19:49:44 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-19 19:49:44 +0200 |
| commit | fa61956f03491101b6ef64423ea2f1f73af26a73 (patch) | |
| tree | c3800702071ca78aa4dd71bdd0a59a9bbe460bdd /cli/js2/02_abort_signal.js | |
| parent | 53adde866dd399aa2509d14508642fce37afb8f5 (diff) | |
Port internal TS code to JS (#6793)
Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'cli/js2/02_abort_signal.js')
| -rw-r--r-- | cli/js2/02_abort_signal.js | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/cli/js2/02_abort_signal.js b/cli/js2/02_abort_signal.js new file mode 100644 index 000000000..cd38fff64 --- /dev/null +++ b/cli/js2/02_abort_signal.js @@ -0,0 +1,75 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +((window) => { + const add = Symbol("add"); + const signalAbort = Symbol("signalAbort"); + const remove = Symbol("remove"); + + class AbortSignal extends EventTarget { + #aborted = false; + #abortAlgorithms = new Set(); + + [add](algorithm) { + this.#abortAlgorithms.add(algorithm); + } + + [signalAbort]() { + if (this.#aborted) { + return; + } + this.#aborted = true; + for (const algorithm of this.#abortAlgorithms) { + algorithm(); + } + this.#abortAlgorithms.clear(); + this.dispatchEvent(new Event("abort")); + } + + [remove](algorithm) { + this.#abortAlgorithms.delete(algorithm); + } + + constructor() { + super(); + this.onabort = null; + this.addEventListener("abort", (evt) => { + const { onabort } = this; + if (typeof onabort === "function") { + onabort.call(this, evt); + } + }); + } + + get aborted() { + return Boolean(this.#aborted); + } + + get [Symbol.toStringTag]() { + return "AbortSignal"; + } + } + + class AbortController { + #signal = new AbortSignal(); + + get signal() { + return this.#signal; + } + + abort() { + this.#signal[signalAbort](); + } + + get [Symbol.toStringTag]() { + return "AbortController"; + } + } + + window.__bootstrap.abortSignal = { + AbortSignal, + add, + signalAbort, + remove, + AbortController, + }; +})(this); |
