-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
850 lines (725 loc) · 21.9 KB
/
index.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
// @flow
import fl from 'fantasy-land'
type Cancel = () => void
type Handler<-T> = (x: T) => void
type Handlers<-S, -F> = {|
success: Handler<S>,
failure: Handler<F>,
catch?: Handler<any>,
|}
type LooseHandlers<-S, -F> = Handler<S> | {|
success?: Handler<S>,
failure?: Handler<F>,
catch?: Handler<any>,
|}
type Computation<+S, +F> = (handleSucc: Handler<S>, handleFail: Handler<F>) => ?Cancel
interface Result<+S, +F> {
success?: S,
failure?: F,
}
type ExtractSucc = <S>(t: Task<S, *>) => S
type ChainRecNext<T> = {type: 'next', value: T}
type ChainRecDone<T> = {type: 'done', value: T}
const chainRecNext = <T>(x: T): ChainRecNext<T> => ({type: 'next', value: x})
const chainRecDone = <T>(x: T): ChainRecDone<T> => ({type: 'done', value: x})
const defaultFailureHandler: Handler<mixed> = failure => {
if (failure instanceof Error) {
throw failure
} else {
throw new Error(`Unhandled task failure: ${String(failure)}`)
}
}
const noop = () => {}
type RunHelperBody<S, F> = (s: Handler<S>, f: Handler<F>, c?: Handler<any>) => {
onCancel?: Cancel, // called only when user cancels
onClose?: Cancel, // called when user cancels plus when succ/fail/catch are called
}
const runHelper = <S, F>(body: RunHelperBody<S, F>, handlers: Handlers<S, F>): Cancel => {
let {success, failure, catch: catch_} = handlers
let onCancel = noop
let onClose = noop
let close = () => {
onClose()
// The idea here is to kill links to all stuff that we exposed from runHelper closure.
// We expose via the return value (cancelation function) and by passing callbacks to the body.
// We reason from an assumption that outer code may keep links to values that we exposed forever.
// So we look at all things that referenced in the exposed callbacks and kill them.
success = noop
failure = noop
catch_ = noop
onCancel = noop
close = noop
}
const bodyReturn = body(
x => {
const s = success
close()
s(x)
},
x => {
const f = failure
close()
f(x)
},
catch_ && (x => {
const c = (catch_: any)
close()
c(x)
})
)
onCancel = bodyReturn.onCancel || noop
onClose = bodyReturn.onClose || noop
if (close === noop) {
onCancel = noop
onClose()
}
return () => { onCancel(); close() }
}
function isTask(maybeTask: mixed): boolean {
return maybeTask instanceof Task
}
function isFun(maybeFunction: mixed): boolean {
return typeof maybeFunction === 'function'
}
function isArrayOfTasks(maybeArray: mixed): boolean {
if (!Array.isArray(maybeArray)) {
return false
}
for (let i = 0; i < maybeArray.length; i++) {
if (!(maybeArray[i] instanceof Task)) {
return false
}
}
return true
}
function isThenableOrFn(maybeThenable: mixed): boolean {
return typeof maybeThenable === 'function' ||
(
typeof maybeThenable === 'object' &&
maybeThenable !== null &&
typeof maybeThenable.then === 'function'
)
}
function inv(shouldBeTrue: boolean, errorMessage: string, actualValue: any): void {
if (!shouldBeTrue) {
throw new TypeError(`${errorMessage}. Actual value: ${actualValue}`)
}
}
export default class Task<+S, +F> {
constructor() {
if (this.constructor === Task) {
throw new Error('Don\'t call `new Task()`, call `Task.create()` instead')
}
}
// Creates a task with an arbitrary computation
static create<S, F>(computation: Computation<S, F>): Task<S, F> {
inv(isFun(computation), 'Task.create(f): f is not a function', computation)
return new FromComputation(computation)
}
// Creates a task that resolves with a given value
static of<S>(value: S): Task<S, *> {
return new Of(value)
}
// Creates a task that fails with a given error
static rejected<F>(error: F): Task<*, F> {
return new Rejected(error)
}
// Creates a task that never completes
static empty(): Task<*, *> {
return new Empty()
}
// Given array of tasks creates a task of array
static parallel<F, S, A: Array<Task<S, F>>>(tasks: A): Task<$TupleMap<A, ExtractSucc>, F> {
inv(isArrayOfTasks(tasks), 'Task.parallel(a): a is not an array of tasks', tasks)
return new Parallel(tasks)
}
// Given array of tasks creates a task that completes with the earliest value or error
static race<S, F>(tasks: Array<Task<S, F>>): Task<S, F> {
inv(isArrayOfTasks(tasks), 'Task.race(a): a is not an array of tasks', tasks)
return new Race(tasks)
}
// Transforms a task by applying `fn` to the successful value
static map<S1>(fn: (x: S) => S1, task: Task<S, F>): Task<S1, F> {
inv(isFun(fn), 'Task.map(f, _): f is not a function', fn)
inv(isTask(task), 'Task.map(_, t): t is not a task', task)
return new Map(task, fn)
}
map<S1>(fn: (x: S) => S1): Task<S1, F> {
inv(isFun(fn), 'task.map(f): f is not a function', fn)
return new Map(this, fn)
}
// Transforms a task by applying `fn` to the failure value
static mapRejected<F1>(fn: (x: F) => F1, task: Task<S, F>): Task<S, F1> {
inv(isFun(fn), 'Task.mapRejected(f, _): f is not a function', fn)
inv(isTask(task), 'Task.mapRejected(_, t): t is not a task', task)
return new MapRejected(task, fn)
}
mapRejected<F1>(fn: (x: F) => F1): Task<S, F1> {
inv(isFun(fn), 'task.mapRejected(f): f is not a function', fn)
return new MapRejected(this, fn)
}
// Transforms a task by applying `sf` to the successful value or `ff` to the failure value
static bimap<S1, F1>(ff: (x: F) => F1, fs: (x: S) => S1, task: Task<S, F>): Task<S1, F1> {
inv(isFun(ff), 'Task.bimap(f, _, _): f is not a function', ff)
inv(isFun(fs), 'Task.bimap(_, f, _): f is not a function', fs)
inv(isTask(task), 'Task.bimap(_, _, t): t is not a task', task)
return task.map(fs).mapRejected(ff)
}
bimap<S1, F1>(ff: (x: F) => F1, fs: (x: S) => S1): Task<S1, F1> {
inv(isFun(ff), 'task.bimap(f, _): f is not a function', ff)
inv(isFun(fs), 'task.bimap(_, f): f is not a function', fs)
return this.map(fs).mapRejected(ff)
}
// Transforms a task by applying `fn` to the successful value, where `fn` returns a Task
static chain<S, F, S1, F1>(fn: (x: S) => Task<S1, F1>, task: Task<S, F>): Task<S1, F | F1> {
inv(isFun(fn), 'Task.chain(f, _): f is not a function', fn)
inv(isTask(task), 'Task.chain(_, t): t is not a task', task)
return new Chain(task, fn)
}
chain<S1, F1>(fn: (x: S) => Task<S1, F1>): Task<S1, F | F1> {
inv(isFun(fn), 'task.chain(f): f is not a function', fn)
return new Chain(this, fn)
}
// Transforms a task by applying `fn` to the failure value, where `fn` returns a Task
static orElse<S, F, S1, F1>(fn: (x: F) => Task<S1, F1>, task: Task<S, F>): Task<S | S1, F1> {
inv(isFun(fn), 'Task.orElse(f, _): f is not a function', fn)
inv(isTask(task), 'Task.orElse(_, t): t is not a task', task)
return new OrElse(task, fn)
}
orElse<S1, F1>(fn: (x: F) => Task<S1, F1>): Task<S | S1, F1> {
inv(isFun(fn), 'task.orElse(f): f is not a function', fn)
return new OrElse(this, fn)
}
static recur<S, F, S1, F1>(fn: (x: S | S1) => Task<S1, F1>, task: Task<S, F>): Task<*, F | F1> {
inv(isFun(fn), 'Task.recur(f, _): f is not a function', fn)
inv(isTask(task), 'Task.recur(_, t): t is not a task', task)
return new Recur(task, fn)
}
recur<S1, F1>(fn: (x: S | S1) => Task<S1, F1>): Task<*, F | F1> {
inv(isFun(fn), 'task.recur(f): f is not a function', fn)
return new Recur(this, fn)
}
static chainRec<N, D, F>(
fn: (
next: (x: N) => ChainRecNext<N>,
done: (x: D) => ChainRecDone<D>,
v: N
) => Task<ChainRecNext<N> | ChainRecDone<D>, F>,
initial: N
): Task<D, F> {
inv(isFun(fn), 'Task.chainRec(f, _): f is not a function', fn)
return new ChainRec(fn, initial)
}
// Applies the successful value of task `this` to the successful value of task `otherTask`
static ap<A, B, F1, F2>(tf: Task<(x: A) => B, F1>, tx: Task<A, F2>): Task<B, F1 | F2> {
inv(isTask(tf), 'Task.ap(t, _): t is not a task', tf)
inv(isTask(tx), 'Task.ap(_, t): t is not a task', tx)
return tf.chain(f => tx.map(x => f(x)))
}
ap<F1, S1>(otherTask: Task<(x: S) => S1, F1>): Task<S1, F | F1> {
inv(isTask(otherTask), 'task.ap(t): t is not a task', otherTask)
return otherTask.chain(f => this.map(x => f(x)))
}
// Selects the earlier of the two tasks
static concat<S1, F1, S2, F2>(a: Task<S1, F1>, b: Task<S2, F2>): Task<S1 | S2, F1 | F2> {
inv(isTask(a), 'Task.concat(t, _): t is not a task', a)
inv(isTask(b), 'Task.concat(_, t): t is not a task', b)
return Task.race([a, b])
}
concat<S1, F1>(otherTask: Task<S1, F1>): Task<S | S1, F | F1> {
inv(isTask(otherTask), 'task.concat(t): t is not a task', otherTask)
return Task.race([this, otherTask])
}
static do(generator: () => Generator<Task<any, any>, Task<any, any>, mixed>): Task<mixed, mixed> {
inv(isFun(generator), 'Task.do(f): f is not a function', generator)
return new Do(generator)
}
_run(handlers: Handlers<S, F>): Cancel { // eslint-disable-line
throw new Error('Method run() is not implemented in basic Task class.')
}
_toString(): string {
return '<abstract>'
}
toString() {
return `Task.${this._toString()}`
}
run(h: LooseHandlers<S, F>): Cancel {
const handlers = typeof h === 'function'
? {success: h, failure: defaultFailureHandler}
: {success: h.success || noop, failure: h.failure || defaultFailureHandler, catch: h.catch}
return this._run(handlers)
}
toPromise(options?: {catch: boolean} = {catch: true}): Promise<Result<S, F>> {
return new Promise((suc, err) => {
this.run({
success(x) { suc({success: x}) },
failure(x) { suc({failure: x}) },
catch: options.catch ? err : undefined,
})
})
}
static fromPromise<S>(promise: Promise<S> | () => Promise<S>): Task<S, *> {
inv(isThenableOrFn(promise), 'Task.fromPromise(p): p is not a promise', promise)
return new FromPromise(promise)
}
runAndLog(): void {
this.run({
success(x) { console.log('Success:', x) }, // eslint-disable-line
failure(x) { console.log('Failure:', x) }, // eslint-disable-line
})
}
}
/* We should put Fantasy Land methods to class like this:
*
* class Task {
* 'fantasy-land/of'<S>(value: S): Task<S, *> { return Task.of(value) }
* }
*
* but unfortunately Flow yields "literal properties not yet supported".
*/
function makeFLCompatible(constructor: any) {
constructor.prototype[fl.of] = constructor[fl.of] = constructor.of
constructor.prototype[fl.empty] = constructor[fl.empty] = constructor.empty
constructor.prototype[fl.chainRec] = constructor[fl.chainRec] = constructor.chainRec
constructor.prototype[fl.concat] = constructor.prototype.concat
constructor.prototype[fl.map] = constructor.prototype.map
constructor.prototype[fl.bimap] = constructor.prototype.bimap
constructor.prototype[fl.ap] = constructor.prototype.ap
constructor.prototype[fl.chain] = constructor.prototype.chain
}
makeFLCompatible(Task)
class FromComputation<S, F> extends Task<S, F> {
_computation: Computation<S, F>;
constructor(computation: Computation<S, F>) {
super()
this._computation = computation
}
_run(handlers: Handlers<S, F>) {
const {_computation} = this
return runHelper((s, f, c) => {
let cancel
if (c) {
try {
cancel = _computation(s, f)
} catch (e) { c(e) }
} else {
cancel = _computation(s, f)
}
return {onCancel: cancel || noop}
}, handlers)
}
_toString() {
return 'create(..)'
}
}
class Of<S> extends Task<S, *> {
_value: S;
constructor(value: S) {
super()
this._value = value
}
_run(handlers: Handlers<S, *>): Cancel {
const {success} = handlers
success(this._value)
return noop
}
_toString() {
return 'of(..)'
}
}
class Rejected<F> extends Task<*, F> {
_error: F;
constructor(error: F) {
super()
this._error = error
}
_run(handlers: Handlers<*, F>): Cancel {
const {failure} = handlers
failure(this._error)
return noop
}
_toString() {
return 'rejected(..)'
}
}
class Empty extends Task<*, *> {
run(): Cancel {
return noop
}
_toString() {
return `empty()`
}
}
class Parallel<S, F> extends Task<S[], F> {
_tasks: Array<Task<S, F>>;
constructor(tasks: Array<Task<S, F>>) {
super()
this._tasks = tasks
}
_run(handlers: Handlers<S[], F>): Cancel {
return runHelper((s, f, c) => {
const length = this._tasks.length
const values: Array<?S> = Array(length)
let completedCount = 0
const runTask = (task, index) => task.run({
success(x) {
values[index] = x
completedCount++
if (completedCount === length) {
s((values: any))
}
},
failure: f,
catch: c,
})
const cancels = this._tasks.map(runTask)
return {onClose() { cancels.forEach(cancel => cancel()) }}
}, handlers)
}
_toString() {
return `parallel([${this._tasks.map(x => x._toString()).join(', ')}])`
}
}
class Race<S, F> extends Task<S, F> {
_tasks: Array<Task<S, F>>;
constructor(tasks: Array<Task<S, F>>) {
super()
this._tasks = tasks
}
_run(handlers: Handlers<S, F>): Cancel {
return runHelper((success, failure, _catch) => {
const handlers = {success, failure, catch: _catch}
const cancels = this._tasks.map(task => task.run(handlers))
return {onClose() { cancels.forEach(cancel => cancel()) }}
}, handlers)
}
_toString() {
return `race([${this._tasks.map(x => x._toString()).join(', ')}])`
}
}
class Map<SIn, SOut, F> extends Task<SOut, F> {
_task: Task<SIn, F>;
_fn: (x: SIn) => SOut;
constructor(task: Task<SIn, F>, fn: (x: SIn) => SOut) {
super()
this._task = task
this._fn = fn
}
_run(handlers: Handlers<SOut, F>): Cancel {
const {_fn} = this
const {success, failure, catch: catch_} = handlers
return this._task.run({
success(x) {
let value
if (catch_) {
try {
value = _fn(x)
} catch (e) {
catch_(e)
return
}
} else {
value = _fn(x)
}
success(value)
},
failure,
catch: catch_,
})
}
_toString() {
return `${this._task._toString()}.map(..)`
}
}
class MapRejected<S, FIn, FOut> extends Task<S, FOut> {
_task: Task<S, FIn>;
_fn: (x: FIn) => FOut;
constructor(task: Task<S, FIn>, fn: (x: FIn) => FOut) {
super()
this._task = task
this._fn = fn
}
_run(handlers: Handlers<S, FOut>): Cancel {
const {_fn} = this
const {success, failure, catch: catch_} = handlers
return this._task.run({
success,
failure(x) {
let value
if (catch_) {
try {
value = _fn(x)
} catch (e) {
catch_(e)
return
}
} else {
value = _fn(x)
}
failure(value)
},
catch: catch_,
})
}
_toString() {
return `${this._task._toString()}.mapRejected(..)`
}
}
class Chain<SIn, SOut, F, F1> extends Task<SOut, F1 | F> {
_task: Task<SIn, F>;
_fn: (x: SIn) => Task<SOut, F1>;
constructor(task: Task<SIn, F>, fn: (x: SIn) => Task<SOut, F1>) {
super()
this._task = task
this._fn = fn
}
_run(handlers: Handlers<SOut, F | F1>): Cancel {
const {_fn, _task} = this
return runHelper((success, failure, catch_) => {
let cancel = noop
let spawnedHasBeenRun = false
const cancel1 = _task.run({ // #1
success(x) {
let spawned
if (catch_) {
try {
spawned = _fn(x)
} catch (e) { catch_(e) }
} else {
spawned = _fn(x)
}
if (spawned) {
cancel = spawned.run({success, failure, catch: catch_}) // #2
spawnedHasBeenRun = true
}
},
failure,
catch: catch_,
})
if (!spawnedHasBeenRun) { // #2 run() may return before #1 run() returns
cancel = cancel1
}
return {onCancel() { cancel() }}
}, handlers)
}
_toString() {
return `${this._task._toString()}.chain(..)`
}
}
class OrElse<S, S1, FIn, FOut> extends Task<S | S1, FOut> {
_task: Task<S, FIn>;
_fn: (x: FIn) => Task<S1, FOut>;
constructor(task: Task<S, FIn>, fn: (x: FIn) => Task<S1, FOut>) {
super()
this._task = task
this._fn = fn
}
_run(handlers: Handlers<S | S1, FOut>): Cancel {
const {_fn, _task} = this
return runHelper((success, failure, catch_) => {
let cancel = noop
let spawnedHasBeenRun = false
const cancel1 = _task.run({ // #1
success,
failure(x) {
let spawned
if (catch_) {
try {
spawned = _fn(x)
} catch (e) { catch_(e) }
} else {
spawned = _fn(x)
}
if (spawned) {
cancel = spawned.run({success, failure, catch: catch_}) // #2
spawnedHasBeenRun = true
}
},
catch: catch_,
})
if (!spawnedHasBeenRun) { // #2 run() may return before #1 run() returns
cancel = cancel1
}
return {onCancel() { cancel() }}
}, handlers)
}
_toString() {
return `${this._task._toString()}.orElse(..)`
}
}
class Recur<S1, F1, S, F> extends Task<*, F | F1> {
_task: Task<S, F>;
_fn: (x: S | S1) => Task<S1, F1>;
constructor(task: Task<S, F>, fn: (x: S | S1) => Task<S1, F1>) {
super()
this._task = task
this._fn = fn
}
_run(handlers: Handlers<S1, F | F1>): Cancel {
const {_fn, _task} = this
return runHelper((_, failure, catch_) => {
let x
let haveNewX = false
let inLoop = false
let spawnedHasBeenRun = false
let sharedCancel = noop
const success = _x => {
haveNewX = true
x = _x
if (inLoop) {
return
}
inLoop = true
while(haveNewX) {
haveNewX = false
let spawned
if (catch_) {
try {
spawned = _fn(x)
} catch (e) { catch_(e) }
} else {
spawned = _fn(x)
}
if (spawned) {
sharedCancel = spawned.run({success, failure, catch: catch_}) // #2
spawnedHasBeenRun = true
}
}
inLoop = false
}
const cancel = _task.run({success, failure, catch: catch_}) // #1
if (!spawnedHasBeenRun) { // #2 run() may return before #1 run() returns
sharedCancel = cancel
}
return {onCancel() { sharedCancel() }}
}, handlers)
}
_toString() {
return `${this._task._toString()}.recur(..)`
}
}
class ChainRec<N, D, F> extends Task<D, F> {
_fn: (
next: (x: N) => ChainRecNext<N>,
done: (x: D) => ChainRecDone<D>,
v: N
) => Task<ChainRecNext<N> | ChainRecDone<D>, F>;
_initial: N;
constructor(
fn: (
next: (x: N) => ChainRecNext<N>,
done: (x: D) => ChainRecDone<D>,
v: N
) => Task<ChainRecNext<N> | ChainRecDone<D>, F>,
initial: N
) {
super()
this._fn = fn
this._initial = initial
}
_run(handlers: Handlers<D, F>): Cancel {
const {_fn, _initial} = this
return runHelper((success, failure, catch_) => {
let newNext = null
let haveNewNext = false
let inLoop = false
let sharedCancel = noop
const step = (result) => {
if (result.type === 'done') {
success(result.value)
return
}
newNext = result.value
haveNewNext = true
if (inLoop) {
return
}
inLoop = true
while(haveNewNext) {
haveNewNext = false
let spawned
if (catch_) {
try {
spawned = _fn(chainRecNext, chainRecDone, newNext)
} catch (e) { catch_(e) }
} else {
spawned = _fn(chainRecNext, chainRecDone, newNext)
}
if (spawned) {
sharedCancel = spawned.run({success: step, failure, catch: catch_})
}
}
inLoop = false
}
step(chainRecNext(_initial))
return {onCancel() { sharedCancel() }}
}, handlers)
}
_toString() {
return `chainRec(..)`
}
}
class Do extends Task<mixed, mixed> {
_generator: () => Generator<Task<any, any>, Task<any, any>, mixed>;
constructor(generator: () => Generator<Task<any, any>, Task<any, any>, mixed>) {
super()
this._generator = generator
}
_run(handlers: Handlers<mixed, mixed>): Cancel {
const {_generator} = this
return runHelper((success, failure, catch_) => {
const iterator = _generator()
let x
let haveNewX = false
let inLoop = false
let sharedCancel = noop
const step = _x => {
haveNewX = true
x = _x
if (inLoop) {
return
}
inLoop = true
while(haveNewX) {
haveNewX = false
let iteratorNext
if (catch_) {
try {
iteratorNext = iterator.next(x)
} catch (e) { catch_(e) }
} else {
iteratorNext = iterator.next(x)
}
if (iteratorNext) {
const {value: spawned, done} = iteratorNext
sharedCancel = (spawned: any).run({success: done ? success : step, failure, catch: catch_})
}
}
inLoop = false
}
step(undefined)
return {onCancel() { sharedCancel() }}
}, handlers)
}
_toString() {
return `do(..)`
}
}
class FromPromise<S> extends Task<S, *> {
_promise: Promise<S> | () => Promise<S>;
constructor(promise: Promise<S> | () => Promise<S>) {
super()
this._promise = promise
}
_run(handlers: Handlers<S, *>) {
const {_promise} = this
const promise = typeof _promise === 'function' ? _promise() : _promise
return runHelper((success, _, catch_) => {
promise.then(success, catch_)
return {}
}, handlers)
}
_toString() {
return 'fromPromise(..)'
}
}