forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abort_controller.js
143 lines (120 loc) · 3.02 KB
/
abort_controller.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
136
137
138
139
140
141
142
143
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.polyfill.AbortController');
goog.require('shaka.polyfill');
goog.require('shaka.util.FakeEvent');
goog.require('shaka.util.FakeEventTarget');
/**
* @summary A polyfill for systems that do not implement AbortController.
* This is used both with the fetch API for HTTP requests and inside the HLS
* parser.
* @export
* @extends AbortController
*/
shaka.polyfill.AbortController = class {
/**
* Install the polyfill if needed.
* @export
*/
static install() {
if (window.AbortController) {
// Not needed.
return;
}
window.AbortController = shaka.polyfill.AbortController;
window.AbortSignal = shaka.polyfill.AbortController.AbortSignal;
}
/** */
constructor() {
/** @private {!shaka.polyfill.AbortController.AbortSignal} */
this.signal_ = new shaka.polyfill.AbortController.AbortSignal();
}
/**
* @override
* @suppress {const|duplicate} Since the original is defined as "const", we
* need this suppression to override it.
*/
get signal() {
return this.signal_;
}
/**
* @param {*=} reason
* @override
*/
abort(reason) {
this.signal_.doAbort_(reason);
}
};
/**
* @summary A polyfill for AbortSignal, part of the AbortController API.
* @implements {AbortSignal}
*/
shaka.polyfill.AbortController.AbortSignal =
class extends shaka.util.FakeEventTarget {
/** */
constructor() {
super();
/** @private {boolean} */
this.aborted_ = false;
/** @private {*} */
this.reason_ = undefined;
/** @type {?function(!Event)} */
this.onabort = null;
}
/** @override */
get aborted() {
return this.aborted_;
}
/** @return {*} */
get reason() {
return this.reason_;
}
/**
* @param {*} reason
* @private
*/
doAbort_(reason) {
if (this.aborted_) {
return;
}
this.aborted_ = true;
this.reason_ = reason;
if (this.reason_ === undefined) {
// This is equivalent to a native implementation.
this.reason_ = new DOMException(
'signal is aborted without reason', 'AbortError');
}
// According to MDN:
// "Event type - A generic Event with no added properties."
const event = new shaka.util.FakeEvent('abort');
if (this.onabort) {
this.onabort(event);
}
this.dispatchEvent(event);
}
/**
* @param {*=} reason
* @return {!AbortSignal}
*/
static abort(reason) {
const signal = new shaka.polyfill.AbortController.AbortSignal();
signal.doAbort_(reason);
return signal;
}
/**
* @param {number} timeMs
* @return {!AbortSignal}
*/
static timeout(timeMs) {
const signal = new shaka.polyfill.AbortController.AbortSignal();
window.setTimeout(() => {
// This is equivalent to a native implementation.
signal.doAbort_(new DOMException('signal timed out', 'TimeoutError'));
}, timeMs);
return signal;
}
};
shaka.polyfill.register(shaka.polyfill.AbortController.install);