UNPKG

49.6 kBJavaScriptView Raw
1var AsyncEndpoint = (function (exports) {
2'use strict';
3
4//
5var channel = (function () {
6 var limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : Infinity;
7
8 var releaseRequest = function releaseRequest() {};
9 var releaseResponse = function releaseResponse() {};
10 //Calling request will return a new promise that's primed to resolve with the arguments of respond when next called
11 var request = function request() {
12 var debug = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
13
14 var lastAnswer = void 0;
15 if (limit < 1) {
16 return Promise.resolve(lastAnswer);
17 }
18 limit--;
19 setTimeout(releaseResponse);
20 var returnPromise = new Promise(function (outerResolve) {
21 releaseRequest = function releaseRequest(answer) {
22 var returnPromiseB = new Promise(function (innerResolve) {
23 outerResolve(answer);
24 releaseResponse = function releaseResponse() {
25 return innerResolve(debug);
26 };
27 return releaseResponse;
28 });
29 return returnPromiseB;
30 };
31 return releaseRequest;
32 });
33 return returnPromise;
34 };
35 //Calling respond will resolve the promise most recently created by calling request.
36 //It returns a promise that will be resolved after the next call to request creates a new promise.
37 var respond = function respond(answer) {
38 return releaseRequest(answer);
39 };
40 return [request, respond];
41});
42
43var asyncIterator = function (iterable) {
44 if (typeof Symbol === "function") {
45 if (Symbol.asyncIterator) {
46 var method = iterable[Symbol.asyncIterator];
47 if (method != null) return method.call(iterable);
48 }
49
50 if (Symbol.iterator) {
51 return iterable[Symbol.iterator]();
52 }
53 }
54
55 throw new TypeError("Object is not async iterable");
56};
57
58var asyncGenerator = function () {
59 function AwaitValue(value) {
60 this.value = value;
61 }
62
63 function AsyncGenerator(gen) {
64 var front, back;
65
66 function send(key, arg) {
67 return new Promise(function (resolve, reject) {
68 var request = {
69 key: key,
70 arg: arg,
71 resolve: resolve,
72 reject: reject,
73 next: null
74 };
75
76 if (back) {
77 back = back.next = request;
78 } else {
79 front = back = request;
80 resume(key, arg);
81 }
82 });
83 }
84
85 function resume(key, arg) {
86 try {
87 var result = gen[key](arg);
88 var value = result.value;
89
90 if (value instanceof AwaitValue) {
91 Promise.resolve(value.value).then(function (arg) {
92 resume("next", arg);
93 }, function (arg) {
94 resume("throw", arg);
95 });
96 } else {
97 settle(result.done ? "return" : "normal", result.value);
98 }
99 } catch (err) {
100 settle("throw", err);
101 }
102 }
103
104 function settle(type, value) {
105 switch (type) {
106 case "return":
107 front.resolve({
108 value: value,
109 done: true
110 });
111 break;
112
113 case "throw":
114 front.reject(value);
115 break;
116
117 default:
118 front.resolve({
119 value: value,
120 done: false
121 });
122 break;
123 }
124
125 front = front.next;
126
127 if (front) {
128 resume(front.key, front.arg);
129 } else {
130 back = null;
131 }
132 }
133
134 this._invoke = send;
135
136 if (typeof gen.return !== "function") {
137 this.return = undefined;
138 }
139 }
140
141 if (typeof Symbol === "function" && Symbol.asyncIterator) {
142 AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
143 return this;
144 };
145 }
146
147 AsyncGenerator.prototype.next = function (arg) {
148 return this._invoke("next", arg);
149 };
150
151 AsyncGenerator.prototype.throw = function (arg) {
152 return this._invoke("throw", arg);
153 };
154
155 AsyncGenerator.prototype.return = function (arg) {
156 return this._invoke("return", arg);
157 };
158
159 return {
160 wrap: function (fn) {
161 return function () {
162 return new AsyncGenerator(fn.apply(this, arguments));
163 };
164 },
165 await: function (value) {
166 return new AwaitValue(value);
167 }
168 };
169}();
170
171
172
173var asyncToGenerator = function (fn) {
174 return function () {
175 var gen = fn.apply(this, arguments);
176 return new Promise(function (resolve, reject) {
177 function step(key, arg) {
178 try {
179 var info = gen[key](arg);
180 var value = info.value;
181 } catch (error) {
182 reject(error);
183 return;
184 }
185
186 if (info.done) {
187 resolve(value);
188 } else {
189 return Promise.resolve(value).then(function (value) {
190 step("next", value);
191 }, function (err) {
192 step("throw", err);
193 });
194 }
195 }
196
197 return step("next");
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
232var slicedToArray = function () {
233 function sliceIterator(arr, i) {
234 var _arr = [];
235 var _n = true;
236 var _d = false;
237 var _e = undefined;
238
239 try {
240 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
241 _arr.push(_s.value);
242
243 if (i && _arr.length === i) break;
244 }
245 } catch (err) {
246 _d = true;
247 _e = err;
248 } finally {
249 try {
250 if (!_n && _i["return"]) _i["return"]();
251 } finally {
252 if (_d) throw _e;
253 }
254 }
255
256 return _arr;
257 }
258
259 return function (arr, i) {
260 if (Array.isArray(arr)) {
261 return arr;
262 } else if (Symbol.iterator in Object(arr)) {
263 return sliceIterator(arr, i);
264 } else {
265 throw new TypeError("Invalid attempt to destructure non-iterable instance");
266 }
267 };
268}();
269
270
271
272
273
274
275
276
277
278
279
280
281
282var toConsumableArray = function (arr) {
283 if (Array.isArray(arr)) {
284 for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
285
286 return arr2;
287 } else {
288 return Array.from(arr);
289 }
290};
291
292//
293var map = (function (iterator, mapper) {
294 var newGenerator = function () {
295 var _ref = asyncGenerator.wrap( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
296 var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, item;
297
298 return regeneratorRuntime.wrap(function _callee$(_context) {
299 while (1) {
300 switch (_context.prev = _context.next) {
301 case 0:
302 _iteratorNormalCompletion = true;
303 _didIteratorError = false;
304 _iteratorError = undefined;
305 _context.prev = 3;
306 _iterator = asyncIterator(iterator);
307
308 case 5:
309 _context.next = 7;
310 return asyncGenerator.await(_iterator.next());
311
312 case 7:
313 _step = _context.sent;
314 _iteratorNormalCompletion = _step.done;
315 _context.next = 11;
316 return asyncGenerator.await(_step.value);
317
318 case 11:
319 _value = _context.sent;
320
321 if (_iteratorNormalCompletion) {
322 _context.next = 21;
323 break;
324 }
325
326 item = _value;
327 _context.next = 16;
328 return asyncGenerator.await(mapper(item));
329
330 case 16:
331 _context.next = 18;
332 return _context.sent;
333
334 case 18:
335 _iteratorNormalCompletion = true;
336 _context.next = 5;
337 break;
338
339 case 21:
340 _context.next = 27;
341 break;
342
343 case 23:
344 _context.prev = 23;
345 _context.t0 = _context["catch"](3);
346 _didIteratorError = true;
347 _iteratorError = _context.t0;
348
349 case 27:
350 _context.prev = 27;
351 _context.prev = 28;
352
353 if (!(!_iteratorNormalCompletion && _iterator.return)) {
354 _context.next = 32;
355 break;
356 }
357
358 _context.next = 32;
359 return asyncGenerator.await(_iterator.return());
360
361 case 32:
362 _context.prev = 32;
363
364 if (!_didIteratorError) {
365 _context.next = 35;
366 break;
367 }
368
369 throw _iteratorError;
370
371 case 35:
372 return _context.finish(32);
373
374 case 36:
375 return _context.finish(27);
376
377 case 37:
378 case "end":
379 return _context.stop();
380 }
381 }
382 }, _callee, this, [[3, 23, 27, 37], [28,, 32, 36]]);
383 }));
384
385 return function newGenerator() {
386 return _ref.apply(this, arguments);
387 };
388 }();
389 return newGenerator();
390});
391
392var _this = undefined;
393
394//
395/*eslint no-console: "off"*/
396var createPassThrought = (function () {
397 for (var _len = arguments.length, targets = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
398 targets[_key - 1] = arguments[_key];
399 }
400
401 var target = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : console.log;
402 return function () {
403 for (var _len2 = arguments.length, programs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
404 programs[_key2] = arguments[_key2];
405 }
406
407 var _loop = function _loop(program) {
408 asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
409 var _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _value, output, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _iterator3, _step3, _target;
410
411 return regeneratorRuntime.wrap(function _callee$(_context) {
412 while (1) {
413 switch (_context.prev = _context.next) {
414 case 0:
415 _iteratorNormalCompletion2 = true;
416 _didIteratorError2 = false;
417 _iteratorError2 = undefined;
418 _context.prev = 3;
419 _iterator2 = asyncIterator(program);
420
421 case 5:
422 _context.next = 7;
423 return _iterator2.next();
424
425 case 7:
426 _step2 = _context.sent;
427 _iteratorNormalCompletion2 = _step2.done;
428 _context.next = 11;
429 return _step2.value;
430
431 case 11:
432 _value = _context.sent;
433
434 if (_iteratorNormalCompletion2) {
435 _context.next = 45;
436 break;
437 }
438
439 output = _value;
440 _context.next = 16;
441 return target(output);
442
443 case 16:
444 _iteratorNormalCompletion3 = true;
445 _didIteratorError3 = false;
446 _iteratorError3 = undefined;
447 _context.prev = 19;
448 _iterator3 = targets[Symbol.iterator]();
449
450 case 21:
451 if (_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done) {
452 _context.next = 28;
453 break;
454 }
455
456 _target = _step3.value;
457 _context.next = 25;
458 return _target(output);
459
460 case 25:
461 _iteratorNormalCompletion3 = true;
462 _context.next = 21;
463 break;
464
465 case 28:
466 _context.next = 34;
467 break;
468
469 case 30:
470 _context.prev = 30;
471 _context.t0 = _context["catch"](19);
472 _didIteratorError3 = true;
473 _iteratorError3 = _context.t0;
474
475 case 34:
476 _context.prev = 34;
477 _context.prev = 35;
478
479 if (!_iteratorNormalCompletion3 && _iterator3.return) {
480 _iterator3.return();
481 }
482
483 case 37:
484 _context.prev = 37;
485
486 if (!_didIteratorError3) {
487 _context.next = 40;
488 break;
489 }
490
491 throw _iteratorError3;
492
493 case 40:
494 return _context.finish(37);
495
496 case 41:
497 return _context.finish(34);
498
499 case 42:
500 _iteratorNormalCompletion2 = true;
501 _context.next = 5;
502 break;
503
504 case 45:
505 _context.next = 51;
506 break;
507
508 case 47:
509 _context.prev = 47;
510 _context.t1 = _context["catch"](3);
511 _didIteratorError2 = true;
512 _iteratorError2 = _context.t1;
513
514 case 51:
515 _context.prev = 51;
516 _context.prev = 52;
517
518 if (!(!_iteratorNormalCompletion2 && _iterator2.return)) {
519 _context.next = 56;
520 break;
521 }
522
523 _context.next = 56;
524 return _iterator2.return();
525
526 case 56:
527 _context.prev = 56;
528
529 if (!_didIteratorError2) {
530 _context.next = 59;
531 break;
532 }
533
534 throw _iteratorError2;
535
536 case 59:
537 return _context.finish(56);
538
539 case 60:
540 return _context.finish(51);
541
542 case 61:
543 case "end":
544 return _context.stop();
545 }
546 }
547 }, _callee, _this, [[3, 47, 51, 61], [19, 30, 34, 42], [35,, 37, 41], [52,, 56, 60]]);
548 }))();
549 };
550
551 //targets[0] = targets[0] || console.log;
552 var _iteratorNormalCompletion = true;
553 var _didIteratorError = false;
554 var _iteratorError = undefined;
555
556 try {
557 for (var _iterator = programs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
558 var program = _step.value;
559
560 _loop(program);
561 }
562 } catch (err) {
563 _didIteratorError = true;
564 _iteratorError = err;
565 } finally {
566 try {
567 if (!_iteratorNormalCompletion && _iterator.return) {
568 _iterator.return();
569 }
570 } finally {
571 if (_didIteratorError) {
572 throw _iteratorError;
573 }
574 }
575 }
576 };
577});
578
579var _this$1 = undefined;
580
581//
582var composeAsyncTransformer = (function (current) {
583 var pre = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (_) {
584 return _;
585 };
586 return asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
587 var _args = arguments;
588 return regeneratorRuntime.wrap(function _callee$(_context) {
589 while (1) {
590 switch (_context.prev = _context.next) {
591 case 0:
592 _context.t0 = current;
593 _context.next = 3;
594 return pre.apply(undefined, _args);
595
596 case 3:
597 _context.t1 = _context.sent;
598 _context.next = 6;
599 return (0, _context.t0)(_context.t1);
600
601 case 6:
602 return _context.abrupt("return", _context.sent);
603
604 case 7:
605 case "end":
606 return _context.stop();
607 }
608 }
609 }, _callee, _this$1);
610 }));
611});
612
613//
614var composePrograms = (function () {
615 for (var _len = arguments.length, programs = Array(_len), _key = 0; _key < _len; _key++) {
616 programs[_key] = arguments[_key];
617 }
618
619 return function (initialRequest) {
620 var _programs$pop = programs.pop(),
621 _programs$pop2 = slicedToArray(_programs$pop, 4),
622 lastProgram = _programs$pop2[0],
623 lastInit = _programs$pop2[1],
624 lastA = _programs$pop2[2],
625 lastB = _programs$pop2[3];
626
627 var request = initialRequest;
628 var _iteratorNormalCompletion = true;
629 var _didIteratorError = false;
630 var _iteratorError = undefined;
631
632 try {
633 for (var _iterator = programs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
634 var output = _step.value;
635
636 var program = void 0,
637 init = void 0,
638 a = void 0,
639 b = void 0;
640 if (typeof output === "function") {
641 program = output;
642 a = b = function b($) {
643 return $;
644 };
645 } else {
646 program = output[0];
647 init = output[1];
648 a = output[2];
649 b = output[3];
650 }
651
652 var _channel = channel(),
653 _channel2 = slicedToArray(_channel, 2),
654 requestNext = _channel2[0],
655 respond = _channel2[1];
656
657 createPassThrought(program(init, composeAsyncTransformer(a, request)), composeAsyncTransformer(respond, b));
658 request = requestNext;
659 }
660 //handle last
661 } catch (err) {
662 _didIteratorError = true;
663 _iteratorError = err;
664 } finally {
665 try {
666 if (!_iteratorNormalCompletion && _iterator.return) {
667 _iterator.return();
668 }
669 } finally {
670 if (_didIteratorError) {
671 throw _iteratorError;
672 }
673 }
674 }
675
676 return map(lastProgram(lastInit, composeAsyncTransformer(lastA, request)), lastB);
677 };
678});
679
680var _this$2 = undefined;
681
682//
683var forEach = (function () {
684 var _ref = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(iterator, handler) {
685 var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, item;
686
687 return regeneratorRuntime.wrap(function _callee$(_context) {
688 while (1) {
689 switch (_context.prev = _context.next) {
690 case 0:
691 _iteratorNormalCompletion = true;
692 _didIteratorError = false;
693 _iteratorError = undefined;
694 _context.prev = 3;
695 _iterator = asyncIterator(iterator);
696
697 case 5:
698 _context.next = 7;
699 return _iterator.next();
700
701 case 7:
702 _step = _context.sent;
703 _iteratorNormalCompletion = _step.done;
704 _context.next = 11;
705 return _step.value;
706
707 case 11:
708 _value = _context.sent;
709
710 if (_iteratorNormalCompletion) {
711 _context.next = 19;
712 break;
713 }
714
715 item = _value;
716 _context.next = 16;
717 return handler(item);
718
719 case 16:
720 _iteratorNormalCompletion = true;
721 _context.next = 5;
722 break;
723
724 case 19:
725 _context.next = 25;
726 break;
727
728 case 21:
729 _context.prev = 21;
730 _context.t0 = _context["catch"](3);
731 _didIteratorError = true;
732 _iteratorError = _context.t0;
733
734 case 25:
735 _context.prev = 25;
736 _context.prev = 26;
737
738 if (!(!_iteratorNormalCompletion && _iterator.return)) {
739 _context.next = 30;
740 break;
741 }
742
743 _context.next = 30;
744 return _iterator.return();
745
746 case 30:
747 _context.prev = 30;
748
749 if (!_didIteratorError) {
750 _context.next = 33;
751 break;
752 }
753
754 throw _iteratorError;
755
756 case 33:
757 return _context.finish(30);
758
759 case 34:
760 return _context.finish(25);
761
762 case 35:
763 case "end":
764 return _context.stop();
765 }
766 }
767 }, _callee, _this$2, [[3, 21, 25, 35], [26,, 30, 34]]);
768 }));
769
770 return function (_x, _x2) {
771 return _ref.apply(this, arguments);
772 };
773})();
774
775//
776var filter = (function (iterator, filterer) {
777 var newGenerator = function () {
778 var _ref = asyncGenerator.wrap( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
779 var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, item;
780
781 return regeneratorRuntime.wrap(function _callee$(_context) {
782 while (1) {
783 switch (_context.prev = _context.next) {
784 case 0:
785 _iteratorNormalCompletion = true;
786 _didIteratorError = false;
787 _iteratorError = undefined;
788 _context.prev = 3;
789 _iterator = asyncIterator(iterator);
790
791 case 5:
792 _context.next = 7;
793 return asyncGenerator.await(_iterator.next());
794
795 case 7:
796 _step = _context.sent;
797 _iteratorNormalCompletion = _step.done;
798 _context.next = 11;
799 return asyncGenerator.await(_step.value);
800
801 case 11:
802 _value = _context.sent;
803
804 if (_iteratorNormalCompletion) {
805 _context.next = 22;
806 break;
807 }
808
809 item = _value;
810 _context.next = 16;
811 return asyncGenerator.await(filterer(item));
812
813 case 16:
814 if (!_context.sent) {
815 _context.next = 19;
816 break;
817 }
818
819 _context.next = 19;
820 return item;
821
822 case 19:
823 _iteratorNormalCompletion = true;
824 _context.next = 5;
825 break;
826
827 case 22:
828 _context.next = 28;
829 break;
830
831 case 24:
832 _context.prev = 24;
833 _context.t0 = _context["catch"](3);
834 _didIteratorError = true;
835 _iteratorError = _context.t0;
836
837 case 28:
838 _context.prev = 28;
839 _context.prev = 29;
840
841 if (!(!_iteratorNormalCompletion && _iterator.return)) {
842 _context.next = 33;
843 break;
844 }
845
846 _context.next = 33;
847 return asyncGenerator.await(_iterator.return());
848
849 case 33:
850 _context.prev = 33;
851
852 if (!_didIteratorError) {
853 _context.next = 36;
854 break;
855 }
856
857 throw _iteratorError;
858
859 case 36:
860 return _context.finish(33);
861
862 case 37:
863 return _context.finish(28);
864
865 case 38:
866 case "end":
867 return _context.stop();
868 }
869 }
870 }, _callee, this, [[3, 24, 28, 38], [29,, 33, 37]]);
871 }));
872
873 return function newGenerator() {
874 return _ref.apply(this, arguments);
875 };
876 }();
877 return newGenerator();
878});
879
880//
881var reduce = (function (iterator, reducer, initial) {
882 var condition = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function () {
883 return false;
884 };
885 var resetInitial = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : function () {
886 return initial;
887 };
888
889 var newGenerator = function () {
890 var _ref = asyncGenerator.wrap( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
891 var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, item;
892
893 return regeneratorRuntime.wrap(function _callee$(_context) {
894 while (1) {
895 switch (_context.prev = _context.next) {
896 case 0:
897 _iteratorNormalCompletion = true;
898 _didIteratorError = false;
899 _iteratorError = undefined;
900 _context.prev = 3;
901 _iterator = asyncIterator(iterator);
902
903 case 5:
904 _context.next = 7;
905 return asyncGenerator.await(_iterator.next());
906
907 case 7:
908 _step = _context.sent;
909 _iteratorNormalCompletion = _step.done;
910 _context.next = 11;
911 return asyncGenerator.await(_step.value);
912
913 case 11:
914 _value = _context.sent;
915
916 if (_iteratorNormalCompletion) {
917 _context.next = 24;
918 break;
919 }
920
921 item = _value;
922 _context.next = 16;
923 return asyncGenerator.await(reducer(item, initial));
924
925 case 16:
926 initial = _context.sent;
927
928 if (!condition(item, initial)) {
929 _context.next = 21;
930 break;
931 }
932
933 _context.next = 20;
934 return initial;
935
936 case 20:
937 initial = resetInitial(item, initial);
938
939 case 21:
940 _iteratorNormalCompletion = true;
941 _context.next = 5;
942 break;
943
944 case 24:
945 _context.next = 30;
946 break;
947
948 case 26:
949 _context.prev = 26;
950 _context.t0 = _context["catch"](3);
951 _didIteratorError = true;
952 _iteratorError = _context.t0;
953
954 case 30:
955 _context.prev = 30;
956 _context.prev = 31;
957
958 if (!(!_iteratorNormalCompletion && _iterator.return)) {
959 _context.next = 35;
960 break;
961 }
962
963 _context.next = 35;
964 return asyncGenerator.await(_iterator.return());
965
966 case 35:
967 _context.prev = 35;
968
969 if (!_didIteratorError) {
970 _context.next = 38;
971 break;
972 }
973
974 throw _iteratorError;
975
976 case 38:
977 return _context.finish(35);
978
979 case 39:
980 return _context.finish(30);
981
982 case 40:
983 case "end":
984 return _context.stop();
985 }
986 }
987 }, _callee, this, [[3, 26, 30, 40], [31,, 35, 39]]);
988 }));
989
990 return function newGenerator() {
991 return _ref.apply(this, arguments);
992 };
993 }();
994 return newGenerator();
995});
996
997//
998var pause = (function () {
999 var milliseconds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
1000 var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
1001 return new Promise(function (resolve) {
1002 return setTimeout(function () {
1003 return resolve(value);
1004 }, milliseconds);
1005 });
1006});
1007
1008//
1009var createQueue = (function () {
1010 for (var _len = arguments.length, initial = Array(_len), _key = 0; _key < _len; _key++) {
1011 initial[_key] = arguments[_key];
1012 }
1013
1014 var push = function push(item) {
1015 return initial.push(item);
1016 };
1017 var newGenerator = function () {
1018 var _ref = asyncGenerator.wrap( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
1019 return regeneratorRuntime.wrap(function _callee$(_context) {
1020 while (1) {
1021 switch (_context.prev = _context.next) {
1022 case 0:
1023
1024
1025 if (!initial.length) {
1026 _context.next = 8;
1027 break;
1028 }
1029
1030 _context.next = 4;
1031 return asyncGenerator.await(initial.shift());
1032
1033 case 4:
1034 _context.next = 6;
1035 return _context.sent;
1036
1037 case 6:
1038 _context.next = 8;
1039 break;
1040
1041 case 8:
1042 _context.next = 0;
1043 break;
1044
1045 case 10:
1046 case "end":
1047 return _context.stop();
1048 }
1049 }
1050 }, _callee, this);
1051 }));
1052
1053 return function newGenerator() {
1054 return _ref.apply(this, arguments);
1055 };
1056 }();
1057 return [newGenerator(), push];
1058});
1059
1060//
1061var createStack = (function () {
1062 for (var _len = arguments.length, initial = Array(_len), _key = 0; _key < _len; _key++) {
1063 initial[_key] = arguments[_key];
1064 }
1065
1066 var push = function push(item) {
1067 return initial.push(item);
1068 };
1069 var newGenerator = function () {
1070 var _ref = asyncGenerator.wrap( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
1071 return regeneratorRuntime.wrap(function _callee$(_context) {
1072 while (1) {
1073 switch (_context.prev = _context.next) {
1074 case 0:
1075
1076
1077 if (!initial.length) {
1078 _context.next = 8;
1079 break;
1080 }
1081
1082 _context.next = 4;
1083 return asyncGenerator.await(initial.pop());
1084
1085 case 4:
1086 _context.next = 6;
1087 return _context.sent;
1088
1089 case 6:
1090 _context.next = 8;
1091 break;
1092
1093 case 8:
1094 _context.next = 0;
1095 break;
1096
1097 case 10:
1098 case "end":
1099 return _context.stop();
1100 }
1101 }
1102 }, _callee, this);
1103 }));
1104
1105 return function newGenerator() {
1106 return _ref.apply(this, arguments);
1107 };
1108 }();
1109 return [newGenerator(), push];
1110});
1111
1112var _this$3 = undefined;
1113
1114//
1115var createProgramQueue = (function () {
1116 var _createQueue = createQueue(),
1117 _createQueue2 = slicedToArray(_createQueue, 2),
1118 iterator = _createQueue2[0],
1119 push = _createQueue2[1];
1120
1121 var render = createPassThrought(push);
1122 var listen = function () {
1123 var _ref = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
1124 for (var _len = arguments.length, programs = Array(_len), _key = 0; _key < _len; _key++) {
1125 programs[_key] = arguments[_key];
1126 }
1127
1128 var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, program;
1129
1130 return regeneratorRuntime.wrap(function _callee$(_context) {
1131 while (1) {
1132 switch (_context.prev = _context.next) {
1133 case 0:
1134 _iteratorNormalCompletion = true;
1135 _didIteratorError = false;
1136 _iteratorError = undefined;
1137 _context.prev = 3;
1138
1139 for (_iterator = programs[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
1140 program = _step.value;
1141
1142 render(program);
1143 }
1144 _context.next = 11;
1145 break;
1146
1147 case 7:
1148 _context.prev = 7;
1149 _context.t0 = _context["catch"](3);
1150 _didIteratorError = true;
1151 _iteratorError = _context.t0;
1152
1153 case 11:
1154 _context.prev = 11;
1155 _context.prev = 12;
1156
1157 if (!_iteratorNormalCompletion && _iterator.return) {
1158 _iterator.return();
1159 }
1160
1161 case 14:
1162 _context.prev = 14;
1163
1164 if (!_didIteratorError) {
1165 _context.next = 17;
1166 break;
1167 }
1168
1169 throw _iteratorError;
1170
1171 case 17:
1172 return _context.finish(14);
1173
1174 case 18:
1175 return _context.finish(11);
1176
1177 case 19:
1178 case "end":
1179 return _context.stop();
1180 }
1181 }
1182 }, _callee, _this$3, [[3, 7, 11, 19], [12,, 14, 18]]);
1183 }));
1184
1185 return function listen() {
1186 return _ref.apply(this, arguments);
1187 };
1188 }();
1189 return [iterator, listen];
1190});
1191
1192var _this$4 = undefined;
1193
1194//
1195var index = (function () {
1196 var _ref = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(iterator, number) {
1197 var skip = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
1198
1199 var result, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, output;
1200
1201 return regeneratorRuntime.wrap(function _callee$(_context) {
1202 while (1) {
1203 switch (_context.prev = _context.next) {
1204 case 0:
1205 result = [];
1206 _iteratorNormalCompletion = true;
1207 _didIteratorError = false;
1208 _iteratorError = undefined;
1209 _context.prev = 4;
1210 _iterator = asyncIterator(iterator);
1211
1212 case 6:
1213 _context.next = 8;
1214 return _iterator.next();
1215
1216 case 8:
1217 _step = _context.sent;
1218 _iteratorNormalCompletion = _step.done;
1219 _context.next = 12;
1220 return _step.value;
1221
1222 case 12:
1223 _value = _context.sent;
1224
1225 if (_iteratorNormalCompletion) {
1226 _context.next = 25;
1227 break;
1228 }
1229
1230 output = _value;
1231
1232 if (skip) {
1233 _context.next = 21;
1234 break;
1235 }
1236
1237 if (!(result.length === number)) {
1238 _context.next = 18;
1239 break;
1240 }
1241
1242 return _context.abrupt("return", result);
1243
1244 case 18:
1245 result.push(output);
1246 _context.next = 22;
1247 break;
1248
1249 case 21:
1250 skip--;
1251
1252 case 22:
1253 _iteratorNormalCompletion = true;
1254 _context.next = 6;
1255 break;
1256
1257 case 25:
1258 _context.next = 31;
1259 break;
1260
1261 case 27:
1262 _context.prev = 27;
1263 _context.t0 = _context["catch"](4);
1264 _didIteratorError = true;
1265 _iteratorError = _context.t0;
1266
1267 case 31:
1268 _context.prev = 31;
1269 _context.prev = 32;
1270
1271 if (!(!_iteratorNormalCompletion && _iterator.return)) {
1272 _context.next = 36;
1273 break;
1274 }
1275
1276 _context.next = 36;
1277 return _iterator.return();
1278
1279 case 36:
1280 _context.prev = 36;
1281
1282 if (!_didIteratorError) {
1283 _context.next = 39;
1284 break;
1285 }
1286
1287 throw _iteratorError;
1288
1289 case 39:
1290 return _context.finish(36);
1291
1292 case 40:
1293 return _context.finish(31);
1294
1295 case 41:
1296 case "end":
1297 return _context.stop();
1298 }
1299 }
1300 }, _callee, _this$4, [[4, 27, 31, 41], [32,, 36, 40]]);
1301 }));
1302
1303 return function (_x2, _x3) {
1304 return _ref.apply(this, arguments);
1305 };
1306})();
1307
1308var _this$5 = undefined;
1309
1310//
1311var _while = (function () {
1312 var _ref = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(iterator) {
1313 var accept = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {
1314 return true;
1315 };
1316 var skip = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
1317
1318 var result, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, output;
1319
1320 return regeneratorRuntime.wrap(function _callee$(_context) {
1321 while (1) {
1322 switch (_context.prev = _context.next) {
1323 case 0:
1324 result = [];
1325 _iteratorNormalCompletion = true;
1326 _didIteratorError = false;
1327 _iteratorError = undefined;
1328 _context.prev = 4;
1329 _iterator = asyncIterator(iterator);
1330
1331 case 6:
1332 _context.next = 8;
1333 return _iterator.next();
1334
1335 case 8:
1336 _step = _context.sent;
1337 _iteratorNormalCompletion = _step.done;
1338 _context.next = 12;
1339 return _step.value;
1340
1341 case 12:
1342 _value = _context.sent;
1343
1344 if (_iteratorNormalCompletion) {
1345 _context.next = 25;
1346 break;
1347 }
1348
1349 output = _value;
1350
1351 if (skip) {
1352 _context.next = 21;
1353 break;
1354 }
1355
1356 if (accept(output)) {
1357 _context.next = 18;
1358 break;
1359 }
1360
1361 return _context.abrupt("return", result);
1362
1363 case 18:
1364 result.push(output);
1365 _context.next = 22;
1366 break;
1367
1368 case 21:
1369 skip--;
1370
1371 case 22:
1372 _iteratorNormalCompletion = true;
1373 _context.next = 6;
1374 break;
1375
1376 case 25:
1377 _context.next = 31;
1378 break;
1379
1380 case 27:
1381 _context.prev = 27;
1382 _context.t0 = _context["catch"](4);
1383 _didIteratorError = true;
1384 _iteratorError = _context.t0;
1385
1386 case 31:
1387 _context.prev = 31;
1388 _context.prev = 32;
1389
1390 if (!(!_iteratorNormalCompletion && _iterator.return)) {
1391 _context.next = 36;
1392 break;
1393 }
1394
1395 _context.next = 36;
1396 return _iterator.return();
1397
1398 case 36:
1399 _context.prev = 36;
1400
1401 if (!_didIteratorError) {
1402 _context.next = 39;
1403 break;
1404 }
1405
1406 throw _iteratorError;
1407
1408 case 39:
1409 return _context.finish(36);
1410
1411 case 40:
1412 return _context.finish(31);
1413
1414 case 41:
1415 case "end":
1416 return _context.stop();
1417 }
1418 }
1419 }, _callee, _this$5, [[4, 27, 31, 41], [32,, 36, 40]]);
1420 }));
1421
1422 return function (_x3) {
1423 return _ref.apply(this, arguments);
1424 };
1425})();
1426
1427//
1428var identity = (function () {
1429 var _ref = asyncGenerator.wrap( /*#__PURE__*/regeneratorRuntime.mark(function _callee(delay, request) {
1430 return regeneratorRuntime.wrap(function _callee$(_context) {
1431 while (1) {
1432 switch (_context.prev = _context.next) {
1433 case 0:
1434
1435
1436 _context.next = 3;
1437 return asyncGenerator.await(pause(delay));
1438
1439 case 3:
1440 _context.next = 5;
1441 return asyncGenerator.await(request());
1442
1443 case 5:
1444 _context.next = 7;
1445 return _context.sent;
1446
1447 case 7:
1448 _context.next = 0;
1449 break;
1450
1451 case 9:
1452 case "end":
1453 return _context.stop();
1454 }
1455 }
1456 }, _callee, this);
1457 }));
1458
1459 return function (_x, _x2) {
1460 return _ref.apply(this, arguments);
1461 };
1462})();
1463
1464//
1465var continuousOutput = (function () {
1466 var _ref = asyncGenerator.wrap( /*#__PURE__*/regeneratorRuntime.mark(function _callee(sample) {
1467 return regeneratorRuntime.wrap(function _callee$(_context) {
1468 while (1) {
1469 switch (_context.prev = _context.next) {
1470 case 0:
1471
1472
1473 _context.next = 3;
1474 return asyncGenerator.await(sample());
1475
1476 case 3:
1477 _context.next = 5;
1478 return _context.sent;
1479
1480 case 5:
1481 _context.next = 0;
1482 break;
1483
1484 case 7:
1485 case "end":
1486 return _context.stop();
1487 }
1488 }
1489 }, _callee, this);
1490 }));
1491
1492 return function (_x) {
1493 return _ref.apply(this, arguments);
1494 };
1495})();
1496
1497var _this$6 = undefined;
1498
1499//
1500var tee = (function () {
1501 for (var _len = arguments.length, programs = Array(_len), _key = 0; _key < _len; _key++) {
1502 programs[_key] = arguments[_key];
1503 }
1504
1505 var ps = [];
1506 var _iteratorNormalCompletion = true;
1507 var _didIteratorError = false;
1508 var _iteratorError = undefined;
1509
1510 try {
1511 for (var _iterator = programs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
1512 var output = _step.value;
1513
1514 var program = void 0,
1515 init = void 0,
1516 a = void 0,
1517 b = void 0;
1518 if (typeof output === "function") {
1519 program = output;
1520 a = b = function b($) {
1521 return $;
1522 };
1523 } else {
1524 program = output[0];
1525 init = output[1];
1526 a = output[2];
1527 b = output[3];
1528 }
1529
1530 var _channel = channel(),
1531 _channel2 = slicedToArray(_channel, 2),
1532 request = _channel2[0],
1533 respond = _channel2[1];
1534
1535 ps.push({
1536 init: init,
1537 program: program,
1538 request: composeAsyncTransformer(b, request),
1539 respond: composeAsyncTransformer(respond, a)
1540 });
1541 }
1542 } catch (err) {
1543 _didIteratorError = true;
1544 _iteratorError = err;
1545 } finally {
1546 try {
1547 if (!_iteratorNormalCompletion && _iterator.return) {
1548 _iterator.return();
1549 }
1550 } finally {
1551 if (_didIteratorError) {
1552 throw _iteratorError;
1553 }
1554 }
1555 }
1556
1557 return function () {
1558 var _ref = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(program) {
1559 var responds, passthrough;
1560 return regeneratorRuntime.wrap(function _callee$(_context) {
1561 while (1) {
1562 switch (_context.prev = _context.next) {
1563 case 0:
1564 responds = ps.map(function (_ref2) {
1565 var respond = _ref2.respond;
1566 return respond;
1567 });
1568 passthrough = createPassThrought.apply(undefined, toConsumableArray(responds));
1569
1570 passthrough(program);
1571 return _context.abrupt("return", ps.map(function (_ref3) {
1572 var init = _ref3.init,
1573 program = _ref3.program,
1574 request = _ref3.request;
1575 return program(init, request);
1576 }));
1577
1578 case 4:
1579 case "end":
1580 return _context.stop();
1581 }
1582 }
1583 }, _callee, _this$6);
1584 }));
1585
1586 return function (_x) {
1587 return _ref.apply(this, arguments);
1588 };
1589 }();
1590});
1591
1592var readline = {};
1593
1594var _this$7 = undefined;
1595
1596//
1597/*global process */
1598readline.createInterface = readline.createInterface || function () {
1599 console.warn("readline has no browser equivalent");
1600 return {
1601 question: function question() {
1602 console.warn("readline has no browser equivalent");
1603 }
1604 };
1605}; //Shim included in browser
1606
1607var rl = readline.createInterface && readline.createInterface({
1608 input: process.stdin,
1609 output: process.stdout
1610});
1611var get$1 = function get$$1() {
1612 return new Promise(function (resolve) {
1613 return rl.question(">", function (answer) {
1614 return resolve(answer);
1615 });
1616 });
1617};
1618var console$1 = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
1619 for (var _len = arguments.length, inputs = Array(_len), _key = 0; _key < _len; _key++) {
1620 inputs[_key] = arguments[_key];
1621 }
1622
1623 var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, input;
1624
1625 return regeneratorRuntime.wrap(function _callee$(_context) {
1626 while (1) {
1627 switch (_context.prev = _context.next) {
1628 case 0:
1629
1630
1631 _iteratorNormalCompletion = true;
1632 _didIteratorError = false;
1633 _iteratorError = undefined;
1634 _context.prev = 4;
1635 _iterator = inputs[Symbol.iterator]();
1636
1637 case 6:
1638 if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
1639 _context.next = 17;
1640 break;
1641 }
1642
1643 input = _step.value;
1644 _context.t0 = input;
1645 _context.next = 11;
1646 return get$1();
1647
1648 case 11:
1649 _context.t1 = _context.sent;
1650 _context.next = 14;
1651 return (0, _context.t0)(_context.t1);
1652
1653 case 14:
1654 _iteratorNormalCompletion = true;
1655 _context.next = 6;
1656 break;
1657
1658 case 17:
1659 _context.next = 23;
1660 break;
1661
1662 case 19:
1663 _context.prev = 19;
1664 _context.t2 = _context["catch"](4);
1665 _didIteratorError = true;
1666 _iteratorError = _context.t2;
1667
1668 case 23:
1669 _context.prev = 23;
1670 _context.prev = 24;
1671
1672 if (!_iteratorNormalCompletion && _iterator.return) {
1673 _iterator.return();
1674 }
1675
1676 case 26:
1677 _context.prev = 26;
1678
1679 if (!_didIteratorError) {
1680 _context.next = 29;
1681 break;
1682 }
1683
1684 throw _iteratorError;
1685
1686 case 29:
1687 return _context.finish(26);
1688
1689 case 30:
1690 return _context.finish(23);
1691
1692 case 31:
1693 _context.next = 0;
1694 break;
1695
1696 case 33:
1697 case "end":
1698 return _context.stop();
1699 }
1700 }
1701 }, _callee, _this$7, [[4, 19, 23, 31], [24,, 26, 30]]);
1702}));
1703
1704var _this$8 = undefined;
1705
1706//
1707/*global process */
1708var pipe = (function () {
1709 var _ref = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(opts) {
1710 for (var _len = arguments.length, inputs = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1711 inputs[_key - 1] = arguments[_key];
1712 }
1713
1714 return regeneratorRuntime.wrap(function _callee2$(_context2) {
1715 while (1) {
1716 switch (_context2.prev = _context2.next) {
1717 case 0:
1718 // let options = {};
1719 // if (typeof opts === "object") {
1720 // options = Object.assign(options, opts);
1721 // } else {
1722 // inputs.unshift(opts);
1723 // }
1724 process.stdin.resume();
1725 //process.setEncoding("utf8");
1726 process.stdin.on("data", function () {
1727 var _ref2 = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(answer) {
1728 var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, input;
1729
1730 return regeneratorRuntime.wrap(function _callee$(_context) {
1731 while (1) {
1732 switch (_context.prev = _context.next) {
1733 case 0:
1734 _iteratorNormalCompletion = true;
1735 _didIteratorError = false;
1736 _iteratorError = undefined;
1737 _context.prev = 3;
1738 _iterator = inputs[Symbol.iterator]();
1739
1740 case 5:
1741 if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
1742 _context.next = 12;
1743 break;
1744 }
1745
1746 input = _step.value;
1747 _context.next = 9;
1748 return input(answer);
1749
1750 case 9:
1751 _iteratorNormalCompletion = true;
1752 _context.next = 5;
1753 break;
1754
1755 case 12:
1756 _context.next = 18;
1757 break;
1758
1759 case 14:
1760 _context.prev = 14;
1761 _context.t0 = _context["catch"](3);
1762 _didIteratorError = true;
1763 _iteratorError = _context.t0;
1764
1765 case 18:
1766 _context.prev = 18;
1767 _context.prev = 19;
1768
1769 if (!_iteratorNormalCompletion && _iterator.return) {
1770 _iterator.return();
1771 }
1772
1773 case 21:
1774 _context.prev = 21;
1775
1776 if (!_didIteratorError) {
1777 _context.next = 24;
1778 break;
1779 }
1780
1781 throw _iteratorError;
1782
1783 case 24:
1784 return _context.finish(21);
1785
1786 case 25:
1787 return _context.finish(18);
1788
1789 case 26:
1790 case "end":
1791 return _context.stop();
1792 }
1793 }
1794 }, _callee, _this$8, [[3, 14, 18, 26], [19,, 21, 25]]);
1795 }));
1796
1797 return function (_x2) {
1798 return _ref2.apply(this, arguments);
1799 };
1800 }());
1801
1802 case 2:
1803 case "end":
1804 return _context2.stop();
1805 }
1806 }
1807 }, _callee2, _this$8);
1808 }));
1809
1810 return function (_x) {
1811 return _ref.apply(this, arguments);
1812 };
1813})();
1814
1815/* eslint-disable */
1816/**
1817 * @typedef {Function} PairedRequest
1818 * @description a function that receives it's response from a paired PairedRespond function
1819 * @return {Promise<*>} response from respond reunction
1820 */
1821
1822/**
1823 * @typedef {Function} PairedRespond
1824 * @description a function that sends it's input to a paired PairedRequest function
1825 * @param {*} response - response for request function
1826 */
1827
1828/**
1829 * @typedef {Array} AsyncPair
1830 * @description a pair of paired PairedRequest and PairedRespond functions
1831 * @property {PairedRequest} 0 - request function
1832 * @property {PairedRespond} 1 - respond function
1833 */
1834
1835/**
1836 * @typedef {Function} AsyncTransformer
1837 * @description stateless asynchronous function that transforms input without side effects
1838 * @param {*} input - input
1839 * @return {*} transformed input
1840 */
1841
1842/**
1843 * @typedef {Function} Program
1844 * @description an iteractive program
1845 * @param {*} init
1846 * @param {PairedRequest} request - request function for input
1847 * @return {AsynchronousIterator} asynchronous iterator result
1848 */
1849
1850/**
1851 * @typedef {Function} AsyncRenderFunction
1852 * @description a function that renders values from a given [Asynchronous] Iterator
1853 * @param {AsynchronousIterator} program_return;
1854 */
1855
1856/**
1857 * @typedef {Array} PushPair
1858 * @description an iterator and a paired function to add to it
1859 * @property {AsynchornousIterator} 0 - iterator
1860 * @property {Function} 1 - function used to add to iterator
1861 */
1862
1863/**
1864 * @name channel
1865 * @description creates a pair of asynchronous functions used to transfer objects between programs
1866 * @returns {AsyncPair} array of paired functions
1867 * @example
1868 * import {channel} from "async-endpoint";
1869 * const [request, respond] = channel();
1870 * const main = async()=>{
1871 * setTimeout(()=>{
1872 * respond("hello");
1873 * })
1874 * console.log(await request());
1875 * }
1876 * main();
1877 * //logs "hello"
1878 */
1879
1880exports.channel = channel;
1881exports.composeProgram = composePrograms;
1882exports.map = map;
1883exports.forEach = forEach;
1884exports.filter = filter;
1885exports.reduce = reduce;
1886exports.pause = pause;
1887exports.composeAsyncTransformer = composeAsyncTransformer;
1888exports.createQueue = createQueue;
1889exports.createStack = createStack;
1890exports.programQueue = createProgramQueue;
1891exports.programStack = createProgramQueue;
1892exports.take = index;
1893exports.takeWhile = _while;
1894exports.identity = identity;
1895exports.continuousOutput = continuousOutput;
1896exports.renderer = createPassThrought;
1897exports.tee = tee;
1898exports.inputConsole = console$1;
1899exports.inputPipe = pipe;
1900
1901return exports;
1902
1903}({}));