-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
abort-controller.ts
63 lines (55 loc) · 1.57 KB
/
abort-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import AbortSignal, { abortSignal, createAbortSignal } from "./abort-signal"
/**
* The AbortController.
* @see https://dom.spec.whatwg.org/#abortcontroller
*/
export default class AbortController {
/**
* Initialize this controller.
*/
public constructor() {
signals.set(this, createAbortSignal())
}
/**
* Returns the `AbortSignal` object associated with this object.
*/
public get signal(): AbortSignal {
return getSignal(this)
}
/**
* Abort and signal to any observers that the associated activity is to be aborted.
*/
public abort(): void {
abortSignal(getSignal(this))
}
}
/**
* Associated signals.
*/
const signals = new WeakMap<AbortController, AbortSignal>()
/**
* Get the associated signal of a given controller.
*/
function getSignal(controller: AbortController): AbortSignal {
const signal = signals.get(controller)
if (signal == null) {
throw new TypeError(
`Expected 'this' to be an 'AbortController' object, but got ${
controller === null ? "null" : typeof controller
}`,
)
}
return signal
}
// Properties should be enumerable.
Object.defineProperties(AbortController.prototype, {
signal: { enumerable: true },
abort: { enumerable: true },
})
if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
Object.defineProperty(AbortController.prototype, Symbol.toStringTag, {
configurable: true,
value: "AbortController",
})
}
export { AbortController, AbortSignal }