t-tasks 0.9.6 → 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2049 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- (function (Just) {
6
- /**
7
- * Standalone Just value extractor
8
- *
9
- * Userful for passing as a function to collection transformers
10
- *
11
- * @template R underlying value type
12
- * @param just Just instance
13
- * @returns underlying value
14
- */
15
- function just(just) {
16
- return just.just;
17
- }
18
-
19
- Just.just = just;
20
- })(exports.Just || (exports.Just = {}));
21
-
22
- (function (Maybe) {
23
- /**
24
- * Non-empty monad constructor
25
- *
26
- * @template R underlying type
27
- * @param value underlying value
28
- * @returns 'just value'
29
- */
30
- function just(value) {
31
- return new JustClass(value);
32
- }
33
-
34
- Maybe.just = just;
35
- /**
36
- * Empty monad constructor
37
- *
38
- * @returns 'nothing'
39
- */
40
-
41
- function nothing() {
42
- return staticNothing;
43
- }
44
-
45
- Maybe.nothing = nothing;
46
-
47
- function fromOptional(value) {
48
- return typeof value !== 'undefined' ? Maybe.just(value) : Maybe.nothing();
49
- }
50
-
51
- Maybe.fromOptional = fromOptional;
52
-
53
- function fromNullable(value) {
54
- return Maybe.fromOptional(value).chain(function (v) {
55
- return v !== null ? Maybe.just(v) : Maybe.nothing();
56
- });
57
- }
58
-
59
- Maybe.fromNullable = fromNullable;
60
- /**
61
- * Standalone type guard for 'just'
62
- *
63
- * Userful for passing as a predicate to collection transformers
64
- *
65
- * @template R underlying value type
66
- * @param maybe Maybe instance
67
- * @returns 'true' in case wrapped value exists (and resolves argument type to be 'just')
68
- */
69
-
70
- function isJust(maybe) {
71
- return maybe.isJust();
72
- }
73
-
74
- Maybe.isJust = isJust;
75
- /**
76
- * Standalone type guard for 'nothing'
77
- *
78
- * Userful for passing as a predicate to collection transformers
79
- *
80
- * @template R underlying value type
81
- * @param maybe Maybe instance
82
- * @returns 'true' in case wrapped value is 'nothing' (and resolves argument type to be 'nothing')
83
- */
84
-
85
- function isNothing(maybe) {
86
- return maybe.isNothing();
87
- }
88
-
89
- Maybe.isNothing = isNothing;
90
- /**
91
- * Standalone list predicate
92
- *
93
- * @param maybes a list of Maybe
94
- * @returns true in case every list element is Just
95
- */
96
-
97
- function everyJust(maybes) {
98
- return maybes.every(Maybe.isJust);
99
- }
100
-
101
- Maybe.everyJust = everyJust;
102
- /**
103
- * Standalone list predicate
104
- *
105
- * @param maybes a list of Maybe
106
- * @returns true in case al east one list element is Just
107
- */
108
-
109
- function someJust(maybes) {
110
- return maybes.some(Maybe.isJust);
111
- }
112
-
113
- Maybe.someJust = someJust;
114
- /**
115
- * Standalone list predicate
116
- *
117
- * @param maybes a list of Maybe
118
- * @returns true in case every list element is Nothing
119
- */
120
-
121
- function everyNothing(maybes) {
122
- return maybes.every(Maybe.isNothing);
123
- }
124
-
125
- Maybe.everyNothing = everyNothing;
126
- /**
127
- * Standalone list predicate
128
- *
129
- * @param maybes a list of Maybe
130
- * @returns true in case at least one list element is Nothing
131
- */
132
-
133
- function someNothing(maybes) {
134
- return maybes.some(Maybe.isNothing);
135
- }
136
-
137
- Maybe.someNothing = someNothing;
138
- })(exports.Maybe || (exports.Maybe = {})); /// --------------------------------------------------------------------------------------
139
- /// Private section
140
- /// --------------------------------------------------------------------------------------
141
-
142
-
143
- var JustClass = /*#__PURE__*/function () {
144
- function JustClass(just) {
145
- this.just = just;
146
- }
147
-
148
- var _proto = JustClass.prototype;
149
-
150
- _proto.tap = function tap(op) {
151
- op(this.just);
152
- return this;
153
- };
154
-
155
- _proto.orTap = function orTap() {
156
- return this;
157
- };
158
-
159
- _proto.map = function map(op) {
160
- return exports.Maybe.just(op(this.just));
161
- };
162
-
163
- _proto.orMap = function orMap() {
164
- return this;
165
- };
166
-
167
- _proto.chain = function chain(op) {
168
- return op(this.just);
169
- };
170
-
171
- _proto.orChain = function orChain() {
172
- return this;
173
- };
174
-
175
- _proto.isJust = function isJust() {
176
- return true;
177
- };
178
-
179
- _proto.isNothing = function isNothing() {
180
- return false;
181
- };
182
-
183
- _proto.matchTap = function matchTap(op) {
184
- return this.tap(op.just);
185
- };
186
-
187
- _proto.matchMap = function matchMap(op) {
188
- return this.map(op.just);
189
- };
190
-
191
- _proto.matchChain = function matchChain(op) {
192
- return this.chain(op.just);
193
- };
194
-
195
- return JustClass;
196
- }();
197
-
198
- var NothingClass = /*#__PURE__*/function () {
199
- function NothingClass() {}
200
-
201
- var _proto2 = NothingClass.prototype;
202
-
203
- _proto2.tap = function tap() {
204
- return this;
205
- };
206
-
207
- _proto2.orTap = function orTap(op) {
208
- op();
209
- return this;
210
- };
211
-
212
- _proto2.map = function map() {
213
- return this;
214
- };
215
-
216
- _proto2.orMap = function orMap(op) {
217
- return exports.Maybe.just(op());
218
- };
219
-
220
- _proto2.chain = function chain() {
221
- return this;
222
- };
223
-
224
- _proto2.orChain = function orChain(op) {
225
- return op();
226
- };
227
-
228
- _proto2.isJust = function isJust() {
229
- return false;
230
- };
231
-
232
- _proto2.isNothing = function isNothing() {
233
- return true;
234
- };
235
-
236
- _proto2.matchTap = function matchTap(op) {
237
- return this.orTap(op.nothing);
238
- };
239
-
240
- _proto2.matchMap = function matchMap(op) {
241
- return this.orMap(op.nothing);
242
- };
243
-
244
- _proto2.matchChain = function matchChain(op) {
245
- return this.orChain(op.nothing);
246
- };
247
-
248
- return NothingClass;
249
- }();
250
-
251
- var staticNothing = /*#__PURE__*/new NothingClass();
252
-
253
- (function (Right) {
254
- /**
255
- * Standalone Right value extractor
256
- *
257
- * Userful for passing as a function to collection transformers
258
- *
259
- * @template R underlying value type
260
- * @template L underlying error type
261
- * @param right Right instance
262
- * @returns underlying value
263
- */
264
- function right(right) {
265
- return right.right;
266
- }
267
-
268
- Right.right = right;
269
- })(exports.Right || (exports.Right = {}));
270
-
271
- (function (Left) {
272
- /**
273
- * Standalone Left error extractor
274
- *
275
- * Userful for passing as a function to collection transformers
276
- *
277
- * @template R underlying value type
278
- * @template L underlying error type
279
- * @param left Left instance
280
- * @returns underlying error
281
- */
282
- function left(left) {
283
- return left.left;
284
- }
285
-
286
- Left.left = left;
287
- })(exports.Left || (exports.Left = {}));
288
-
289
- (function (Either) {
290
- /**
291
- * Right monad constructor
292
- *
293
- * @template R underlying value type
294
- * @param value underlying value
295
- * @returns 'right value'
296
- */
297
- function right(value) {
298
- return new RightClass(value);
299
- }
300
-
301
- Either.right = right;
302
- /**
303
- * Left monad constructor
304
- *
305
- * @template L underlying error type
306
- * @param error underlying error
307
- * @returns 'left error'
308
- */
309
-
310
- function left(error) {
311
- return new LeftClass(error);
312
- }
313
-
314
- Either.left = left;
315
-
316
- function fromOptional(value, error) {
317
- return typeof value !== 'undefined' ? Either.right(value) : Either.left(error);
318
- }
319
-
320
- Either.fromOptional = fromOptional;
321
-
322
- function fromNullable(value, error) {
323
- return Either.fromOptional(value, error).chain(function (v) {
324
- return v !== null ? Either.right(value) : Either.left(error);
325
- });
326
- }
327
-
328
- Either.fromNullable = fromNullable;
329
- /**
330
- * Standalone type guard for 'right'
331
- *
332
- * Userful for passing as a predicate to collection transformers
333
- *
334
- * @template R underlying value type
335
- * @param either Either instance
336
- * @returns 'true' in case argument is 'right value' (and resolves type to be 'right')
337
- */
338
-
339
- function isRight(either) {
340
- return either.isRight();
341
- }
342
-
343
- Either.isRight = isRight;
344
- /**
345
- * Standalone type guard for 'left'
346
- *
347
- * Userful for passing as a predicate to collection transformers
348
- *
349
- * @template R underlying value type
350
- * @param either Either instance
351
- * @returns 'true' in case argument is 'left error' (and resolves type to be 'left')
352
- */
353
-
354
- function isLeft(either) {
355
- return either.isLeft();
356
- }
357
-
358
- Either.isLeft = isLeft;
359
- /**
360
- * Standalone list predicate
361
- *
362
- * @param eithers a list of Either
363
- * @returns true in case every list element is Right
364
- */
365
-
366
- function everyRight(eithers) {
367
- return eithers.every(Either.isRight);
368
- }
369
-
370
- Either.everyRight = everyRight;
371
- /**
372
- * Standalone list predicate
373
- *
374
- * @param eithers a list of Either
375
- * @returns true in case at least one list element is Right
376
- */
377
-
378
- function someRight(eithers) {
379
- return eithers.some(Either.isRight);
380
- }
381
-
382
- Either.someRight = someRight;
383
- /**
384
- * Standalone list predicate
385
- *
386
- * @param eithers a list of Either
387
- * @returns true in case every list element is Left
388
- */
389
-
390
- function everyLeft(eithers) {
391
- return eithers.every(Either.isLeft);
392
- }
393
-
394
- Either.everyLeft = everyLeft;
395
- /**
396
- * Standalone list predicate
397
- *
398
- * @param eithers a list of Either
399
- * @returns true in case at least one list element is Left
400
- */
401
-
402
- function someLeft(eithers) {
403
- return eithers.some(Either.isLeft);
404
- }
405
-
406
- Either.someLeft = someLeft;
407
- })(exports.Either || (exports.Either = {})); /// --------------------------------------------------------------------------------------
408
- /// Private section
409
- /// --------------------------------------------------------------------------------------
410
-
411
-
412
- var RightClass = /*#__PURE__*/function () {
413
- function RightClass(right) {
414
- this.right = right;
415
- }
416
-
417
- var _proto = RightClass.prototype;
418
-
419
- _proto.tap = function tap(op) {
420
- op(this.right);
421
- return this;
422
- };
423
-
424
- _proto.orTap = function orTap() {
425
- return this;
426
- };
427
-
428
- _proto.map = function map(op) {
429
- return exports.Either.right(op(this.right));
430
- };
431
-
432
- _proto.orMap = function orMap() {
433
- return this;
434
- };
435
-
436
- _proto.chain = function chain(op) {
437
- return op(this.right);
438
- };
439
-
440
- _proto.orChain = function orChain() {
441
- return this;
442
- };
443
-
444
- _proto.isRight = function isRight() {
445
- return true;
446
- };
447
-
448
- _proto.isLeft = function isLeft() {
449
- return false;
450
- };
451
-
452
- _proto.matchTap = function matchTap(op) {
453
- return this.tap(op.right);
454
- };
455
-
456
- _proto.matchMap = function matchMap(op) {
457
- return this.map(op.right);
458
- };
459
-
460
- _proto.matchChain = function matchChain(op) {
461
- return this.chain(op.right);
462
- };
463
-
464
- return RightClass;
465
- }();
466
-
467
- var LeftClass = /*#__PURE__*/function () {
468
- function LeftClass(left) {
469
- this.left = left;
470
- }
471
-
472
- var _proto2 = LeftClass.prototype;
473
-
474
- _proto2.tap = function tap() {
475
- return this;
476
- };
477
-
478
- _proto2.orTap = function orTap(op) {
479
- op(this.left);
480
- return this;
481
- };
482
-
483
- _proto2.map = function map() {
484
- return this;
485
- };
486
-
487
- _proto2.orMap = function orMap(op) {
488
- return exports.Either.right(op(this.left));
489
- };
490
-
491
- _proto2.chain = function chain() {
492
- return this;
493
- };
494
-
495
- _proto2.orChain = function orChain(op) {
496
- return op(this.left);
497
- };
498
-
499
- _proto2.isRight = function isRight() {
500
- return false;
501
- };
502
-
503
- _proto2.isLeft = function isLeft() {
504
- return true;
505
- };
506
-
507
- _proto2.matchTap = function matchTap(op) {
508
- return this.orTap(op.left);
509
- };
510
-
511
- _proto2.matchMap = function matchMap(op) {
512
- return this.orMap(op.left);
513
- };
514
-
515
- _proto2.matchChain = function matchChain(op) {
516
- return this.orChain(op.left);
517
- };
518
-
519
- return LeftClass;
520
- }();
521
-
522
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
523
- try {
524
- var info = gen[key](arg);
525
- var value = info.value;
526
- } catch (error) {
527
- reject(error);
528
- return;
529
- }
530
-
531
- if (info.done) {
532
- resolve(value);
533
- } else {
534
- Promise.resolve(value).then(_next, _throw);
535
- }
536
- }
537
-
538
- function _asyncToGenerator(fn) {
539
- return function () {
540
- var self = this,
541
- args = arguments;
542
- return new Promise(function (resolve, reject) {
543
- var gen = fn.apply(self, args);
544
-
545
- function _next(value) {
546
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
547
- }
548
-
549
- function _throw(err) {
550
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
551
- }
552
-
553
- _next(undefined);
554
- });
555
- };
556
- }
557
-
558
- function createCommonjsModule(fn, module) {
559
- return module = { exports: {} }, fn(module, module.exports), module.exports;
560
- }
561
-
562
- var runtime_1 = createCommonjsModule(function (module) {
563
- /**
564
- * Copyright (c) 2014-present, Facebook, Inc.
565
- *
566
- * This source code is licensed under the MIT license found in the
567
- * LICENSE file in the root directory of this source tree.
568
- */
569
-
570
- var runtime = (function (exports) {
571
-
572
- var Op = Object.prototype;
573
- var hasOwn = Op.hasOwnProperty;
574
- var undefined$1; // More compressible than void 0.
575
- var $Symbol = typeof Symbol === "function" ? Symbol : {};
576
- var iteratorSymbol = $Symbol.iterator || "@@iterator";
577
- var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
578
- var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
579
-
580
- function define(obj, key, value) {
581
- Object.defineProperty(obj, key, {
582
- value: value,
583
- enumerable: true,
584
- configurable: true,
585
- writable: true
586
- });
587
- return obj[key];
588
- }
589
- try {
590
- // IE 8 has a broken Object.defineProperty that only works on DOM objects.
591
- define({}, "");
592
- } catch (err) {
593
- define = function(obj, key, value) {
594
- return obj[key] = value;
595
- };
596
- }
597
-
598
- function wrap(innerFn, outerFn, self, tryLocsList) {
599
- // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
600
- var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
601
- var generator = Object.create(protoGenerator.prototype);
602
- var context = new Context(tryLocsList || []);
603
-
604
- // The ._invoke method unifies the implementations of the .next,
605
- // .throw, and .return methods.
606
- generator._invoke = makeInvokeMethod(innerFn, self, context);
607
-
608
- return generator;
609
- }
610
- exports.wrap = wrap;
611
-
612
- // Try/catch helper to minimize deoptimizations. Returns a completion
613
- // record like context.tryEntries[i].completion. This interface could
614
- // have been (and was previously) designed to take a closure to be
615
- // invoked without arguments, but in all the cases we care about we
616
- // already have an existing method we want to call, so there's no need
617
- // to create a new function object. We can even get away with assuming
618
- // the method takes exactly one argument, since that happens to be true
619
- // in every case, so we don't have to touch the arguments object. The
620
- // only additional allocation required is the completion record, which
621
- // has a stable shape and so hopefully should be cheap to allocate.
622
- function tryCatch(fn, obj, arg) {
623
- try {
624
- return { type: "normal", arg: fn.call(obj, arg) };
625
- } catch (err) {
626
- return { type: "throw", arg: err };
627
- }
628
- }
629
-
630
- var GenStateSuspendedStart = "suspendedStart";
631
- var GenStateSuspendedYield = "suspendedYield";
632
- var GenStateExecuting = "executing";
633
- var GenStateCompleted = "completed";
634
-
635
- // Returning this object from the innerFn has the same effect as
636
- // breaking out of the dispatch switch statement.
637
- var ContinueSentinel = {};
638
-
639
- // Dummy constructor functions that we use as the .constructor and
640
- // .constructor.prototype properties for functions that return Generator
641
- // objects. For full spec compliance, you may wish to configure your
642
- // minifier not to mangle the names of these two functions.
643
- function Generator() {}
644
- function GeneratorFunction() {}
645
- function GeneratorFunctionPrototype() {}
646
-
647
- // This is a polyfill for %IteratorPrototype% for environments that
648
- // don't natively support it.
649
- var IteratorPrototype = {};
650
- IteratorPrototype[iteratorSymbol] = function () {
651
- return this;
652
- };
653
-
654
- var getProto = Object.getPrototypeOf;
655
- var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
656
- if (NativeIteratorPrototype &&
657
- NativeIteratorPrototype !== Op &&
658
- hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) {
659
- // This environment has a native %IteratorPrototype%; use it instead
660
- // of the polyfill.
661
- IteratorPrototype = NativeIteratorPrototype;
662
- }
663
-
664
- var Gp = GeneratorFunctionPrototype.prototype =
665
- Generator.prototype = Object.create(IteratorPrototype);
666
- GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
667
- GeneratorFunctionPrototype.constructor = GeneratorFunction;
668
- GeneratorFunction.displayName = define(
669
- GeneratorFunctionPrototype,
670
- toStringTagSymbol,
671
- "GeneratorFunction"
672
- );
673
-
674
- // Helper for defining the .next, .throw, and .return methods of the
675
- // Iterator interface in terms of a single ._invoke method.
676
- function defineIteratorMethods(prototype) {
677
- ["next", "throw", "return"].forEach(function(method) {
678
- define(prototype, method, function(arg) {
679
- return this._invoke(method, arg);
680
- });
681
- });
682
- }
683
-
684
- exports.isGeneratorFunction = function(genFun) {
685
- var ctor = typeof genFun === "function" && genFun.constructor;
686
- return ctor
687
- ? ctor === GeneratorFunction ||
688
- // For the native GeneratorFunction constructor, the best we can
689
- // do is to check its .name property.
690
- (ctor.displayName || ctor.name) === "GeneratorFunction"
691
- : false;
692
- };
693
-
694
- exports.mark = function(genFun) {
695
- if (Object.setPrototypeOf) {
696
- Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
697
- } else {
698
- genFun.__proto__ = GeneratorFunctionPrototype;
699
- define(genFun, toStringTagSymbol, "GeneratorFunction");
700
- }
701
- genFun.prototype = Object.create(Gp);
702
- return genFun;
703
- };
704
-
705
- // Within the body of any async function, `await x` is transformed to
706
- // `yield regeneratorRuntime.awrap(x)`, so that the runtime can test
707
- // `hasOwn.call(value, "__await")` to determine if the yielded value is
708
- // meant to be awaited.
709
- exports.awrap = function(arg) {
710
- return { __await: arg };
711
- };
712
-
713
- function AsyncIterator(generator, PromiseImpl) {
714
- function invoke(method, arg, resolve, reject) {
715
- var record = tryCatch(generator[method], generator, arg);
716
- if (record.type === "throw") {
717
- reject(record.arg);
718
- } else {
719
- var result = record.arg;
720
- var value = result.value;
721
- if (value &&
722
- typeof value === "object" &&
723
- hasOwn.call(value, "__await")) {
724
- return PromiseImpl.resolve(value.__await).then(function(value) {
725
- invoke("next", value, resolve, reject);
726
- }, function(err) {
727
- invoke("throw", err, resolve, reject);
728
- });
729
- }
730
-
731
- return PromiseImpl.resolve(value).then(function(unwrapped) {
732
- // When a yielded Promise is resolved, its final value becomes
733
- // the .value of the Promise<{value,done}> result for the
734
- // current iteration.
735
- result.value = unwrapped;
736
- resolve(result);
737
- }, function(error) {
738
- // If a rejected Promise was yielded, throw the rejection back
739
- // into the async generator function so it can be handled there.
740
- return invoke("throw", error, resolve, reject);
741
- });
742
- }
743
- }
744
-
745
- var previousPromise;
746
-
747
- function enqueue(method, arg) {
748
- function callInvokeWithMethodAndArg() {
749
- return new PromiseImpl(function(resolve, reject) {
750
- invoke(method, arg, resolve, reject);
751
- });
752
- }
753
-
754
- return previousPromise =
755
- // If enqueue has been called before, then we want to wait until
756
- // all previous Promises have been resolved before calling invoke,
757
- // so that results are always delivered in the correct order. If
758
- // enqueue has not been called before, then it is important to
759
- // call invoke immediately, without waiting on a callback to fire,
760
- // so that the async generator function has the opportunity to do
761
- // any necessary setup in a predictable way. This predictability
762
- // is why the Promise constructor synchronously invokes its
763
- // executor callback, and why async functions synchronously
764
- // execute code before the first await. Since we implement simple
765
- // async functions in terms of async generators, it is especially
766
- // important to get this right, even though it requires care.
767
- previousPromise ? previousPromise.then(
768
- callInvokeWithMethodAndArg,
769
- // Avoid propagating failures to Promises returned by later
770
- // invocations of the iterator.
771
- callInvokeWithMethodAndArg
772
- ) : callInvokeWithMethodAndArg();
773
- }
774
-
775
- // Define the unified helper method that is used to implement .next,
776
- // .throw, and .return (see defineIteratorMethods).
777
- this._invoke = enqueue;
778
- }
779
-
780
- defineIteratorMethods(AsyncIterator.prototype);
781
- AsyncIterator.prototype[asyncIteratorSymbol] = function () {
782
- return this;
783
- };
784
- exports.AsyncIterator = AsyncIterator;
785
-
786
- // Note that simple async functions are implemented on top of
787
- // AsyncIterator objects; they just return a Promise for the value of
788
- // the final result produced by the iterator.
789
- exports.async = function(innerFn, outerFn, self, tryLocsList, PromiseImpl) {
790
- if (PromiseImpl === void 0) PromiseImpl = Promise;
791
-
792
- var iter = new AsyncIterator(
793
- wrap(innerFn, outerFn, self, tryLocsList),
794
- PromiseImpl
795
- );
796
-
797
- return exports.isGeneratorFunction(outerFn)
798
- ? iter // If outerFn is a generator, return the full iterator.
799
- : iter.next().then(function(result) {
800
- return result.done ? result.value : iter.next();
801
- });
802
- };
803
-
804
- function makeInvokeMethod(innerFn, self, context) {
805
- var state = GenStateSuspendedStart;
806
-
807
- return function invoke(method, arg) {
808
- if (state === GenStateExecuting) {
809
- throw new Error("Generator is already running");
810
- }
811
-
812
- if (state === GenStateCompleted) {
813
- if (method === "throw") {
814
- throw arg;
815
- }
816
-
817
- // Be forgiving, per 25.3.3.3.3 of the spec:
818
- // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
819
- return doneResult();
820
- }
821
-
822
- context.method = method;
823
- context.arg = arg;
824
-
825
- while (true) {
826
- var delegate = context.delegate;
827
- if (delegate) {
828
- var delegateResult = maybeInvokeDelegate(delegate, context);
829
- if (delegateResult) {
830
- if (delegateResult === ContinueSentinel) continue;
831
- return delegateResult;
832
- }
833
- }
834
-
835
- if (context.method === "next") {
836
- // Setting context._sent for legacy support of Babel's
837
- // function.sent implementation.
838
- context.sent = context._sent = context.arg;
839
-
840
- } else if (context.method === "throw") {
841
- if (state === GenStateSuspendedStart) {
842
- state = GenStateCompleted;
843
- throw context.arg;
844
- }
845
-
846
- context.dispatchException(context.arg);
847
-
848
- } else if (context.method === "return") {
849
- context.abrupt("return", context.arg);
850
- }
851
-
852
- state = GenStateExecuting;
853
-
854
- var record = tryCatch(innerFn, self, context);
855
- if (record.type === "normal") {
856
- // If an exception is thrown from innerFn, we leave state ===
857
- // GenStateExecuting and loop back for another invocation.
858
- state = context.done
859
- ? GenStateCompleted
860
- : GenStateSuspendedYield;
861
-
862
- if (record.arg === ContinueSentinel) {
863
- continue;
864
- }
865
-
866
- return {
867
- value: record.arg,
868
- done: context.done
869
- };
870
-
871
- } else if (record.type === "throw") {
872
- state = GenStateCompleted;
873
- // Dispatch the exception by looping back around to the
874
- // context.dispatchException(context.arg) call above.
875
- context.method = "throw";
876
- context.arg = record.arg;
877
- }
878
- }
879
- };
880
- }
881
-
882
- // Call delegate.iterator[context.method](context.arg) and handle the
883
- // result, either by returning a { value, done } result from the
884
- // delegate iterator, or by modifying context.method and context.arg,
885
- // setting context.delegate to null, and returning the ContinueSentinel.
886
- function maybeInvokeDelegate(delegate, context) {
887
- var method = delegate.iterator[context.method];
888
- if (method === undefined$1) {
889
- // A .throw or .return when the delegate iterator has no .throw
890
- // method always terminates the yield* loop.
891
- context.delegate = null;
892
-
893
- if (context.method === "throw") {
894
- // Note: ["return"] must be used for ES3 parsing compatibility.
895
- if (delegate.iterator["return"]) {
896
- // If the delegate iterator has a return method, give it a
897
- // chance to clean up.
898
- context.method = "return";
899
- context.arg = undefined$1;
900
- maybeInvokeDelegate(delegate, context);
901
-
902
- if (context.method === "throw") {
903
- // If maybeInvokeDelegate(context) changed context.method from
904
- // "return" to "throw", let that override the TypeError below.
905
- return ContinueSentinel;
906
- }
907
- }
908
-
909
- context.method = "throw";
910
- context.arg = new TypeError(
911
- "The iterator does not provide a 'throw' method");
912
- }
913
-
914
- return ContinueSentinel;
915
- }
916
-
917
- var record = tryCatch(method, delegate.iterator, context.arg);
918
-
919
- if (record.type === "throw") {
920
- context.method = "throw";
921
- context.arg = record.arg;
922
- context.delegate = null;
923
- return ContinueSentinel;
924
- }
925
-
926
- var info = record.arg;
927
-
928
- if (! info) {
929
- context.method = "throw";
930
- context.arg = new TypeError("iterator result is not an object");
931
- context.delegate = null;
932
- return ContinueSentinel;
933
- }
934
-
935
- if (info.done) {
936
- // Assign the result of the finished delegate to the temporary
937
- // variable specified by delegate.resultName (see delegateYield).
938
- context[delegate.resultName] = info.value;
939
-
940
- // Resume execution at the desired location (see delegateYield).
941
- context.next = delegate.nextLoc;
942
-
943
- // If context.method was "throw" but the delegate handled the
944
- // exception, let the outer generator proceed normally. If
945
- // context.method was "next", forget context.arg since it has been
946
- // "consumed" by the delegate iterator. If context.method was
947
- // "return", allow the original .return call to continue in the
948
- // outer generator.
949
- if (context.method !== "return") {
950
- context.method = "next";
951
- context.arg = undefined$1;
952
- }
953
-
954
- } else {
955
- // Re-yield the result returned by the delegate method.
956
- return info;
957
- }
958
-
959
- // The delegate iterator is finished, so forget it and continue with
960
- // the outer generator.
961
- context.delegate = null;
962
- return ContinueSentinel;
963
- }
964
-
965
- // Define Generator.prototype.{next,throw,return} in terms of the
966
- // unified ._invoke helper method.
967
- defineIteratorMethods(Gp);
968
-
969
- define(Gp, toStringTagSymbol, "Generator");
970
-
971
- // A Generator should always return itself as the iterator object when the
972
- // @@iterator function is called on it. Some browsers' implementations of the
973
- // iterator prototype chain incorrectly implement this, causing the Generator
974
- // object to not be returned from this call. This ensures that doesn't happen.
975
- // See https://github.com/facebook/regenerator/issues/274 for more details.
976
- Gp[iteratorSymbol] = function() {
977
- return this;
978
- };
979
-
980
- Gp.toString = function() {
981
- return "[object Generator]";
982
- };
983
-
984
- function pushTryEntry(locs) {
985
- var entry = { tryLoc: locs[0] };
986
-
987
- if (1 in locs) {
988
- entry.catchLoc = locs[1];
989
- }
990
-
991
- if (2 in locs) {
992
- entry.finallyLoc = locs[2];
993
- entry.afterLoc = locs[3];
994
- }
995
-
996
- this.tryEntries.push(entry);
997
- }
998
-
999
- function resetTryEntry(entry) {
1000
- var record = entry.completion || {};
1001
- record.type = "normal";
1002
- delete record.arg;
1003
- entry.completion = record;
1004
- }
1005
-
1006
- function Context(tryLocsList) {
1007
- // The root entry object (effectively a try statement without a catch
1008
- // or a finally block) gives us a place to store values thrown from
1009
- // locations where there is no enclosing try statement.
1010
- this.tryEntries = [{ tryLoc: "root" }];
1011
- tryLocsList.forEach(pushTryEntry, this);
1012
- this.reset(true);
1013
- }
1014
-
1015
- exports.keys = function(object) {
1016
- var keys = [];
1017
- for (var key in object) {
1018
- keys.push(key);
1019
- }
1020
- keys.reverse();
1021
-
1022
- // Rather than returning an object with a next method, we keep
1023
- // things simple and return the next function itself.
1024
- return function next() {
1025
- while (keys.length) {
1026
- var key = keys.pop();
1027
- if (key in object) {
1028
- next.value = key;
1029
- next.done = false;
1030
- return next;
1031
- }
1032
- }
1033
-
1034
- // To avoid creating an additional object, we just hang the .value
1035
- // and .done properties off the next function object itself. This
1036
- // also ensures that the minifier will not anonymize the function.
1037
- next.done = true;
1038
- return next;
1039
- };
1040
- };
1041
-
1042
- function values(iterable) {
1043
- if (iterable) {
1044
- var iteratorMethod = iterable[iteratorSymbol];
1045
- if (iteratorMethod) {
1046
- return iteratorMethod.call(iterable);
1047
- }
1048
-
1049
- if (typeof iterable.next === "function") {
1050
- return iterable;
1051
- }
1052
-
1053
- if (!isNaN(iterable.length)) {
1054
- var i = -1, next = function next() {
1055
- while (++i < iterable.length) {
1056
- if (hasOwn.call(iterable, i)) {
1057
- next.value = iterable[i];
1058
- next.done = false;
1059
- return next;
1060
- }
1061
- }
1062
-
1063
- next.value = undefined$1;
1064
- next.done = true;
1065
-
1066
- return next;
1067
- };
1068
-
1069
- return next.next = next;
1070
- }
1071
- }
1072
-
1073
- // Return an iterator with no values.
1074
- return { next: doneResult };
1075
- }
1076
- exports.values = values;
1077
-
1078
- function doneResult() {
1079
- return { value: undefined$1, done: true };
1080
- }
1081
-
1082
- Context.prototype = {
1083
- constructor: Context,
1084
-
1085
- reset: function(skipTempReset) {
1086
- this.prev = 0;
1087
- this.next = 0;
1088
- // Resetting context._sent for legacy support of Babel's
1089
- // function.sent implementation.
1090
- this.sent = this._sent = undefined$1;
1091
- this.done = false;
1092
- this.delegate = null;
1093
-
1094
- this.method = "next";
1095
- this.arg = undefined$1;
1096
-
1097
- this.tryEntries.forEach(resetTryEntry);
1098
-
1099
- if (!skipTempReset) {
1100
- for (var name in this) {
1101
- // Not sure about the optimal order of these conditions:
1102
- if (name.charAt(0) === "t" &&
1103
- hasOwn.call(this, name) &&
1104
- !isNaN(+name.slice(1))) {
1105
- this[name] = undefined$1;
1106
- }
1107
- }
1108
- }
1109
- },
1110
-
1111
- stop: function() {
1112
- this.done = true;
1113
-
1114
- var rootEntry = this.tryEntries[0];
1115
- var rootRecord = rootEntry.completion;
1116
- if (rootRecord.type === "throw") {
1117
- throw rootRecord.arg;
1118
- }
1119
-
1120
- return this.rval;
1121
- },
1122
-
1123
- dispatchException: function(exception) {
1124
- if (this.done) {
1125
- throw exception;
1126
- }
1127
-
1128
- var context = this;
1129
- function handle(loc, caught) {
1130
- record.type = "throw";
1131
- record.arg = exception;
1132
- context.next = loc;
1133
-
1134
- if (caught) {
1135
- // If the dispatched exception was caught by a catch block,
1136
- // then let that catch block handle the exception normally.
1137
- context.method = "next";
1138
- context.arg = undefined$1;
1139
- }
1140
-
1141
- return !! caught;
1142
- }
1143
-
1144
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
1145
- var entry = this.tryEntries[i];
1146
- var record = entry.completion;
1147
-
1148
- if (entry.tryLoc === "root") {
1149
- // Exception thrown outside of any try block that could handle
1150
- // it, so set the completion value of the entire function to
1151
- // throw the exception.
1152
- return handle("end");
1153
- }
1154
-
1155
- if (entry.tryLoc <= this.prev) {
1156
- var hasCatch = hasOwn.call(entry, "catchLoc");
1157
- var hasFinally = hasOwn.call(entry, "finallyLoc");
1158
-
1159
- if (hasCatch && hasFinally) {
1160
- if (this.prev < entry.catchLoc) {
1161
- return handle(entry.catchLoc, true);
1162
- } else if (this.prev < entry.finallyLoc) {
1163
- return handle(entry.finallyLoc);
1164
- }
1165
-
1166
- } else if (hasCatch) {
1167
- if (this.prev < entry.catchLoc) {
1168
- return handle(entry.catchLoc, true);
1169
- }
1170
-
1171
- } else if (hasFinally) {
1172
- if (this.prev < entry.finallyLoc) {
1173
- return handle(entry.finallyLoc);
1174
- }
1175
-
1176
- } else {
1177
- throw new Error("try statement without catch or finally");
1178
- }
1179
- }
1180
- }
1181
- },
1182
-
1183
- abrupt: function(type, arg) {
1184
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
1185
- var entry = this.tryEntries[i];
1186
- if (entry.tryLoc <= this.prev &&
1187
- hasOwn.call(entry, "finallyLoc") &&
1188
- this.prev < entry.finallyLoc) {
1189
- var finallyEntry = entry;
1190
- break;
1191
- }
1192
- }
1193
-
1194
- if (finallyEntry &&
1195
- (type === "break" ||
1196
- type === "continue") &&
1197
- finallyEntry.tryLoc <= arg &&
1198
- arg <= finallyEntry.finallyLoc) {
1199
- // Ignore the finally entry if control is not jumping to a
1200
- // location outside the try/catch block.
1201
- finallyEntry = null;
1202
- }
1203
-
1204
- var record = finallyEntry ? finallyEntry.completion : {};
1205
- record.type = type;
1206
- record.arg = arg;
1207
-
1208
- if (finallyEntry) {
1209
- this.method = "next";
1210
- this.next = finallyEntry.finallyLoc;
1211
- return ContinueSentinel;
1212
- }
1213
-
1214
- return this.complete(record);
1215
- },
1216
-
1217
- complete: function(record, afterLoc) {
1218
- if (record.type === "throw") {
1219
- throw record.arg;
1220
- }
1221
-
1222
- if (record.type === "break" ||
1223
- record.type === "continue") {
1224
- this.next = record.arg;
1225
- } else if (record.type === "return") {
1226
- this.rval = this.arg = record.arg;
1227
- this.method = "return";
1228
- this.next = "end";
1229
- } else if (record.type === "normal" && afterLoc) {
1230
- this.next = afterLoc;
1231
- }
1232
-
1233
- return ContinueSentinel;
1234
- },
1235
-
1236
- finish: function(finallyLoc) {
1237
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
1238
- var entry = this.tryEntries[i];
1239
- if (entry.finallyLoc === finallyLoc) {
1240
- this.complete(entry.completion, entry.afterLoc);
1241
- resetTryEntry(entry);
1242
- return ContinueSentinel;
1243
- }
1244
- }
1245
- },
1246
-
1247
- "catch": function(tryLoc) {
1248
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
1249
- var entry = this.tryEntries[i];
1250
- if (entry.tryLoc === tryLoc) {
1251
- var record = entry.completion;
1252
- if (record.type === "throw") {
1253
- var thrown = record.arg;
1254
- resetTryEntry(entry);
1255
- }
1256
- return thrown;
1257
- }
1258
- }
1259
-
1260
- // The context.catch method must only be called with a location
1261
- // argument that corresponds to a known catch block.
1262
- throw new Error("illegal catch attempt");
1263
- },
1264
-
1265
- delegateYield: function(iterable, resultName, nextLoc) {
1266
- this.delegate = {
1267
- iterator: values(iterable),
1268
- resultName: resultName,
1269
- nextLoc: nextLoc
1270
- };
1271
-
1272
- if (this.method === "next") {
1273
- // Deliberately forget the last sent value so that we don't
1274
- // accidentally pass it on to the delegate.
1275
- this.arg = undefined$1;
1276
- }
1277
-
1278
- return ContinueSentinel;
1279
- }
1280
- };
1281
-
1282
- // Regardless of whether this script is executing as a CommonJS module
1283
- // or not, return the runtime object so that we can declare the variable
1284
- // regeneratorRuntime in the outer scope, which allows this module to be
1285
- // injected easily by `bin/regenerator --include-runtime script.js`.
1286
- return exports;
1287
-
1288
- }(
1289
- // If this script is executing as a CommonJS module, use module.exports
1290
- // as the regeneratorRuntime namespace. Otherwise create a new empty
1291
- // object. Either way, the resulting object will be used to initialize
1292
- // the regeneratorRuntime variable at the top of this file.
1293
- module.exports
1294
- ));
1295
-
1296
- try {
1297
- regeneratorRuntime = runtime;
1298
- } catch (accidentalStrictMode) {
1299
- // This module should not be running in strict mode, so the above
1300
- // assignment should always work unless something is misconfigured. Just
1301
- // in case runtime.js accidentally runs in strict mode, we can escape
1302
- // strict mode using a global Function call. This could conceivably fail
1303
- // if a Content Security Policy forbids using Function, but in that case
1304
- // the proper solution is to fix the accidental strict mode problem. If
1305
- // you've misconfigured your bundler to force strict mode and applied a
1306
- // CSP to forbid Function, and you're not willing to fix either of those
1307
- // problems, please detail your unique predicament in a GitHub issue.
1308
- Function("r", "regeneratorRuntime = r")(runtime);
1309
- }
1310
- });
1311
-
1312
- (function (Task) {
1313
- /**
1314
- * Custom task monad constructor
1315
- *
1316
- * @template R returned task's resolve type
1317
- * @param invoke promise defining task execution
1318
- * @param cancel cancelation function
1319
- * @returns task resolving to resolve value of invoke
1320
- *
1321
- * @note low-level primitive for creating custom tasks, not intended for general use
1322
- */
1323
- function create(invoke, cancel) {
1324
- return new TaskClass(invoke, cancel);
1325
- }
1326
-
1327
- Task.create = create;
1328
- /**
1329
- * Invariant task constructor creating resolved task from plain value
1330
- *
1331
- * @template R returned task's resolve type
1332
- * @param value value to be returned upon awaiting
1333
- * @returns task resolving to specified value
1334
- */
1335
-
1336
- function resolved(value) {
1337
- return Task.create(Promise.resolve(exports.Maybe.just(exports.Either.right(value))), function () {});
1338
- }
1339
-
1340
- Task.resolved = resolved;
1341
- /**
1342
- * Invariant task constructor creating rejected task from error value
1343
- *
1344
- * @template R returned task's resolve type
1345
- * @param error error to be returned upon awaiting
1346
- * @returns task resolving to specified value
1347
- */
1348
-
1349
- function rejected(error) {
1350
- return Task.create(Promise.resolve(exports.Maybe.just(exports.Either.left(error))), function () {});
1351
- }
1352
-
1353
- Task.rejected = rejected;
1354
- /**
1355
- * Invariant task constructor creating canceled task
1356
- *
1357
- * @template R returned task's resolve type
1358
- * @returns task resolving to specified value
1359
- */
1360
-
1361
- function canceled() {
1362
- return Task.create(Promise.resolve(exports.Maybe.nothing()), function () {});
1363
- }
1364
-
1365
- Task.canceled = canceled;
1366
- /**
1367
- * Lift from promise to task resolving to that promise result
1368
- *
1369
- * Userfull for converting promises to tasks
1370
- * If converted task is canceled or failed externally return value will be ignored without side-effects
1371
- * All tasks are no-trowing by default, any occured errors are returned as Left<any>
1372
- *
1373
- * @template R returned task resolve type
1374
- * @param promise promise to be resolved
1375
- * @returns task resolving to specified promise value
1376
- *
1377
- * @example
1378
- * ```typescript
1379
- * const task = Task.fromPromise(someAsyncOperation('someData')).map((result) => result.length);
1380
- * ```
1381
- */
1382
-
1383
- function fromPromise(promise) {
1384
- var _resolver = resolver(),
1385
- resolve = _resolver[0],
1386
- setResolve = _resolver[1];
1387
-
1388
- return Task.create(new Promise(function (_resolve) {
1389
- setResolve(_resolve);
1390
- promise.then(exports.Either.right, exports.Either.left).then(exports.Maybe.just).then(resolve);
1391
- }), function (error) {
1392
- return resolve(error.map(exports.Either.left));
1393
- });
1394
- }
1395
-
1396
- Task.fromPromise = fromPromise;
1397
- /**
1398
- * Lift from sync function to task resolving to said function result
1399
- *
1400
- * Generally, functions are not considered tasks as they cannot be interrupted in any way
1401
- * However there may be cases when impure function performs actions leading to cancelling or failing the task they are called from
1402
- * Without such wrapper such cases would lead to ignoring said cancelation and continuilg task chain
1403
- * This function is mostly used internally and is not expected to be widely used except of some edge scenarios
1404
- *
1405
- * If converted task is canceled or failed externally return value will be ignored without side-effects
1406
- * All tasks are no-trowing by default, any occured errors are returned as Left<any>
1407
- *
1408
- * @template R returned task resolve type
1409
- * @param producer function to be wrapped to a task
1410
- * @param cancelRef reference object allowing to set cancelation function prior to invoking function
1411
- * @returns task resolving to the specified function's result
1412
- *
1413
- * @example
1414
- * ```typescript
1415
- * const cancelRef = { cancel: () => {} };
1416
- *
1417
- * const task = Task.fromFunction(() => someFunction('someData'), cancelRef).map((result) => result.length);
1418
- * ```
1419
- */
1420
-
1421
- function fromFunction(producer, cancelRef) {
1422
- var _resolver2 = resolver(),
1423
- resolve = _resolver2[0],
1424
- setResolve = _resolver2[1];
1425
-
1426
- var cancel = function cancel(error) {
1427
- return resolve(error.map(exports.Either.left));
1428
- };
1429
-
1430
- cancelRef.cancel = cancel;
1431
- return Task.create(new Promise(function (_resolve) {
1432
- setResolve(_resolve);
1433
-
1434
- try {
1435
- resolve(exports.Maybe.just(exports.Either.right(producer())));
1436
- } catch (e) {
1437
- resolve(exports.Maybe.just(exports.Either.left(e)));
1438
- }
1439
- }), cancel);
1440
- }
1441
-
1442
- Task.fromFunction = fromFunction;
1443
- /**
1444
- * Convinience shortcut for yielding async functions as tasks
1445
- *
1446
- * @template R returned generator resolve type
1447
- * @param promise promise to be resolved
1448
- * @returns generator to be be used with yield*
1449
- */
1450
-
1451
- function promiseGenerator(promise) {
1452
- return Task.fromPromise(promise).generator();
1453
- }
1454
-
1455
- Task.promiseGenerator = promiseGenerator;
1456
- /**
1457
- * Lift from function returning value/promise to function returning task resolving to that value
1458
- *
1459
- * Userfull for converting exising async functions to task functions for further use
1460
- *
1461
- * @template A returned function's argument types
1462
- * @template R returned function's result task's resolve type
1463
- * @param promiseFunction function returning promise or plain value to be resolved
1464
- * @returns task function wrapping specified promise function
1465
- * @see Task.fromPromise
1466
- *
1467
- * @example
1468
- * ```typescript
1469
- * const taskFunction = liftPromiseFunction(someAsyncOperation);
1470
- *
1471
- * const task = taskFunction('someData').map((result) => result.length);
1472
- * ```
1473
- */
1474
-
1475
- function lift(promiseFunction) {
1476
- return function () {
1477
- return Task.fromPromise(promiseFunction.apply(void 0, arguments));
1478
- };
1479
- }
1480
-
1481
- Task.lift = lift;
1482
- /**
1483
- * Create compound task from generator function
1484
- *
1485
- * Applying yield to task within the generator function awaits the task and returns underlying value in case of success
1486
- * However the convinient option for typescript is to use ```yield* task.generator()``` as othervise one may have to deal with union types
1487
- *
1488
- * In case of failure an error is thrown and may be caught by try-catch
1489
- *
1490
- * If error is not caught the task is interrupted and returns 'just left error' immediately. In case of cancelation the task is interrupted and returns 'nothing' immediately
1491
- *
1492
- * @template TT yielded task type
1493
- * @template R returned task resolve type
1494
- * @param taskGeneratorFunction task generator function
1495
- * @returns task resolving to generator's return type
1496
- *
1497
- * @note compound task execution is interrupted only at yield statements, so despite returning immediately any promise-based chains would continue running until the first yield
1498
- * @note due to type unpredictability you HAVE to use ```.generator()``` together with yield* to avoid type issues, despite the fact that task may be yielded directly
1499
- *
1500
- * @example
1501
- * ```typescript
1502
- * const task = Task.generate(function*() {
1503
- * try {
1504
- * const value1 = yield* delayedValueTask('data', 400).generator();
1505
- *
1506
- * const value2 = yield* delayedValueTask(value1.length, 300).generator();
1507
- *
1508
- * return value2;
1509
- * } catch (e) {
1510
- * return -1;
1511
- * }
1512
- * });
1513
- * ```
1514
- */
1515
-
1516
- function generate(taskGeneratorFunction) {
1517
- var generator = taskGeneratorFunction();
1518
-
1519
- var sequentor = function sequentor(next) {
1520
- return next.done ? Task.resolved(next.value) : next.value.matchChain({
1521
- resolved: function resolved(value) {
1522
- return sequentor(generator.next(value));
1523
- },
1524
- rejected: function rejected(error) {
1525
- return sequentor(generator["throw"](error));
1526
- },
1527
- canceled: Task.canceled
1528
- });
1529
- };
1530
-
1531
- return Task.resolved(undefined).chain(function () {
1532
- return sequentor(generator.next());
1533
- });
1534
- }
1535
-
1536
- Task.generate = generate;
1537
- /**
1538
- * Generic timeout task
1539
- *
1540
- * Usefull for creating delays in task chains or implementing limiting tasks
1541
- *
1542
- * @param delay duration in ms after that the task resolves to void
1543
- * @returns task resolving to void (undefined) after specified delay
1544
- *
1545
- * @example
1546
- * ```typescript
1547
- * const delayedValueTask = <T>(value: T, delay: number) => Task.timeout(delay).map(() => value);
1548
- * // ... //
1549
- * const value = yield* delayedValueTask(42, 1000).generator();
1550
- *
1551
- * console.log("It's past 1 second and here's a value:", value)
1552
- * ```
1553
- */
1554
-
1555
- function timeout(delay) {
1556
- return fromCallback(function (resolve) {
1557
- return setTimeout(function () {
1558
- return resolve();
1559
- }, delay);
1560
- }, clearTimeout);
1561
- }
1562
-
1563
- Task.timeout = timeout;
1564
- /**
1565
- * Generic callback task
1566
- *
1567
- * Usefull for creating custom tasks from success and error callbacks and cancelaton function. Synchronous callbacks are not supported, @see fromFuction
1568
- *
1569
- * @param create task body with success and error callbacks
1570
- * @param cancel cancelation function intended for stopping task execution
1571
- * @returns task resolving to success value
1572
- *
1573
- * @example
1574
- * ```typescript
1575
- * export function timeout(delay: number) {
1576
- * return fromCallback<NodeJS.Timeout, void>((resolve) => setTimeout(() => resolve(), delay), clearTimeout);
1577
- * }
1578
- * ```
1579
- */
1580
-
1581
- function fromCallback(create, cancel) {
1582
- var handler;
1583
-
1584
- var _resolver3 = resolver(),
1585
- resolve = _resolver3[0],
1586
- setResolve = _resolver3[1];
1587
-
1588
- return Task.create(new Promise(function (_resolve) {
1589
- setResolve(_resolve);
1590
- handler = create(function (r) {
1591
- return resolve(exports.Maybe.just(exports.Either.right(r)));
1592
- }, function (e) {
1593
- return resolve(exports.Maybe.just(exports.Either.left(e)));
1594
- });
1595
- }), function (error) {
1596
- resolve(error.map(exports.Either.left));
1597
- cancel(handler);
1598
- });
1599
- }
1600
-
1601
- Task.fromCallback = fromCallback;
1602
- /**
1603
- * Start multiple tasks one after another
1604
- *
1605
- * @param taskFunctions list of task functions (without arguments)
1606
- * @returns composite task invoring every task in order and resolving to the list of results
1607
- */
1608
-
1609
- function sequence(taskFunctions) {
1610
- return Array.from(taskFunctions).reduce(function (prev, taskFunction) {
1611
- return prev.chain(function (list) {
1612
- return taskFunction().map(function (value) {
1613
- return list.concat(value);
1614
- });
1615
- });
1616
- }, Task.resolved([]));
1617
- }
1618
-
1619
- Task.sequence = sequence;
1620
- /**
1621
- * Execute all tasks in parallel and return list of results
1622
- *
1623
- * Resulting task resolves when all tasks are resolved, returning list of tasks results maintaining result order
1624
- * In case of failure (external or internal) resulting task is immediately immediately rejected with that error and all pending tasks are canceled
1625
- * In case of cancelation resulting task is immediately immediately canceled together with all pending tasks
1626
- *
1627
- * @note direct equivalent of Promise.all
1628
- *
1629
- * @template T underlying task type
1630
- * @param tasks iterable of tasks to execute
1631
- * @retuirns task resolving to the list of results
1632
- */
1633
-
1634
- function all(tasks) {
1635
- var _resolver4 = resolver(),
1636
- resolve = _resolver4[0],
1637
- setResolve = _resolver4[1];
1638
-
1639
- var taskArray = Array.from(tasks);
1640
-
1641
- var cancel = function cancel(error) {
1642
- return taskArray.forEach(function (task) {
1643
- return task._cancel(error);
1644
- });
1645
- };
1646
-
1647
- var list = taskArray.map(exports.Maybe.nothing);
1648
-
1649
- var _resolved = function resolved(value, i) {
1650
- list[i] = exports.Maybe.just(value);
1651
-
1652
- if (exports.Maybe.everyJust(list)) {
1653
- resolve(exports.Maybe.just(exports.Either.right(list.map(exports.Just.just))));
1654
- }
1655
- };
1656
-
1657
- var _rejected = function rejected(error) {
1658
- resolve(error.map(exports.Either.left));
1659
- cancel(error);
1660
- };
1661
-
1662
- return Task.create(new Promise(function (_resolve) {
1663
- setResolve(_resolve);
1664
- taskArray.map(function (task, i) {
1665
- return task.matchTap({
1666
- resolved: function resolved(value) {
1667
- return _resolved(value, i);
1668
- },
1669
- rejected: function rejected(error) {
1670
- return _rejected(exports.Maybe.just(error));
1671
- },
1672
- canceled: function canceled() {
1673
- return _rejected(exports.Maybe.nothing());
1674
- }
1675
- });
1676
- });
1677
- }), cancel);
1678
- }
1679
-
1680
- Task.all = all;
1681
- /**
1682
- * Execute all tasks in parallel and return the result of the first successful one
1683
- *
1684
- * Resulting task resolves when the first tasks is successfuly resolved, returning it's result
1685
- * In case of external task rejecttion all tasks are rejected with that error and resulting task is rejected with an array of errors
1686
- * In case of internal task rejection the execution is continued untill fthe first task resolves succesfully or all the tasks are rejected this way resulting task is rejected with an array of errors
1687
- * In case of cancelation resulting task is immediately immediately canceled together with all pending tasks
1688
- *
1689
- * @note direct equivalent of Promise.any
1690
- *
1691
- * @template T underlying task type
1692
- * @param tasks iterable of tasks to execute
1693
- * @retuirns task resolving to the result of first successfull task
1694
- */
1695
-
1696
- function any(tasks) {
1697
- var _resolver5 = resolver(),
1698
- resolve = _resolver5[0],
1699
- setResolve = _resolver5[1];
1700
-
1701
- var taskArray = Array.from(tasks);
1702
-
1703
- var cancel = function cancel(error) {
1704
- return taskArray.forEach(function (task) {
1705
- return task._cancel(error);
1706
- });
1707
- };
1708
-
1709
- var list = taskArray.map(exports.Maybe.nothing);
1710
-
1711
- var _resolved2 = function resolved(value) {
1712
- resolve(value.map(exports.Either.right));
1713
- cancel(exports.Maybe.nothing());
1714
- };
1715
-
1716
- var _rejected2 = function rejected(error, i) {
1717
- list[i] = exports.Maybe.just(error);
1718
-
1719
- if (exports.Maybe.everyJust(list)) {
1720
- resolve(exports.Maybe.just(exports.Either.left(list.map(exports.Just.just))));
1721
- }
1722
- };
1723
-
1724
- return Task.create(new Promise(function (_resolve) {
1725
- setResolve(_resolve);
1726
- taskArray.map(function (task, i) {
1727
- return task.matchTap({
1728
- resolved: function resolved(value) {
1729
- return _resolved2(exports.Maybe.just(value));
1730
- },
1731
- rejected: function rejected(error) {
1732
- return _rejected2(error, i);
1733
- },
1734
- canceled: function canceled() {
1735
- return _resolved2(exports.Maybe.nothing());
1736
- }
1737
- });
1738
- });
1739
- }), cancel);
1740
- }
1741
-
1742
- Task.any = any;
1743
- /**
1744
- * Limit task execution based on another task
1745
- *
1746
- * If the second task resolves sooner than the first, resulting task is canceled
1747
- * Otherwise the resulting task resolves together with the first one
1748
- *
1749
- * @param task task to limit
1750
- * @param limitTask limiting task
1751
- * @returns task limites according to limitTask
1752
- */
1753
-
1754
- function limit(task, limitTask) {
1755
- return Task.any([task, limitTask.chain(Task.canceled)]).chainRejected(function (errors) {
1756
- return Task.rejected(errors[0]);
1757
- });
1758
- }
1759
-
1760
- Task.limit = limit;
1761
- /**
1762
- * Repeat task untill successful
1763
- *
1764
- * Given task is re-generated in case of failure (both external and internal)
1765
- * Resulting task is still cancelable by standard means
1766
- *
1767
- * @param taskFunction task preoducing function
1768
- */
1769
-
1770
- function repeat(taskFunction) {
1771
- var sequentor = function sequentor() {
1772
- return taskFunction().chainRejected(sequentor);
1773
- };
1774
-
1775
- return sequentor();
1776
- }
1777
-
1778
- Task.repeat = repeat;
1779
- })(exports.Task || (exports.Task = {}));
1780
-
1781
- var resolver = function resolver() {
1782
- var stub = function stub(_) {};
1783
-
1784
- var globalResolve = stub;
1785
-
1786
- var resolve = function resolve(value) {
1787
- var r = globalResolve;
1788
- globalResolve = stub;
1789
- r(value);
1790
- };
1791
-
1792
- var setResolve = function setResolve(value) {
1793
- globalResolve = value;
1794
- };
1795
-
1796
- return [resolve, setResolve];
1797
- };
1798
-
1799
- function chainTaskMaybe(_task, op) {
1800
- var globalCancel = {
1801
- cancel: _task._cancel
1802
- };
1803
- return exports.Task.create(_task._invoke.then( /*#__PURE__*/function () {
1804
- var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(result) {
1805
- var _Task$fromFunction, invoke2, cancel2, produced, _produced$just$right, invoke3, cancel3;
1806
-
1807
- return runtime_1.wrap(function _callee$(_context) {
1808
- while (1) {
1809
- switch (_context.prev = _context.next) {
1810
- case 0:
1811
- _context.prev = 0;
1812
- _Task$fromFunction = exports.Task.fromFunction(function () {
1813
- return op(result);
1814
- }, globalCancel), invoke2 = _Task$fromFunction._invoke, cancel2 = _Task$fromFunction._cancel;
1815
- globalCancel.cancel = cancel2;
1816
- _context.next = 5;
1817
- return invoke2;
1818
-
1819
- case 5:
1820
- produced = _context.sent;
1821
-
1822
- if (!produced.isNothing()) {
1823
- _context.next = 8;
1824
- break;
1825
- }
1826
-
1827
- return _context.abrupt("return", exports.Maybe.nothing());
1828
-
1829
- case 8:
1830
- if (!produced.just.isLeft()) {
1831
- _context.next = 10;
1832
- break;
1833
- }
1834
-
1835
- return _context.abrupt("return", exports.Maybe.just(exports.Either.left(produced.just.left)));
1836
-
1837
- case 10:
1838
- _produced$just$right = produced.just.right, invoke3 = _produced$just$right._invoke, cancel3 = _produced$just$right._cancel;
1839
- globalCancel.cancel = cancel3;
1840
- return _context.abrupt("return", invoke3);
1841
-
1842
- case 15:
1843
- _context.prev = 15;
1844
- _context.t0 = _context["catch"](0);
1845
- return _context.abrupt("return", exports.Maybe.just(exports.Either.left(_context.t0)));
1846
-
1847
- case 18:
1848
- case "end":
1849
- return _context.stop();
1850
- }
1851
- }
1852
- }, _callee, null, [[0, 15]]);
1853
- }));
1854
-
1855
- return function (_x) {
1856
- return _ref.apply(this, arguments);
1857
- };
1858
- }()), function (error) {
1859
- return globalCancel.cancel(error);
1860
- });
1861
- }
1862
-
1863
- function chainTaskEither(_task, op) {
1864
- return chainTaskMaybe(_task, function (maybe) {
1865
- return maybe.map(op).orMap(exports.Task.canceled).just;
1866
- });
1867
- }
1868
-
1869
- function mapTaskMaybe(_task, op) {
1870
- return chainTaskMaybe(_task, function (maybe) {
1871
- return op(maybe).matchMap({
1872
- just: function just(either) {
1873
- return either.matchMap({
1874
- right: exports.Task.resolved,
1875
- left: exports.Task.rejected
1876
- }).right;
1877
- },
1878
- nothing: exports.Task.canceled
1879
- }).just;
1880
- });
1881
- }
1882
-
1883
- function mapTaskEither(_task, op) {
1884
- return mapTaskMaybe(_task, function (maybe) {
1885
- return maybe.map(op);
1886
- });
1887
- }
1888
-
1889
- function tapTaskMaybe(_task, op) {
1890
- return mapTaskMaybe(_task, function (value) {
1891
- op(value);
1892
- return value;
1893
- });
1894
- }
1895
-
1896
- function tapTaskEither(_task, op) {
1897
- return tapTaskMaybe(_task, function (maybe) {
1898
- return maybe.tap(op);
1899
- });
1900
- }
1901
-
1902
- var TaskClass = /*#__PURE__*/function () {
1903
- function TaskClass(_invoke, _cancel) {
1904
- this._invoke = _invoke;
1905
- this._cancel = _cancel;
1906
- }
1907
-
1908
- var _proto = TaskClass.prototype;
1909
-
1910
- _proto.tapCanceled = function tapCanceled(op) {
1911
- return tapTaskMaybe(this, function (maybe) {
1912
- return maybe.orTap(op);
1913
- });
1914
- };
1915
-
1916
- _proto.tapRejected = function tapRejected(op) {
1917
- return tapTaskEither(this, function (either) {
1918
- return either.orTap(op);
1919
- });
1920
- };
1921
-
1922
- _proto.tap = function tap(op) {
1923
- return tapTaskEither(this, function (either) {
1924
- return either.tap(op);
1925
- });
1926
- };
1927
-
1928
- _proto.mapCanceled = function mapCanceled(op) {
1929
- return mapTaskMaybe(this, function (maybe) {
1930
- return maybe.orMap(function () {
1931
- return exports.Either.right(op());
1932
- });
1933
- });
1934
- };
1935
-
1936
- _proto.mapRejected = function mapRejected(op) {
1937
- return mapTaskEither(this, function (either) {
1938
- return either.orMap(op);
1939
- });
1940
- };
1941
-
1942
- _proto.map = function map(op) {
1943
- return mapTaskEither(this, function (either) {
1944
- return either.map(op);
1945
- });
1946
- };
1947
-
1948
- _proto.chainCanceled = function chainCanceled(op) {
1949
- var mapEither = function mapEither(either) {
1950
- return either.map(exports.Task.resolved).orMap(exports.Task.rejected).right;
1951
- };
1952
-
1953
- return chainTaskMaybe(this, function (maybe) {
1954
- return maybe.map(mapEither).orMap(op).just;
1955
- });
1956
- };
1957
-
1958
- _proto.chainRejected = function chainRejected(op) {
1959
- return chainTaskEither(this, function (either) {
1960
- return either.map(exports.Task.resolved).orMap(op).right;
1961
- });
1962
- };
1963
-
1964
- _proto.chain = function chain(op) {
1965
- return chainTaskEither(this, function (either) {
1966
- return either.map(op).orMap(exports.Task.rejected).right;
1967
- });
1968
- };
1969
-
1970
- _proto.resolve = function resolve() {
1971
- return this._invoke;
1972
- };
1973
-
1974
- _proto.cancel = function cancel() {
1975
- return this._cancel(exports.Maybe.nothing());
1976
- };
1977
-
1978
- _proto.reject = function reject(error) {
1979
- return this._cancel(exports.Maybe.just(error));
1980
- };
1981
-
1982
- _proto.generator = function generator() {
1983
- return /*#__PURE__*/runtime_1.mark(function _callee2(task) {
1984
- return runtime_1.wrap(function _callee2$(_context2) {
1985
- while (1) {
1986
- switch (_context2.prev = _context2.next) {
1987
- case 0:
1988
- _context2.next = 2;
1989
- return task;
1990
-
1991
- case 2:
1992
- return _context2.abrupt("return", _context2.sent);
1993
-
1994
- case 3:
1995
- case "end":
1996
- return _context2.stop();
1997
- }
1998
- }
1999
- }, _callee2);
2000
- })(this);
2001
- };
2002
-
2003
- _proto.matchTap = function matchTap(op) {
2004
- return tapTaskMaybe(this, function (maybe) {
2005
- return maybe.matchTap({
2006
- just: function just(either) {
2007
- return either.matchTap({
2008
- right: op.resolved,
2009
- left: op.rejected
2010
- });
2011
- },
2012
- nothing: op.canceled
2013
- });
2014
- });
2015
- };
2016
-
2017
- _proto.matchMap = function matchMap(op) {
2018
- return mapTaskMaybe(this, function (maybe) {
2019
- return maybe.matchMap({
2020
- just: function just(either) {
2021
- return either.matchMap({
2022
- right: op.resolved,
2023
- left: op.rejected
2024
- });
2025
- },
2026
- nothing: function nothing() {
2027
- return exports.Either.right(op.canceled());
2028
- }
2029
- });
2030
- });
2031
- };
2032
-
2033
- _proto.matchChain = function matchChain(op) {
2034
- return chainTaskMaybe(this, function (maybe) {
2035
- return maybe.matchMap({
2036
- just: function just(either) {
2037
- return either.matchMap({
2038
- right: op.resolved,
2039
- left: op.rejected
2040
- }).right;
2041
- },
2042
- nothing: op.canceled
2043
- }).just;
2044
- });
2045
- };
2046
-
2047
- return TaskClass;
2048
- }();
2049
- //# sourceMappingURL=t-tasks.cjs.development.js.map