-
Notifications
You must be signed in to change notification settings - Fork 0
/
abort.js
135 lines (122 loc) · 3.29 KB
/
abort.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @fileoverview
*
* @see https://mdn.io/AbortSignal
* @see https://github.com/nodejs/node/blob/master/lib/internal/abort_controller.js
* @see https://github.com/denoland/deno/blob/master/op_crates/web/02_abort_signal.js
*/
const hasSymbol = typeof Symbol === 'function'
const getSymbol = hasSymbol ? Symbol : (v) => v
const kAborted = getSymbol('kAborted')
const kSignal = getSymbol('kSignal')
const EventLike =
typeof EventTarget !== 'undefined'
? EventTarget
: typeof MessagePort !== 'undefined'
? MessagePort
: null
// NOTE: not using `class extends`, making it compatible with IE 11 and Firefox 59+
// see https://caniuse.com/mdn-api_eventtarget_eventtarget
function AbortSignal() {
throw new TypeError('Illegal constructor')
}
AbortSignal.prototype = Object.create(EventLike.prototype, {
constructor: {value: AbortSignal},
aborted: {
enumerable: true,
get() {
return this[kAborted]
},
},
})
const kHandlers = getSymbol('kHandlers')
function getHandlers(self) {
if (!self[kHandlers]) {
self[kHandlers] = new Map()
}
return self[kHandlers]
}
function defineEventHandler(emitter, name) {
Object.defineProperty(emitter, 'on' + name, {
get() {
return getHandlers(this).get(name) || null
},
set(value) {
const handlers = getHandlers(this)
const handler = handlers.get(name)
if (handler) {
this.removeEventListener(name, handler)
}
if (typeof value === 'function') {
const newHandler = value.bind(this)
handlers.set(name, newHandler)
this.addEventListener(name, newHandler)
} else {
handlers.set(name, null)
}
},
configurable: true,
enumerable: true,
})
}
defineEventHandler(AbortSignal.prototype, 'abort')
function createAbortSignal() {
let signal
try {
// https://caniuse.com/mdn-api_eventtarget_eventtarget
signal = new EventTarget()
} catch (e) {
// IE 10+ https://caniuse.com/mdn-api_messagechannel_port1
signal = new MessageChannel().port1
}
Object.setPrototypeOf(signal, AbortSignal.prototype)
signal[kAborted] = false
return signal
}
function createEvent(type) {
// https://caniuse.com/mdn-api_event_event
try {
return new Event(type)
} catch (_) {
// IE 9+ https://caniuse.com/customevent https://caniuse.com/mdn-api_document_createevent
const event = document.createEvent('Event')
event.initEvent('abort', false, false)
return event
}
}
function abortSignal(signal) {
if (signal[kAborted]) return
signal[kAborted] = true
signal.dispatchEvent(createEvent('abort'))
}
function AbortController() {
this[kSignal] = createAbortSignal()
}
Object.defineProperties(AbortController.prototype, {
signal: {
enumerable: true,
get() {
return this[kSignal]
},
},
abort: {
enumerable: true,
value() {
abortSignal(this[kSignal])
},
},
})
function defineStringTag(ctor, tag) {
if (hasSymbol && Symbol.toStringTag) {
Object.defineProperty(ctor.prototype, Symbol.toStringTag, {value: tag})
} else {
Object.defineProperty(ctor.prototype, 'toString', {
value() {
return '[object ' + tag + ']'
},
})
}
}
defineStringTag(AbortSignal, 'AbortSignal')
defineStringTag(AbortController, 'AbortController')
export {AbortController, AbortSignal}