forked from briancavalier/creed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCancelToken.js
308 lines (302 loc) · 7.61 KB
/
CancelToken.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { noop } from './util'
import { Future, resolve, cancel, taskQueue } from './Promise' // deferred
import { subscribe, subscribeOrCall } from './subscribe'
export default class CancelToken {
// https://domenic.github.io/cancelable-promise/#sec-canceltoken-constructor
constructor (executor) {
if (typeof executor !== 'function') {
throw new TypeError('must provide an executor function')
}
this._cancelled = false
this.promise = void 0
this.length = 0
this.scanLow = 0
this.scanHigh = 0
if (executor !== noop) {
executor(reason => this._cancel(reason))
}
}
_cancel (reason) {
if (this._cancelled) return
return this.__cancel(cancel(reason))
}
__cancel (p) {
this._cancelled = true
if (this.length) {
taskQueue.add(this) // needs to be called before __become
}
if (this.promise !== void 0) {
this.promise.__become(p)
} else {
// p.token = this no more necessary
this.promise = p
}
return this._runSync([])
}
_runSync (results) {
// let j = 0;
for (let i = 0; i < this.length; ++i) {
if (this[i] && this[i].promise) { // not already destroyed
this[i].cancel(results, this.promise)
// if (this[i].promise) {
// this[j++] = this[i]
// }
}
// if (j < i)
// this[i] = void 0
// }
}
// this.length = j;
return results
}
run () {
const l = this.length
for (let i = 0; i < l; ++i) {
if (this[i] && this[i].promise) { // not already destroyed
this[i].cancelled(this.promise)
}
this[i] = void 0
}
if (this.length === l) {
this.length = 0
} else {
taskQueue.add(this)
}
}
_subscribe (action) {
if (this.requested) {
action.cancel(null, this)
if (this.length === 0) {
taskQueue.add(this)
}
}
this[this.length++] = action
}
_unsubscribe (action) {
/* eslint complexity:[2,6] */
let i = this._cancelled ? 0 : Math.min(5, this.length)
while (i--) {
// an inplace-filtering algorithm to remove empty actions
// executed at up to 5 steps per unsubscribe
if (this.scanHigh < this.length) {
if (this[this.scanHigh] === action) {
this[this.scanHigh] = action = null
} else if (this[this.scanHigh].promise == null) {
this[this.scanHigh] = null
} else {
this[this.scanLow++] = this[this.scanHigh]
}
this.scanHigh++
} else {
this.length = this.scanLow
this.scanLow = this.scanHigh = 0
}
}
if (action) { // when not found
action.destroy() // at least mark explicitly as empty
}
}
subscribe (fn, token) {
return subscribe(fn, this, new Future(token))
}
subscribeOrCall (fn, c) {
return subscribeOrCall(fn, c, this, new Future())
}
getCancelled () {
if (this.promise === void 0) {
this.promise = new Future(this) // while not settled, provides a reference to token
}
return this.promise
}
// https://domenic.github.io/cancelable-promise/#sec-canceltoken.prototype.requested
get requested () {
return this._cancelled
}
// https://domenic.github.io/cancelable-promise/#sec-canceltoken.source
static source () {
if (this === CancelToken) {
const token = new this(noop)
return {
token,
cancel (r) { return token._cancel(r) }
}
} else {
let cancel
const token = new this(c => { cancel = c })
return {token, cancel}
}
}
static for (thenable) {
return new this(cancel => resolve(thenable).then(cancel)) // finally?
}
static from (cancelTokenlike) {
if (cancelTokenlike == null) return null
/* istanbul ignore else */
if (cancelTokenlike instanceof CancelToken) return cancelTokenlike
else throw new TypeError('not a CancelToken') // TODO
}
static empty () {
return new this(noop) // NeverCancelToken
}
concat (token) {
return new CancelTokenRace([this, token]).get()
}
static race (tokens) {
return new CancelTokenRace(tokens)
}
static pool (tokens) {
return new CancelTokenPool(tokens)
}
static reference (cur) {
return new CancelTokenReference(cur)
}
}
class LiveCancelToken extends CancelToken {
constructor (check) {
super(noop)
this.check = check
}
__cancel (p) {
this.check = null
return super.__cancel(p)
}
get requested () {
return this._cancelled || this.check._testRequested()
}
}
class CancelTokenCombinator { // implements cancel parts of Action
constructor () {
// should be named "token" but is necessary for Action-like usage
this.promise = new LiveCancelToken(this)
}
/* istanbul ignore next */
destroy () {
// possibly called when unsubscribed from a token
}
cancelled (p) {}
// abstract cancel (res, p) {}
// abstract _testRequested () {}
get () {
return this.promise
}
}
class CancelTokenRace extends CancelTokenCombinator {
constructor (tokens) {
super()
this.tokens = []
if (tokens) this.add(...tokens)
}
cancel (results, p) {
/* istanbul ignore if */
if (this.tokens == null) return // when called after been unsubscribed but not destroyed
// assert: !this.promise._cancelled
// for (let t of this.tokens) { // https://phabricator.babeljs.io/T2164
for (let i = 0, t; i < this.tokens.length && (t = this.tokens[i]); i++) {
t._unsubscribe(this)
}
this.tokens = null
const res = this.promise.__cancel(p)
if (results) results.push(...res)
}
_testRequested () {
return this.tokens.some(t => t.requested)
}
add (...tokens) {
if (this.tokens == null) return
// for (let t of tokens) { // https://phabricator.babeljs.io/T2164
for (let i = 0, t; i < tokens.length && (t = tokens[i]); i++) {
t = CancelToken.from(t)
if (t === this.promise || t == null) {
continue
}
if (t.requested) {
this.cancel(null, t.getCancelled())
break
} else {
this.tokens.push(t)
t._subscribe(this)
}
}
}
}
class CancelTokenPool extends CancelTokenCombinator {
constructor (tokens) {
super()
this.tokens = []
this.count = 0
if (tokens) this.add(...tokens)
}
cancel (results, p) {
// assert: !this.promise._cancelled
this.count--
const res = this._check()
if (results && res) results.push(...res)
}
_testRequested () {
return this.tokens.length > 0 && this.tokens.every(t => t.requested)
}
_check () {
if (this.count === 0) {
const reasons = this.tokens.map(t => t.getCancelled().near().value)
this.tokens = null
return this.promise.__cancel(cancel(reasons))
}
}
add (...tokens) {
if (this.tokens == null) return
this.count += tokens.length
// for (let t of tokens) { // https://phabricator.babeljs.io/T2164
for (let i = 0, t; i < tokens.length && (t = tokens[i]); i++) {
t = CancelToken.from(t)
if (t === this.promise || t == null) {
this.count--
continue
}
this.tokens.push(t)
if (t.requested) {
this.count--
} else {
t._subscribe(this)
}
}
if (this.tokens.length > 0) {
this._check()
}
}
}
export class CancelTokenReference extends CancelTokenCombinator {
constructor (cur) {
super()
this.curToken = cur
}
cancel (results, p) {
/* istanbul ignore if */
if (this.curToken == null || this.curToken.getCancelled() !== p) return // when called from an oldToken
// assert: !this.promise._cancelled
const res = this.promise.__cancel(p)
if (results) results.push(...res)
}
_testRequested () {
return this.curToken != null && this.curToken.requested
}
set (newToken) {
/* eslint complexity:[2,7] */
const oldToken = this.curToken
if (oldToken && oldToken.requested) {
throw new ReferenceError('token must not be changed after being cancelled')
}
if (oldToken !== newToken && this.promise !== newToken) {
if (oldToken) {
oldToken._unsubscribe(this)
}
this.curToken = newToken
if (newToken) {
if (newToken.requested) {
this.promise.__cancel(newToken.getCancelled())
} else {
newToken._subscribe(this)
}
}
}
}
}