-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
pledge.js
292 lines (267 loc) · 5.97 KB
/
pledge.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
/**
* @experimental Use of this module is not encouraged!
* This is just an experiment.
* @todo remove `c8 ignore` line once this is moved to "non-experimental"
*/
import * as queue from './queue.js'
import * as object from './object.js'
/* c8 ignore start */
/**
* @type {queue.Queue<queue.QueueValue<()=>void>>}
*/
const ctxFs = queue.create()
/**
* @param {() => void} f
*/
const runInGlobalContext = f => {
const isEmpty = queue.isEmpty(ctxFs)
queue.enqueue(ctxFs, new queue.QueueValue(f))
if (isEmpty) {
while (!queue.isEmpty(ctxFs)) {
/** @type {queue.QueueValue<()=>{}>} */ (ctxFs.start).v()
queue.dequeue(ctxFs)
}
}
}
/**
* @template V
* @typedef {V | PledgeInstance<V>} Pledge
*/
/**
* @template {any} Val
* @template {any} [CancelReason=Error]
*/
export class PledgeInstance {
constructor () {
/**
* @type {Val | CancelReason | null}
*/
this._v = null
this.isResolved = false
/**
* @type {Array<function(Val):void> | null}
*/
this._whenResolved = []
/**
* @type {Array<function(CancelReason):void> | null}
*/
this._whenCanceled = []
}
get isDone () {
return this._whenResolved === null
}
get isCanceled () {
return !this.isResolved && this._whenResolved === null
}
/**
* @param {Val} v
*/
resolve (v) {
const whenResolved = this._whenResolved
if (whenResolved === null) return
this._v = v
this.isResolved = true
this._whenResolved = null
this._whenCanceled = null
for (let i = 0; i < whenResolved.length; i++) {
whenResolved[i](v)
}
}
/**
* @param {CancelReason} reason
*/
cancel (reason) {
const whenCanceled = this._whenCanceled
if (whenCanceled === null) return
this._v = reason
this._whenResolved = null
this._whenCanceled = null
for (let i = 0; i < whenCanceled.length; i++) {
whenCanceled[i](reason)
}
}
/**
* @template R
* @param {function(Val):Pledge<R>} f
* @return {PledgeInstance<R>}
*/
map (f) {
/**
* @type {PledgeInstance<R>}
*/
const p = new PledgeInstance()
this.whenResolved(v => {
const result = f(v)
if (result instanceof PledgeInstance) {
if (result._whenResolved === null) {
result.resolve(/** @type {R} */ (result._v))
} else {
result._whenResolved.push(p.resolve.bind(p))
}
} else {
p.resolve(result)
}
})
return p
}
/**
* @param {function(Val):void} f
*/
whenResolved (f) {
if (this.isResolved) {
f(/** @type {Val} */ (this._v))
} else {
this._whenResolved?.push(f)
}
}
/**
* @param {(reason: CancelReason) => void} f
*/
whenCanceled (f) {
if (this.isCanceled) {
f(/** @type {CancelReason} */ (this._v))
} else {
this._whenCanceled?.push(f)
}
}
/**
* @return {Promise<Val>}
*/
promise () {
return new Promise((resolve, reject) => {
this.whenResolved(resolve)
this.whenCanceled(reject)
})
}
}
/**
* @template T
* @return {PledgeInstance<T>}
*/
export const create = () => new PledgeInstance()
/**
* @typedef {Array<Pledge<unknown>> | Object<string,Pledge<unknown>>} PledgeMap
*/
/**
* @template {Pledge<unknown> | PledgeMap} P
* @typedef {P extends PledgeMap ? { [K in keyof P]: P[K] extends Pledge<infer V> ? V : P[K]} : (P extends Pledge<infer V> ? V : never)} Resolved<P>
*/
/**
* @todo Create a "resolveHelper" that will simplify creating indxeddbv2 functions. Double arguments
* are not necessary.
*
* @template V
* @template {Array<Pledge<unknown>>} DEPS
* @param {(p: PledgeInstance<V>, ...deps: Resolved<DEPS>) => void} init
* @param {DEPS} deps
* @return {PledgeInstance<V>}
*/
export const createWithDependencies = (init, ...deps) => {
/**
* @type {PledgeInstance<V>}
*/
const p = new PledgeInstance()
// @ts-ignore @todo remove this
all(deps).whenResolved(ds => init(p, ...ds))
return p
}
/**
* @template R
* @param {Pledge<R>} p
* @param {function(R):void} f
*/
export const whenResolved = (p, f) => {
if (p instanceof PledgeInstance) {
return p.whenResolved(f)
}
return f(p)
}
/**
* @template {Pledge<unknown>} P
* @param {P} p
* @param {P extends PledgeInstance<unknown, infer CancelReason> ? function(CancelReason):void : function(any):void} f
*/
export const whenCanceled = (p, f) => {
if (p instanceof PledgeInstance) {
p.whenCanceled(f)
}
}
/**
* @template P
* @template Q
* @param {Pledge<P>} p
* @param {(r: P) => Q} f
* @return {Pledge<Q>}
*/
export const map = (p, f) => {
if (p instanceof PledgeInstance) {
return p.map(f)
}
return f(p)
}
/**
* @template {PledgeMap} PS
* @param {PS} ps
* @return {PledgeInstance<Resolved<PS>>}
*/
export const all = ps => {
/**
* @type {any}
*/
const pall = create()
/**
* @type {any}
*/
const result = ps instanceof Array ? new Array(ps.length) : {}
let waitingPs = ps instanceof Array ? ps.length : object.size(ps)
for (const key in ps) {
const p = ps[key]
whenResolved(p, r => {
result[key] = r
if (--waitingPs === 0) {
// @ts-ignore
pall.resolve(result)
}
})
}
return pall
}
/**
* @template Result
* @template {any} YieldResults
* @param {() => Generator<Pledge<YieldResults> | PledgeInstance<YieldResults,any>, Result, any>} f
* @return {PledgeInstance<Result>}
*/
export const coroutine = f => {
const p = create()
const gen = f()
/**
* @param {any} [yv]
*/
const handleGen = (yv) => {
const res = gen.next(yv)
if (res.done) {
p.resolve(res.value)
return
}
// @ts-ignore
whenCanceled(res.value, (reason) => {
gen.throw(reason)
})
runInGlobalContext(() =>
whenResolved(res.value, handleGen)
)
}
handleGen()
return p
}
/**
* @param {number} timeout
* @return {PledgeInstance<undefined>}
*/
export const wait = timeout => {
const p = create()
setTimeout(p.resolve.bind(p), timeout)
return p
}
/* c8 ignore end */