UNPKG

55.3 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || function (d, b) {
3 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4 function __() { this.constructor = d; }
5 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
6};
7var symbol_observable_1 = require("symbol-observable");
8var NO = {};
9exports.NO = NO;
10function noop() { }
11function cp(a) {
12 var l = a.length;
13 var b = Array(l);
14 for (var i = 0; i < l; ++i)
15 b[i] = a[i];
16 return b;
17}
18function and(f1, f2) {
19 return function andFn(t) {
20 return f1(t) && f2(t);
21 };
22}
23function _try(c, t, u) {
24 try {
25 return c.f(t);
26 }
27 catch (e) {
28 u._e(e);
29 return NO;
30 }
31}
32var NO_IL = {
33 _n: noop,
34 _e: noop,
35 _c: noop,
36};
37exports.NO_IL = NO_IL;
38// mutates the input
39function internalizeProducer(producer) {
40 producer._start = function _start(il) {
41 il.next = il._n;
42 il.error = il._e;
43 il.complete = il._c;
44 this.start(il);
45 };
46 producer._stop = producer.stop;
47}
48var StreamSub = (function () {
49 function StreamSub(_stream, _listener) {
50 this._stream = _stream;
51 this._listener = _listener;
52 }
53 StreamSub.prototype.unsubscribe = function () {
54 this._stream.removeListener(this._listener);
55 };
56 return StreamSub;
57}());
58var Observer = (function () {
59 function Observer(_listener) {
60 this._listener = _listener;
61 }
62 Observer.prototype.next = function (value) {
63 this._listener._n(value);
64 };
65 Observer.prototype.error = function (err) {
66 this._listener._e(err);
67 };
68 Observer.prototype.complete = function () {
69 this._listener._c();
70 };
71 return Observer;
72}());
73var FromObservable = (function () {
74 function FromObservable(observable) {
75 this.type = 'fromObservable';
76 this.ins = observable;
77 this.active = false;
78 }
79 FromObservable.prototype._start = function (out) {
80 this.out = out;
81 this.active = true;
82 this._sub = this.ins.subscribe(new Observer(out));
83 if (!this.active)
84 this._sub.unsubscribe();
85 };
86 FromObservable.prototype._stop = function () {
87 if (this._sub)
88 this._sub.unsubscribe();
89 this.active = false;
90 };
91 return FromObservable;
92}());
93var Merge = (function () {
94 function Merge(insArr) {
95 this.type = 'merge';
96 this.insArr = insArr;
97 this.out = NO;
98 this.ac = 0;
99 }
100 Merge.prototype._start = function (out) {
101 this.out = out;
102 var s = this.insArr;
103 var L = s.length;
104 this.ac = L;
105 for (var i = 0; i < L; i++)
106 s[i]._add(this);
107 };
108 Merge.prototype._stop = function () {
109 var s = this.insArr;
110 var L = s.length;
111 for (var i = 0; i < L; i++)
112 s[i]._remove(this);
113 this.out = NO;
114 };
115 Merge.prototype._n = function (t) {
116 var u = this.out;
117 if (u === NO)
118 return;
119 u._n(t);
120 };
121 Merge.prototype._e = function (err) {
122 var u = this.out;
123 if (u === NO)
124 return;
125 u._e(err);
126 };
127 Merge.prototype._c = function () {
128 if (--this.ac <= 0) {
129 var u = this.out;
130 if (u === NO)
131 return;
132 u._c();
133 }
134 };
135 return Merge;
136}());
137var CombineListener = (function () {
138 function CombineListener(i, out, p) {
139 this.i = i;
140 this.out = out;
141 this.p = p;
142 p.ils.push(this);
143 }
144 CombineListener.prototype._n = function (t) {
145 var p = this.p, out = this.out;
146 if (out === NO)
147 return;
148 if (p.up(t, this.i)) {
149 var a = p.vals;
150 var l = a.length;
151 var b = Array(l);
152 for (var i = 0; i < l; ++i)
153 b[i] = a[i];
154 out._n(b);
155 }
156 };
157 CombineListener.prototype._e = function (err) {
158 var out = this.out;
159 if (out === NO)
160 return;
161 out._e(err);
162 };
163 CombineListener.prototype._c = function () {
164 var p = this.p;
165 if (p.out === NO)
166 return;
167 if (--p.Nc === 0)
168 p.out._c();
169 };
170 return CombineListener;
171}());
172var Combine = (function () {
173 function Combine(insArr) {
174 this.type = 'combine';
175 this.insArr = insArr;
176 this.out = NO;
177 this.ils = [];
178 this.Nc = this.Nn = 0;
179 this.vals = [];
180 }
181 Combine.prototype.up = function (t, i) {
182 var v = this.vals[i];
183 var Nn = !this.Nn ? 0 : v === NO ? --this.Nn : this.Nn;
184 this.vals[i] = t;
185 return Nn === 0;
186 };
187 Combine.prototype._start = function (out) {
188 this.out = out;
189 var s = this.insArr;
190 var n = this.Nc = this.Nn = s.length;
191 var vals = this.vals = new Array(n);
192 if (n === 0) {
193 out._n([]);
194 out._c();
195 }
196 else {
197 for (var i = 0; i < n; i++) {
198 vals[i] = NO;
199 s[i]._add(new CombineListener(i, out, this));
200 }
201 }
202 };
203 Combine.prototype._stop = function () {
204 var s = this.insArr;
205 var n = s.length;
206 var ils = this.ils;
207 for (var i = 0; i < n; i++)
208 s[i]._remove(ils[i]);
209 this.out = NO;
210 this.ils = [];
211 this.vals = [];
212 };
213 return Combine;
214}());
215var FromArray = (function () {
216 function FromArray(a) {
217 this.type = 'fromArray';
218 this.a = a;
219 }
220 FromArray.prototype._start = function (out) {
221 var a = this.a;
222 for (var i = 0, n = a.length; i < n; i++)
223 out._n(a[i]);
224 out._c();
225 };
226 FromArray.prototype._stop = function () {
227 };
228 return FromArray;
229}());
230var FromPromise = (function () {
231 function FromPromise(p) {
232 this.type = 'fromPromise';
233 this.on = false;
234 this.p = p;
235 }
236 FromPromise.prototype._start = function (out) {
237 var prod = this;
238 this.on = true;
239 this.p.then(function (v) {
240 if (prod.on) {
241 out._n(v);
242 out._c();
243 }
244 }, function (e) {
245 out._e(e);
246 }).then(noop, function (err) {
247 setTimeout(function () { throw err; });
248 });
249 };
250 FromPromise.prototype._stop = function () {
251 this.on = false;
252 };
253 return FromPromise;
254}());
255var Periodic = (function () {
256 function Periodic(period) {
257 this.type = 'periodic';
258 this.period = period;
259 this.intervalID = -1;
260 this.i = 0;
261 }
262 Periodic.prototype._start = function (out) {
263 var self = this;
264 function intervalHandler() { out._n(self.i++); }
265 this.intervalID = setInterval(intervalHandler, this.period);
266 };
267 Periodic.prototype._stop = function () {
268 if (this.intervalID !== -1)
269 clearInterval(this.intervalID);
270 this.intervalID = -1;
271 this.i = 0;
272 };
273 return Periodic;
274}());
275var Debug = (function () {
276 function Debug(ins, arg) {
277 this.type = 'debug';
278 this.ins = ins;
279 this.out = NO;
280 this.s = noop;
281 this.l = '';
282 if (typeof arg === 'string')
283 this.l = arg;
284 else if (typeof arg === 'function')
285 this.s = arg;
286 }
287 Debug.prototype._start = function (out) {
288 this.out = out;
289 this.ins._add(this);
290 };
291 Debug.prototype._stop = function () {
292 this.ins._remove(this);
293 this.out = NO;
294 };
295 Debug.prototype._n = function (t) {
296 var u = this.out;
297 if (u === NO)
298 return;
299 var s = this.s, l = this.l;
300 if (s !== noop) {
301 try {
302 s(t);
303 }
304 catch (e) {
305 u._e(e);
306 }
307 }
308 else if (l)
309 console.log(l + ':', t);
310 else
311 console.log(t);
312 u._n(t);
313 };
314 Debug.prototype._e = function (err) {
315 var u = this.out;
316 if (u === NO)
317 return;
318 u._e(err);
319 };
320 Debug.prototype._c = function () {
321 var u = this.out;
322 if (u === NO)
323 return;
324 u._c();
325 };
326 return Debug;
327}());
328var Drop = (function () {
329 function Drop(max, ins) {
330 this.type = 'drop';
331 this.ins = ins;
332 this.out = NO;
333 this.max = max;
334 this.dropped = 0;
335 }
336 Drop.prototype._start = function (out) {
337 this.out = out;
338 this.dropped = 0;
339 this.ins._add(this);
340 };
341 Drop.prototype._stop = function () {
342 this.ins._remove(this);
343 this.out = NO;
344 };
345 Drop.prototype._n = function (t) {
346 var u = this.out;
347 if (u === NO)
348 return;
349 if (this.dropped++ >= this.max)
350 u._n(t);
351 };
352 Drop.prototype._e = function (err) {
353 var u = this.out;
354 if (u === NO)
355 return;
356 u._e(err);
357 };
358 Drop.prototype._c = function () {
359 var u = this.out;
360 if (u === NO)
361 return;
362 u._c();
363 };
364 return Drop;
365}());
366var EndWhenListener = (function () {
367 function EndWhenListener(out, op) {
368 this.out = out;
369 this.op = op;
370 }
371 EndWhenListener.prototype._n = function () {
372 this.op.end();
373 };
374 EndWhenListener.prototype._e = function (err) {
375 this.out._e(err);
376 };
377 EndWhenListener.prototype._c = function () {
378 this.op.end();
379 };
380 return EndWhenListener;
381}());
382var EndWhen = (function () {
383 function EndWhen(o, ins) {
384 this.type = 'endWhen';
385 this.ins = ins;
386 this.out = NO;
387 this.o = o;
388 this.oil = NO_IL;
389 }
390 EndWhen.prototype._start = function (out) {
391 this.out = out;
392 this.o._add(this.oil = new EndWhenListener(out, this));
393 this.ins._add(this);
394 };
395 EndWhen.prototype._stop = function () {
396 this.ins._remove(this);
397 this.o._remove(this.oil);
398 this.out = NO;
399 this.oil = NO_IL;
400 };
401 EndWhen.prototype.end = function () {
402 var u = this.out;
403 if (u === NO)
404 return;
405 u._c();
406 };
407 EndWhen.prototype._n = function (t) {
408 var u = this.out;
409 if (u === NO)
410 return;
411 u._n(t);
412 };
413 EndWhen.prototype._e = function (err) {
414 var u = this.out;
415 if (u === NO)
416 return;
417 u._e(err);
418 };
419 EndWhen.prototype._c = function () {
420 this.end();
421 };
422 return EndWhen;
423}());
424var Filter = (function () {
425 function Filter(passes, ins) {
426 this.type = 'filter';
427 this.ins = ins;
428 this.out = NO;
429 this.f = passes;
430 }
431 Filter.prototype._start = function (out) {
432 this.out = out;
433 this.ins._add(this);
434 };
435 Filter.prototype._stop = function () {
436 this.ins._remove(this);
437 this.out = NO;
438 };
439 Filter.prototype._n = function (t) {
440 var u = this.out;
441 if (u === NO)
442 return;
443 var r = _try(this, t, u);
444 if (r === NO || !r)
445 return;
446 u._n(t);
447 };
448 Filter.prototype._e = function (err) {
449 var u = this.out;
450 if (u === NO)
451 return;
452 u._e(err);
453 };
454 Filter.prototype._c = function () {
455 var u = this.out;
456 if (u === NO)
457 return;
458 u._c();
459 };
460 return Filter;
461}());
462var FlattenListener = (function () {
463 function FlattenListener(out, op) {
464 this.out = out;
465 this.op = op;
466 }
467 FlattenListener.prototype._n = function (t) {
468 this.out._n(t);
469 };
470 FlattenListener.prototype._e = function (err) {
471 this.out._e(err);
472 };
473 FlattenListener.prototype._c = function () {
474 this.op.inner = NO;
475 this.op.less();
476 };
477 return FlattenListener;
478}());
479var Flatten = (function () {
480 function Flatten(ins) {
481 this.type = 'flatten';
482 this.ins = ins;
483 this.out = NO;
484 this.open = true;
485 this.inner = NO;
486 this.il = NO_IL;
487 }
488 Flatten.prototype._start = function (out) {
489 this.out = out;
490 this.open = true;
491 this.inner = NO;
492 this.il = NO_IL;
493 this.ins._add(this);
494 };
495 Flatten.prototype._stop = function () {
496 this.ins._remove(this);
497 if (this.inner !== NO)
498 this.inner._remove(this.il);
499 this.out = NO;
500 this.open = true;
501 this.inner = NO;
502 this.il = NO_IL;
503 };
504 Flatten.prototype.less = function () {
505 var u = this.out;
506 if (u === NO)
507 return;
508 if (!this.open && this.inner === NO)
509 u._c();
510 };
511 Flatten.prototype._n = function (s) {
512 var u = this.out;
513 if (u === NO)
514 return;
515 var _a = this, inner = _a.inner, il = _a.il;
516 if (inner !== NO && il !== NO_IL)
517 inner._remove(il);
518 (this.inner = s)._add(this.il = new FlattenListener(u, this));
519 };
520 Flatten.prototype._e = function (err) {
521 var u = this.out;
522 if (u === NO)
523 return;
524 u._e(err);
525 };
526 Flatten.prototype._c = function () {
527 this.open = false;
528 this.less();
529 };
530 return Flatten;
531}());
532var Fold = (function () {
533 function Fold(f, seed, ins) {
534 var _this = this;
535 this.type = 'fold';
536 this.ins = ins;
537 this.out = NO;
538 this.f = function (t) { return f(_this.acc, t); };
539 this.acc = this.seed = seed;
540 }
541 Fold.prototype._start = function (out) {
542 this.out = out;
543 this.acc = this.seed;
544 out._n(this.acc);
545 this.ins._add(this);
546 };
547 Fold.prototype._stop = function () {
548 this.ins._remove(this);
549 this.out = NO;
550 this.acc = this.seed;
551 };
552 Fold.prototype._n = function (t) {
553 var u = this.out;
554 if (u === NO)
555 return;
556 var r = _try(this, t, u);
557 if (r === NO)
558 return;
559 u._n(this.acc = r);
560 };
561 Fold.prototype._e = function (err) {
562 var u = this.out;
563 if (u === NO)
564 return;
565 u._e(err);
566 };
567 Fold.prototype._c = function () {
568 var u = this.out;
569 if (u === NO)
570 return;
571 u._c();
572 };
573 return Fold;
574}());
575var Last = (function () {
576 function Last(ins) {
577 this.type = 'last';
578 this.ins = ins;
579 this.out = NO;
580 this.has = false;
581 this.val = NO;
582 }
583 Last.prototype._start = function (out) {
584 this.out = out;
585 this.has = false;
586 this.ins._add(this);
587 };
588 Last.prototype._stop = function () {
589 this.ins._remove(this);
590 this.out = NO;
591 this.val = NO;
592 };
593 Last.prototype._n = function (t) {
594 this.has = true;
595 this.val = t;
596 };
597 Last.prototype._e = function (err) {
598 var u = this.out;
599 if (u === NO)
600 return;
601 u._e(err);
602 };
603 Last.prototype._c = function () {
604 var u = this.out;
605 if (u === NO)
606 return;
607 if (this.has) {
608 u._n(this.val);
609 u._c();
610 }
611 else
612 u._e(new Error('last() failed because input stream completed'));
613 };
614 return Last;
615}());
616var MapFlattenListener = (function () {
617 function MapFlattenListener(out, op) {
618 this.out = out;
619 this.op = op;
620 }
621 MapFlattenListener.prototype._n = function (r) {
622 this.out._n(r);
623 };
624 MapFlattenListener.prototype._e = function (err) {
625 this.out._e(err);
626 };
627 MapFlattenListener.prototype._c = function () {
628 this.op.inner = NO;
629 this.op.less();
630 };
631 return MapFlattenListener;
632}());
633var MapFlatten = (function () {
634 function MapFlatten(mapOp) {
635 this.type = mapOp.type + "+flatten";
636 this.ins = mapOp.ins;
637 this.out = NO;
638 this.mapOp = mapOp;
639 this.inner = NO;
640 this.il = NO_IL;
641 this.open = true;
642 }
643 MapFlatten.prototype._start = function (out) {
644 this.out = out;
645 this.inner = NO;
646 this.il = NO_IL;
647 this.open = true;
648 this.mapOp.ins._add(this);
649 };
650 MapFlatten.prototype._stop = function () {
651 this.mapOp.ins._remove(this);
652 if (this.inner !== NO)
653 this.inner._remove(this.il);
654 this.out = NO;
655 this.inner = NO;
656 this.il = NO_IL;
657 };
658 MapFlatten.prototype.less = function () {
659 if (!this.open && this.inner === NO) {
660 var u = this.out;
661 if (u === NO)
662 return;
663 u._c();
664 }
665 };
666 MapFlatten.prototype._n = function (v) {
667 var u = this.out;
668 if (u === NO)
669 return;
670 var _a = this, inner = _a.inner, il = _a.il;
671 var s = _try(this.mapOp, v, u);
672 if (s === NO)
673 return;
674 if (inner !== NO && il !== NO_IL)
675 inner._remove(il);
676 (this.inner = s)._add(this.il = new MapFlattenListener(u, this));
677 };
678 MapFlatten.prototype._e = function (err) {
679 var u = this.out;
680 if (u === NO)
681 return;
682 u._e(err);
683 };
684 MapFlatten.prototype._c = function () {
685 this.open = false;
686 this.less();
687 };
688 return MapFlatten;
689}());
690var MapOp = (function () {
691 function MapOp(project, ins) {
692 this.type = 'map';
693 this.ins = ins;
694 this.out = NO;
695 this.f = project;
696 }
697 MapOp.prototype._start = function (out) {
698 this.out = out;
699 this.ins._add(this);
700 };
701 MapOp.prototype._stop = function () {
702 this.ins._remove(this);
703 this.out = NO;
704 };
705 MapOp.prototype._n = function (t) {
706 var u = this.out;
707 if (u === NO)
708 return;
709 var r = _try(this, t, u);
710 if (r === NO)
711 return;
712 u._n(r);
713 };
714 MapOp.prototype._e = function (err) {
715 var u = this.out;
716 if (u === NO)
717 return;
718 u._e(err);
719 };
720 MapOp.prototype._c = function () {
721 var u = this.out;
722 if (u === NO)
723 return;
724 u._c();
725 };
726 return MapOp;
727}());
728var FilterMapFusion = (function (_super) {
729 __extends(FilterMapFusion, _super);
730 function FilterMapFusion(passes, project, ins) {
731 var _this = _super.call(this, project, ins) || this;
732 _this.type = 'filter+map';
733 _this.passes = passes;
734 return _this;
735 }
736 FilterMapFusion.prototype._n = function (t) {
737 if (!this.passes(t))
738 return;
739 var u = this.out;
740 if (u === NO)
741 return;
742 var r = _try(this, t, u);
743 if (r === NO)
744 return;
745 u._n(r);
746 };
747 return FilterMapFusion;
748}(MapOp));
749var Remember = (function () {
750 function Remember(ins) {
751 this.type = 'remember';
752 this.ins = ins;
753 this.out = NO;
754 }
755 Remember.prototype._start = function (out) {
756 this.out = out;
757 this.ins._add(out);
758 };
759 Remember.prototype._stop = function () {
760 this.ins._remove(this.out);
761 this.out = NO;
762 };
763 return Remember;
764}());
765var ReplaceError = (function () {
766 function ReplaceError(replacer, ins) {
767 this.type = 'replaceError';
768 this.ins = ins;
769 this.out = NO;
770 this.f = replacer;
771 }
772 ReplaceError.prototype._start = function (out) {
773 this.out = out;
774 this.ins._add(this);
775 };
776 ReplaceError.prototype._stop = function () {
777 this.ins._remove(this);
778 this.out = NO;
779 };
780 ReplaceError.prototype._n = function (t) {
781 var u = this.out;
782 if (u === NO)
783 return;
784 u._n(t);
785 };
786 ReplaceError.prototype._e = function (err) {
787 var u = this.out;
788 if (u === NO)
789 return;
790 try {
791 this.ins._remove(this);
792 (this.ins = this.f(err))._add(this);
793 }
794 catch (e) {
795 u._e(e);
796 }
797 };
798 ReplaceError.prototype._c = function () {
799 var u = this.out;
800 if (u === NO)
801 return;
802 u._c();
803 };
804 return ReplaceError;
805}());
806var StartWith = (function () {
807 function StartWith(ins, val) {
808 this.type = 'startWith';
809 this.ins = ins;
810 this.out = NO;
811 this.val = val;
812 }
813 StartWith.prototype._start = function (out) {
814 this.out = out;
815 this.out._n(this.val);
816 this.ins._add(out);
817 };
818 StartWith.prototype._stop = function () {
819 this.ins._remove(this.out);
820 this.out = NO;
821 };
822 return StartWith;
823}());
824var Take = (function () {
825 function Take(max, ins) {
826 this.type = 'take';
827 this.ins = ins;
828 this.out = NO;
829 this.max = max;
830 this.taken = 0;
831 }
832 Take.prototype._start = function (out) {
833 this.out = out;
834 this.taken = 0;
835 if (this.max <= 0)
836 out._c();
837 else
838 this.ins._add(this);
839 };
840 Take.prototype._stop = function () {
841 this.ins._remove(this);
842 this.out = NO;
843 };
844 Take.prototype._n = function (t) {
845 var u = this.out;
846 if (u === NO)
847 return;
848 var m = ++this.taken;
849 if (m < this.max)
850 u._n(t);
851 else if (m === this.max) {
852 u._n(t);
853 u._c();
854 }
855 };
856 Take.prototype._e = function (err) {
857 var u = this.out;
858 if (u === NO)
859 return;
860 u._e(err);
861 };
862 Take.prototype._c = function () {
863 var u = this.out;
864 if (u === NO)
865 return;
866 u._c();
867 };
868 return Take;
869}());
870var Stream = (function () {
871 function Stream(producer) {
872 this._prod = producer || NO;
873 this._ils = [];
874 this._stopID = NO;
875 this._dl = NO;
876 this._d = false;
877 this._target = NO;
878 this._err = NO;
879 }
880 Stream.prototype._n = function (t) {
881 var a = this._ils;
882 var L = a.length;
883 if (this._d)
884 this._dl._n(t);
885 if (L == 1)
886 a[0]._n(t);
887 else if (L == 0)
888 return;
889 else {
890 var b = cp(a);
891 for (var i = 0; i < L; i++)
892 b[i]._n(t);
893 }
894 };
895 Stream.prototype._e = function (err) {
896 if (this._err !== NO)
897 return;
898 this._err = err;
899 var a = this._ils;
900 var L = a.length;
901 this._x();
902 if (this._d)
903 this._dl._e(err);
904 if (L == 1)
905 a[0]._e(err);
906 else if (L == 0)
907 return;
908 else {
909 var b = cp(a);
910 for (var i = 0; i < L; i++)
911 b[i]._e(err);
912 }
913 if (!this._d && L == 0)
914 throw this._err;
915 };
916 Stream.prototype._c = function () {
917 var a = this._ils;
918 var L = a.length;
919 this._x();
920 if (this._d)
921 this._dl._c();
922 if (L == 1)
923 a[0]._c();
924 else if (L == 0)
925 return;
926 else {
927 var b = cp(a);
928 for (var i = 0; i < L; i++)
929 b[i]._c();
930 }
931 };
932 Stream.prototype._x = function () {
933 if (this._ils.length === 0)
934 return;
935 if (this._prod !== NO)
936 this._prod._stop();
937 this._err = NO;
938 this._ils = [];
939 };
940 Stream.prototype._stopNow = function () {
941 // WARNING: code that calls this method should
942 // first check if this._prod is valid (not `NO`)
943 this._prod._stop();
944 this._err = NO;
945 this._stopID = NO;
946 };
947 Stream.prototype._add = function (il) {
948 var ta = this._target;
949 if (ta !== NO)
950 return ta._add(il);
951 var a = this._ils;
952 a.push(il);
953 if (a.length > 1)
954 return;
955 if (this._stopID !== NO) {
956 clearTimeout(this._stopID);
957 this._stopID = NO;
958 }
959 else {
960 var p = this._prod;
961 if (p !== NO)
962 p._start(this);
963 }
964 };
965 Stream.prototype._remove = function (il) {
966 var _this = this;
967 var ta = this._target;
968 if (ta !== NO)
969 return ta._remove(il);
970 var a = this._ils;
971 var i = a.indexOf(il);
972 if (i > -1) {
973 a.splice(i, 1);
974 if (this._prod !== NO && a.length <= 0) {
975 this._err = NO;
976 this._stopID = setTimeout(function () { return _this._stopNow(); });
977 }
978 else if (a.length === 1) {
979 this._pruneCycles();
980 }
981 }
982 };
983 // If all paths stemming from `this` stream eventually end at `this`
984 // stream, then we remove the single listener of `this` stream, to
985 // force it to end its execution and dispose resources. This method
986 // assumes as a precondition that this._ils has just one listener.
987 Stream.prototype._pruneCycles = function () {
988 if (this._hasNoSinks(this, []))
989 this._remove(this._ils[0]);
990 };
991 // Checks whether *there is no* path starting from `x` that leads to an end
992 // listener (sink) in the stream graph, following edges A->B where B is a
993 // listener of A. This means these paths constitute a cycle somehow. Is given
994 // a trace of all visited nodes so far.
995 Stream.prototype._hasNoSinks = function (x, trace) {
996 if (trace.indexOf(x) !== -1)
997 return true;
998 else if (x.out === this)
999 return true;
1000 else if (x.out && x.out !== NO)
1001 return this._hasNoSinks(x.out, trace.concat(x));
1002 else if (x._ils) {
1003 for (var i = 0, N = x._ils.length; i < N; i++)
1004 if (!this._hasNoSinks(x._ils[i], trace.concat(x)))
1005 return false;
1006 return true;
1007 }
1008 else
1009 return false;
1010 };
1011 Stream.prototype.ctor = function () {
1012 return this instanceof MemoryStream ? MemoryStream : Stream;
1013 };
1014 /**
1015 * Adds a Listener to the Stream.
1016 *
1017 * @param {Listener} listener
1018 */
1019 Stream.prototype.addListener = function (listener) {
1020 listener._n = listener.next || noop;
1021 listener._e = listener.error || noop;
1022 listener._c = listener.complete || noop;
1023 this._add(listener);
1024 };
1025 /**
1026 * Removes a Listener from the Stream, assuming the Listener was added to it.
1027 *
1028 * @param {Listener<T>} listener
1029 */
1030 Stream.prototype.removeListener = function (listener) {
1031 this._remove(listener);
1032 };
1033 /**
1034 * Adds a Listener to the Stream returning a Subscription to remove that
1035 * listener.
1036 *
1037 * @param {Listener} listener
1038 * @returns {Subscription}
1039 */
1040 Stream.prototype.subscribe = function (listener) {
1041 this.addListener(listener);
1042 return new StreamSub(this, listener);
1043 };
1044 /**
1045 * Add interop between most.js and RxJS 5
1046 *
1047 * @returns {Stream}
1048 */
1049 Stream.prototype[symbol_observable_1.default] = function () {
1050 return this;
1051 };
1052 /**
1053 * Creates a new Stream given a Producer.
1054 *
1055 * @factory true
1056 * @param {Producer} producer An optional Producer that dictates how to
1057 * start, generate events, and stop the Stream.
1058 * @return {Stream}
1059 */
1060 Stream.create = function (producer) {
1061 if (producer) {
1062 if (typeof producer.start !== 'function'
1063 || typeof producer.stop !== 'function')
1064 throw new Error('producer requires both start and stop functions');
1065 internalizeProducer(producer); // mutates the input
1066 }
1067 return new Stream(producer);
1068 };
1069 /**
1070 * Creates a new MemoryStream given a Producer.
1071 *
1072 * @factory true
1073 * @param {Producer} producer An optional Producer that dictates how to
1074 * start, generate events, and stop the Stream.
1075 * @return {MemoryStream}
1076 */
1077 Stream.createWithMemory = function (producer) {
1078 if (producer)
1079 internalizeProducer(producer); // mutates the input
1080 return new MemoryStream(producer);
1081 };
1082 /**
1083 * Creates a Stream that does nothing when started. It never emits any event.
1084 *
1085 * Marble diagram:
1086 *
1087 * ```text
1088 * never
1089 * -----------------------
1090 * ```
1091 *
1092 * @factory true
1093 * @return {Stream}
1094 */
1095 Stream.never = function () {
1096 return new Stream({ _start: noop, _stop: noop });
1097 };
1098 /**
1099 * Creates a Stream that immediately emits the "complete" notification when
1100 * started, and that's it.
1101 *
1102 * Marble diagram:
1103 *
1104 * ```text
1105 * empty
1106 * -|
1107 * ```
1108 *
1109 * @factory true
1110 * @return {Stream}
1111 */
1112 Stream.empty = function () {
1113 return new Stream({
1114 _start: function (il) { il._c(); },
1115 _stop: noop,
1116 });
1117 };
1118 /**
1119 * Creates a Stream that immediately emits an "error" notification with the
1120 * value you passed as the `error` argument when the stream starts, and that's
1121 * it.
1122 *
1123 * Marble diagram:
1124 *
1125 * ```text
1126 * throw(X)
1127 * -X
1128 * ```
1129 *
1130 * @factory true
1131 * @param error The error event to emit on the created stream.
1132 * @return {Stream}
1133 */
1134 Stream.throw = function (error) {
1135 return new Stream({
1136 _start: function (il) { il._e(error); },
1137 _stop: noop,
1138 });
1139 };
1140 /**
1141 * Creates a stream from an Array, Promise, or an Observable.
1142 *
1143 * @factory true
1144 * @param {Array|Promise|Observable} input The input to make a stream from.
1145 * @return {Stream}
1146 */
1147 Stream.from = function (input) {
1148 if (typeof input[symbol_observable_1.default] === 'function')
1149 return Stream.fromObservable(input);
1150 else if (typeof input.then === 'function')
1151 return Stream.fromPromise(input);
1152 else if (Array.isArray(input))
1153 return Stream.fromArray(input);
1154 throw new TypeError("Type of input to from() must be an Array, Promise, or Observable");
1155 };
1156 /**
1157 * Creates a Stream that immediately emits the arguments that you give to
1158 * *of*, then completes.
1159 *
1160 * Marble diagram:
1161 *
1162 * ```text
1163 * of(1,2,3)
1164 * 123|
1165 * ```
1166 *
1167 * @factory true
1168 * @param a The first value you want to emit as an event on the stream.
1169 * @param b The second value you want to emit as an event on the stream. One
1170 * or more of these values may be given as arguments.
1171 * @return {Stream}
1172 */
1173 Stream.of = function () {
1174 var items = [];
1175 for (var _i = 0; _i < arguments.length; _i++) {
1176 items[_i] = arguments[_i];
1177 }
1178 return Stream.fromArray(items);
1179 };
1180 /**
1181 * Converts an array to a stream. The returned stream will emit synchronously
1182 * all the items in the array, and then complete.
1183 *
1184 * Marble diagram:
1185 *
1186 * ```text
1187 * fromArray([1,2,3])
1188 * 123|
1189 * ```
1190 *
1191 * @factory true
1192 * @param {Array} array The array to be converted as a stream.
1193 * @return {Stream}
1194 */
1195 Stream.fromArray = function (array) {
1196 return new Stream(new FromArray(array));
1197 };
1198 /**
1199 * Converts a promise to a stream. The returned stream will emit the resolved
1200 * value of the promise, and then complete. However, if the promise is
1201 * rejected, the stream will emit the corresponding error.
1202 *
1203 * Marble diagram:
1204 *
1205 * ```text
1206 * fromPromise( ----42 )
1207 * -----------------42|
1208 * ```
1209 *
1210 * @factory true
1211 * @param {Promise} promise The promise to be converted as a stream.
1212 * @return {Stream}
1213 */
1214 Stream.fromPromise = function (promise) {
1215 return new Stream(new FromPromise(promise));
1216 };
1217 /**
1218 * Converts an Observable into a Stream.
1219 *
1220 * @factory true
1221 * @param {any} observable The observable to be converted as a stream.
1222 * @return {Stream}
1223 */
1224 Stream.fromObservable = function (obs) {
1225 if (obs.endWhen)
1226 return obs;
1227 return new Stream(new FromObservable(obs));
1228 };
1229 /**
1230 * Creates a stream that periodically emits incremental numbers, every
1231 * `period` milliseconds.
1232 *
1233 * Marble diagram:
1234 *
1235 * ```text
1236 * periodic(1000)
1237 * ---0---1---2---3---4---...
1238 * ```
1239 *
1240 * @factory true
1241 * @param {number} period The interval in milliseconds to use as a rate of
1242 * emission.
1243 * @return {Stream}
1244 */
1245 Stream.periodic = function (period) {
1246 return new Stream(new Periodic(period));
1247 };
1248 Stream.prototype._map = function (project) {
1249 var p = this._prod;
1250 var ctor = this.ctor();
1251 if (p instanceof Filter)
1252 return new ctor(new FilterMapFusion(p.f, project, p.ins));
1253 return new ctor(new MapOp(project, this));
1254 };
1255 /**
1256 * Transforms each event from the input Stream through a `project` function,
1257 * to get a Stream that emits those transformed events.
1258 *
1259 * Marble diagram:
1260 *
1261 * ```text
1262 * --1---3--5-----7------
1263 * map(i => i * 10)
1264 * --10--30-50----70-----
1265 * ```
1266 *
1267 * @param {Function} project A function of type `(t: T) => U` that takes event
1268 * `t` of type `T` from the input Stream and produces an event of type `U`, to
1269 * be emitted on the output Stream.
1270 * @return {Stream}
1271 */
1272 Stream.prototype.map = function (project) {
1273 return this._map(project);
1274 };
1275 /**
1276 * It's like `map`, but transforms each input event to always the same
1277 * constant value on the output Stream.
1278 *
1279 * Marble diagram:
1280 *
1281 * ```text
1282 * --1---3--5-----7-----
1283 * mapTo(10)
1284 * --10--10-10----10----
1285 * ```
1286 *
1287 * @param projectedValue A value to emit on the output Stream whenever the
1288 * input Stream emits any value.
1289 * @return {Stream}
1290 */
1291 Stream.prototype.mapTo = function (projectedValue) {
1292 var s = this.map(function () { return projectedValue; });
1293 var op = s._prod;
1294 op.type = op.type.replace('map', 'mapTo');
1295 return s;
1296 };
1297 /**
1298 * Only allows events that pass the test given by the `passes` argument.
1299 *
1300 * Each event from the input stream is given to the `passes` function. If the
1301 * function returns `true`, the event is forwarded to the output stream,
1302 * otherwise it is ignored and not forwarded.
1303 *
1304 * Marble diagram:
1305 *
1306 * ```text
1307 * --1---2--3-----4-----5---6--7-8--
1308 * filter(i => i % 2 === 0)
1309 * ------2--------4---------6----8--
1310 * ```
1311 *
1312 * @param {Function} passes A function of type `(t: T) +> boolean` that takes
1313 * an event from the input stream and checks if it passes, by returning a
1314 * boolean.
1315 * @return {Stream}
1316 */
1317 Stream.prototype.filter = function (passes) {
1318 var p = this._prod;
1319 if (p instanceof Filter)
1320 return new Stream(new Filter(and(p.f, passes), p.ins));
1321 return new Stream(new Filter(passes, this));
1322 };
1323 /**
1324 * Lets the first `amount` many events from the input stream pass to the
1325 * output stream, then makes the output stream complete.
1326 *
1327 * Marble diagram:
1328 *
1329 * ```text
1330 * --a---b--c----d---e--
1331 * take(3)
1332 * --a---b--c|
1333 * ```
1334 *
1335 * @param {number} amount How many events to allow from the input stream
1336 * before completing the output stream.
1337 * @return {Stream}
1338 */
1339 Stream.prototype.take = function (amount) {
1340 return new (this.ctor())(new Take(amount, this));
1341 };
1342 /**
1343 * Ignores the first `amount` many events from the input stream, and then
1344 * after that starts forwarding events from the input stream to the output
1345 * stream.
1346 *
1347 * Marble diagram:
1348 *
1349 * ```text
1350 * --a---b--c----d---e--
1351 * drop(3)
1352 * --------------d---e--
1353 * ```
1354 *
1355 * @param {number} amount How many events to ignore from the input stream
1356 * before forwarding all events from the input stream to the output stream.
1357 * @return {Stream}
1358 */
1359 Stream.prototype.drop = function (amount) {
1360 return new Stream(new Drop(amount, this));
1361 };
1362 /**
1363 * When the input stream completes, the output stream will emit the last event
1364 * emitted by the input stream, and then will also complete.
1365 *
1366 * Marble diagram:
1367 *
1368 * ```text
1369 * --a---b--c--d----|
1370 * last()
1371 * -----------------d|
1372 * ```
1373 *
1374 * @return {Stream}
1375 */
1376 Stream.prototype.last = function () {
1377 return new Stream(new Last(this));
1378 };
1379 /**
1380 * Prepends the given `initial` value to the sequence of events emitted by the
1381 * input stream. The returned stream is a MemoryStream, which means it is
1382 * already `remember()`'d.
1383 *
1384 * Marble diagram:
1385 *
1386 * ```text
1387 * ---1---2-----3---
1388 * startWith(0)
1389 * 0--1---2-----3---
1390 * ```
1391 *
1392 * @param initial The value or event to prepend.
1393 * @return {MemoryStream}
1394 */
1395 Stream.prototype.startWith = function (initial) {
1396 return new MemoryStream(new StartWith(this, initial));
1397 };
1398 /**
1399 * Uses another stream to determine when to complete the current stream.
1400 *
1401 * When the given `other` stream emits an event or completes, the output
1402 * stream will complete. Before that happens, the output stream will behaves
1403 * like the input stream.
1404 *
1405 * Marble diagram:
1406 *
1407 * ```text
1408 * ---1---2-----3--4----5----6---
1409 * endWhen( --------a--b--| )
1410 * ---1---2-----3--4--|
1411 * ```
1412 *
1413 * @param other Some other stream that is used to know when should the output
1414 * stream of this operator complete.
1415 * @return {Stream}
1416 */
1417 Stream.prototype.endWhen = function (other) {
1418 return new (this.ctor())(new EndWhen(other, this));
1419 };
1420 /**
1421 * "Folds" the stream onto itself.
1422 *
1423 * Combines events from the past throughout
1424 * the entire execution of the input stream, allowing you to accumulate them
1425 * together. It's essentially like `Array.prototype.reduce`. The returned
1426 * stream is a MemoryStream, which means it is already `remember()`'d.
1427 *
1428 * The output stream starts by emitting the `seed` which you give as argument.
1429 * Then, when an event happens on the input stream, it is combined with that
1430 * seed value through the `accumulate` function, and the output value is
1431 * emitted on the output stream. `fold` remembers that output value as `acc`
1432 * ("accumulator"), and then when a new input event `t` happens, `acc` will be
1433 * combined with that to produce the new `acc` and so forth.
1434 *
1435 * Marble diagram:
1436 *
1437 * ```text
1438 * ------1-----1--2----1----1------
1439 * fold((acc, x) => acc + x, 3)
1440 * 3-----4-----5--7----8----9------
1441 * ```
1442 *
1443 * @param {Function} accumulate A function of type `(acc: R, t: T) => R` that
1444 * takes the previous accumulated value `acc` and the incoming event from the
1445 * input stream and produces the new accumulated value.
1446 * @param seed The initial accumulated value, of type `R`.
1447 * @return {MemoryStream}
1448 */
1449 Stream.prototype.fold = function (accumulate, seed) {
1450 return new MemoryStream(new Fold(accumulate, seed, this));
1451 };
1452 /**
1453 * Replaces an error with another stream.
1454 *
1455 * When (and if) an error happens on the input stream, instead of forwarding
1456 * that error to the output stream, *replaceError* will call the `replace`
1457 * function which returns the stream that the output stream will replicate.
1458 * And, in case that new stream also emits an error, `replace` will be called
1459 * again to get another stream to start replicating.
1460 *
1461 * Marble diagram:
1462 *
1463 * ```text
1464 * --1---2-----3--4-----X
1465 * replaceError( () => --10--| )
1466 * --1---2-----3--4--------10--|
1467 * ```
1468 *
1469 * @param {Function} replace A function of type `(err) => Stream` that takes
1470 * the error that occurred on the input stream or on the previous replacement
1471 * stream and returns a new stream. The output stream will behave like the
1472 * stream that this function returns.
1473 * @return {Stream}
1474 */
1475 Stream.prototype.replaceError = function (replace) {
1476 return new (this.ctor())(new ReplaceError(replace, this));
1477 };
1478 /**
1479 * Flattens a "stream of streams", handling only one nested stream at a time
1480 * (no concurrency).
1481 *
1482 * If the input stream is a stream that emits streams, then this operator will
1483 * return an output stream which is a flat stream: emits regular events. The
1484 * flattening happens without concurrency. It works like this: when the input
1485 * stream emits a nested stream, *flatten* will start imitating that nested
1486 * one. However, as soon as the next nested stream is emitted on the input
1487 * stream, *flatten* will forget the previous nested one it was imitating, and
1488 * will start imitating the new nested one.
1489 *
1490 * Marble diagram:
1491 *
1492 * ```text
1493 * --+--------+---------------
1494 * \ \
1495 * \ ----1----2---3--
1496 * --a--b----c----d--------
1497 * flatten
1498 * -----a--b------1----2---3--
1499 * ```
1500 *
1501 * @return {Stream}
1502 */
1503 Stream.prototype.flatten = function () {
1504 var p = this._prod;
1505 return new Stream(p instanceof MapOp && !(p instanceof FilterMapFusion) ?
1506 new MapFlatten(p) :
1507 new Flatten(this));
1508 };
1509 /**
1510 * Passes the input stream to a custom operator, to produce an output stream.
1511 *
1512 * *compose* is a handy way of using an existing function in a chained style.
1513 * Instead of writing `outStream = f(inStream)` you can write
1514 * `outStream = inStream.compose(f)`.
1515 *
1516 * @param {function} operator A function that takes a stream as input and
1517 * returns a stream as well.
1518 * @return {Stream}
1519 */
1520 Stream.prototype.compose = function (operator) {
1521 return operator(this);
1522 };
1523 /**
1524 * Returns an output stream that behaves like the input stream, but also
1525 * remembers the most recent event that happens on the input stream, so that a
1526 * newly added listener will immediately receive that memorised event.
1527 *
1528 * @return {MemoryStream}
1529 */
1530 Stream.prototype.remember = function () {
1531 return new MemoryStream(new Remember(this));
1532 };
1533 /**
1534 * Returns an output stream that identically behaves like the input stream,
1535 * but also runs a `spy` function fo each event, to help you debug your app.
1536 *
1537 * *debug* takes a `spy` function as argument, and runs that for each event
1538 * happening on the input stream. If you don't provide the `spy` argument,
1539 * then *debug* will just `console.log` each event. This helps you to
1540 * understand the flow of events through some operator chain.
1541 *
1542 * Please note that if the output stream has no listeners, then it will not
1543 * start, which means `spy` will never run because no actual event happens in
1544 * that case.
1545 *
1546 * Marble diagram:
1547 *
1548 * ```text
1549 * --1----2-----3-----4--
1550 * debug
1551 * --1----2-----3-----4--
1552 * ```
1553 *
1554 * @param {function} labelOrSpy A string to use as the label when printing
1555 * debug information on the console, or a 'spy' function that takes an event
1556 * as argument, and does not need to return anything.
1557 * @return {Stream}
1558 */
1559 Stream.prototype.debug = function (labelOrSpy) {
1560 return new (this.ctor())(new Debug(this, labelOrSpy));
1561 };
1562 /**
1563 * *imitate* changes this current Stream to emit the same events that the
1564 * `other` given Stream does. This method returns nothing.
1565 *
1566 * This method exists to allow one thing: **circular dependency of streams**.
1567 * For instance, let's imagine that for some reason you need to create a
1568 * circular dependency where stream `first$` depends on stream `second$`
1569 * which in turn depends on `first$`:
1570 *
1571 * <!-- skip-example -->
1572 * ```js
1573 * import delay from 'xstream/extra/delay'
1574 *
1575 * var first$ = second$.map(x => x * 10).take(3);
1576 * var second$ = first$.map(x => x + 1).startWith(1).compose(delay(100));
1577 * ```
1578 *
1579 * However, that is invalid JavaScript, because `second$` is undefined
1580 * on the first line. This is how *imitate* can help solve it:
1581 *
1582 * ```js
1583 * import delay from 'xstream/extra/delay'
1584 *
1585 * var secondProxy$ = xs.create();
1586 * var first$ = secondProxy$.map(x => x * 10).take(3);
1587 * var second$ = first$.map(x => x + 1).startWith(1).compose(delay(100));
1588 * secondProxy$.imitate(second$);
1589 * ```
1590 *
1591 * We create `secondProxy$` before the others, so it can be used in the
1592 * declaration of `first$`. Then, after both `first$` and `second$` are
1593 * defined, we hook `secondProxy$` with `second$` with `imitate()` to tell
1594 * that they are "the same". `imitate` will not trigger the start of any
1595 * stream, it just binds `secondProxy$` and `second$` together.
1596 *
1597 * The following is an example where `imitate()` is important in Cycle.js
1598 * applications. A parent component contains some child components. A child
1599 * has an action stream which is given to the parent to define its state:
1600 *
1601 * <!-- skip-example -->
1602 * ```js
1603 * const childActionProxy$ = xs.create();
1604 * const parent = Parent({...sources, childAction$: childActionProxy$});
1605 * const childAction$ = parent.state$.map(s => s.child.action$).flatten();
1606 * childActionProxy$.imitate(childAction$);
1607 * ```
1608 *
1609 * Note, though, that **`imitate()` does not support MemoryStreams**. If we
1610 * would attempt to imitate a MemoryStream in a circular dependency, we would
1611 * either get a race condition (where the symptom would be "nothing happens")
1612 * or an infinite cyclic emission of values. It's useful to think about
1613 * MemoryStreams as cells in a spreadsheet. It doesn't make any sense to
1614 * define a spreadsheet cell `A1` with a formula that depends on `B1` and
1615 * cell `B1` defined with a formula that depends on `A1`.
1616 *
1617 * If you find yourself wanting to use `imitate()` with a
1618 * MemoryStream, you should rework your code around `imitate()` to use a
1619 * Stream instead. Look for the stream in the circular dependency that
1620 * represents an event stream, and that would be a candidate for creating a
1621 * proxy Stream which then imitates the target Stream.
1622 *
1623 * @param {Stream} target The other stream to imitate on the current one. Must
1624 * not be a MemoryStream.
1625 */
1626 Stream.prototype.imitate = function (target) {
1627 if (target instanceof MemoryStream)
1628 throw new Error('A MemoryStream was given to imitate(), but it only ' +
1629 'supports a Stream. Read more about this restriction here: ' +
1630 'https://github.com/staltz/xstream#faq');
1631 this._target = target;
1632 for (var ils = this._ils, N = ils.length, i = 0; i < N; i++)
1633 target._add(ils[i]);
1634 this._ils = [];
1635 };
1636 /**
1637 * Forces the Stream to emit the given value to its listeners.
1638 *
1639 * As the name indicates, if you use this, you are most likely doing something
1640 * The Wrong Way. Please try to understand the reactive way before using this
1641 * method. Use it only when you know what you are doing.
1642 *
1643 * @param value The "next" value you want to broadcast to all listeners of
1644 * this Stream.
1645 */
1646 Stream.prototype.shamefullySendNext = function (value) {
1647 this._n(value);
1648 };
1649 /**
1650 * Forces the Stream to emit the given error to its listeners.
1651 *
1652 * As the name indicates, if you use this, you are most likely doing something
1653 * The Wrong Way. Please try to understand the reactive way before using this
1654 * method. Use it only when you know what you are doing.
1655 *
1656 * @param {any} error The error you want to broadcast to all the listeners of
1657 * this Stream.
1658 */
1659 Stream.prototype.shamefullySendError = function (error) {
1660 this._e(error);
1661 };
1662 /**
1663 * Forces the Stream to emit the "completed" event to its listeners.
1664 *
1665 * As the name indicates, if you use this, you are most likely doing something
1666 * The Wrong Way. Please try to understand the reactive way before using this
1667 * method. Use it only when you know what you are doing.
1668 */
1669 Stream.prototype.shamefullySendComplete = function () {
1670 this._c();
1671 };
1672 /**
1673 * Adds a "debug" listener to the stream. There can only be one debug
1674 * listener, that's why this is 'setDebugListener'. To remove the debug
1675 * listener, just call setDebugListener(null).
1676 *
1677 * A debug listener is like any other listener. The only difference is that a
1678 * debug listener is "stealthy": its presence/absence does not trigger the
1679 * start/stop of the stream (or the producer inside the stream). This is
1680 * useful so you can inspect what is going on without changing the behavior
1681 * of the program. If you have an idle stream and you add a normal listener to
1682 * it, the stream will start executing. But if you set a debug listener on an
1683 * idle stream, it won't start executing (not until the first normal listener
1684 * is added).
1685 *
1686 * As the name indicates, we don't recommend using this method to build app
1687 * logic. In fact, in most cases the debug operator works just fine. Only use
1688 * this one if you know what you're doing.
1689 *
1690 * @param {Listener<T>} listener
1691 */
1692 Stream.prototype.setDebugListener = function (listener) {
1693 if (!listener) {
1694 this._d = false;
1695 this._dl = NO;
1696 }
1697 else {
1698 this._d = true;
1699 listener._n = listener.next || noop;
1700 listener._e = listener.error || noop;
1701 listener._c = listener.complete || noop;
1702 this._dl = listener;
1703 }
1704 };
1705 return Stream;
1706}());
1707/**
1708 * Blends multiple streams together, emitting events from all of them
1709 * concurrently.
1710 *
1711 * *merge* takes multiple streams as arguments, and creates a stream that
1712 * behaves like each of the argument streams, in parallel.
1713 *
1714 * Marble diagram:
1715 *
1716 * ```text
1717 * --1----2-----3--------4---
1718 * ----a-----b----c---d------
1719 * merge
1720 * --1-a--2--b--3-c---d--4---
1721 * ```
1722 *
1723 * @factory true
1724 * @param {Stream} stream1 A stream to merge together with other streams.
1725 * @param {Stream} stream2 A stream to merge together with other streams. Two
1726 * or more streams may be given as arguments.
1727 * @return {Stream}
1728 */
1729Stream.merge = function merge() {
1730 var streams = [];
1731 for (var _i = 0; _i < arguments.length; _i++) {
1732 streams[_i] = arguments[_i];
1733 }
1734 return new Stream(new Merge(streams));
1735};
1736/**
1737 * Combines multiple input streams together to return a stream whose events
1738 * are arrays that collect the latest events from each input stream.
1739 *
1740 * *combine* internally remembers the most recent event from each of the input
1741 * streams. When any of the input streams emits an event, that event together
1742 * with all the other saved events are combined into an array. That array will
1743 * be emitted on the output stream. It's essentially a way of joining together
1744 * the events from multiple streams.
1745 *
1746 * Marble diagram:
1747 *
1748 * ```text
1749 * --1----2-----3--------4---
1750 * ----a-----b-----c--d------
1751 * combine
1752 * ----1a-2a-2b-3b-3c-3d-4d--
1753 * ```
1754 *
1755 * Note: to minimize garbage collection, *combine* uses the same array
1756 * instance for each emission. If you need to compare emissions over time,
1757 * cache the values with `map` first:
1758 *
1759 * ```js
1760 * import pairwise from 'xstream/extra/pairwise'
1761 *
1762 * const stream1 = xs.of(1);
1763 * const stream2 = xs.of(2);
1764 *
1765 * xs.combine(stream1, stream2).map(
1766 * combinedEmissions => ([ ...combinedEmissions ])
1767 * ).compose(pairwise)
1768 * ```
1769 *
1770 * @factory true
1771 * @param {Stream} stream1 A stream to combine together with other streams.
1772 * @param {Stream} stream2 A stream to combine together with other streams.
1773 * Multiple streams, not just two, may be given as arguments.
1774 * @return {Stream}
1775 */
1776Stream.combine = function combine() {
1777 var streams = [];
1778 for (var _i = 0; _i < arguments.length; _i++) {
1779 streams[_i] = arguments[_i];
1780 }
1781 return new Stream(new Combine(streams));
1782};
1783exports.Stream = Stream;
1784var MemoryStream = (function (_super) {
1785 __extends(MemoryStream, _super);
1786 function MemoryStream(producer) {
1787 var _this = _super.call(this, producer) || this;
1788 _this._has = false;
1789 return _this;
1790 }
1791 MemoryStream.prototype._n = function (x) {
1792 this._v = x;
1793 this._has = true;
1794 _super.prototype._n.call(this, x);
1795 };
1796 MemoryStream.prototype._add = function (il) {
1797 var ta = this._target;
1798 if (ta !== NO)
1799 return ta._add(il);
1800 var a = this._ils;
1801 a.push(il);
1802 if (a.length > 1) {
1803 if (this._has)
1804 il._n(this._v);
1805 return;
1806 }
1807 if (this._stopID !== NO) {
1808 if (this._has)
1809 il._n(this._v);
1810 clearTimeout(this._stopID);
1811 this._stopID = NO;
1812 }
1813 else if (this._has)
1814 il._n(this._v);
1815 else {
1816 var p = this._prod;
1817 if (p !== NO)
1818 p._start(this);
1819 }
1820 };
1821 MemoryStream.prototype._stopNow = function () {
1822 this._has = false;
1823 _super.prototype._stopNow.call(this);
1824 };
1825 MemoryStream.prototype._x = function () {
1826 this._has = false;
1827 _super.prototype._x.call(this);
1828 };
1829 MemoryStream.prototype.map = function (project) {
1830 return this._map(project);
1831 };
1832 MemoryStream.prototype.mapTo = function (projectedValue) {
1833 return _super.prototype.mapTo.call(this, projectedValue);
1834 };
1835 MemoryStream.prototype.take = function (amount) {
1836 return _super.prototype.take.call(this, amount);
1837 };
1838 MemoryStream.prototype.endWhen = function (other) {
1839 return _super.prototype.endWhen.call(this, other);
1840 };
1841 MemoryStream.prototype.replaceError = function (replace) {
1842 return _super.prototype.replaceError.call(this, replace);
1843 };
1844 MemoryStream.prototype.remember = function () {
1845 return this;
1846 };
1847 MemoryStream.prototype.debug = function (labelOrSpy) {
1848 return _super.prototype.debug.call(this, labelOrSpy);
1849 };
1850 return MemoryStream;
1851}(Stream));
1852exports.MemoryStream = MemoryStream;
1853Object.defineProperty(exports, "__esModule", { value: true });
1854exports.default = Stream;
1855//# sourceMappingURL=index.js.map
\No newline at end of file