1 | ;
|
2 | var __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 | };
|
7 | var symbol_observable_1 = require("symbol-observable");
|
8 | var NO = {};
|
9 | exports.NO = NO;
|
10 | function noop() { }
|
11 | function 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 | }
|
18 | function and(f1, f2) {
|
19 | return function andFn(t) {
|
20 | return f1(t) && f2(t);
|
21 | };
|
22 | }
|
23 | function _try(c, t, u) {
|
24 | try {
|
25 | return c.f(t);
|
26 | }
|
27 | catch (e) {
|
28 | u._e(e);
|
29 | return NO;
|
30 | }
|
31 | }
|
32 | var NO_IL = {
|
33 | _n: noop,
|
34 | _e: noop,
|
35 | _c: noop,
|
36 | };
|
37 | exports.NO_IL = NO_IL;
|
38 | // mutates the input
|
39 | function 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 | }
|
48 | var 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 | }());
|
58 | var 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 | }());
|
73 | var 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 | }());
|
93 | var 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 | }());
|
137 | var 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 | }());
|
172 | var 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 | }());
|
215 | var 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 | }());
|
230 | var 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 | }());
|
255 | var 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 | }());
|
275 | var 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 | }());
|
328 | var 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 | }());
|
366 | var 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 | }());
|
382 | var 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 | }());
|
424 | var 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 | }());
|
462 | var 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 | }());
|
479 | var 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 | }());
|
532 | var 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 | }());
|
575 | var 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 | }());
|
616 | var MapOp = (function () {
|
617 | function MapOp(project, ins) {
|
618 | this.type = 'map';
|
619 | this.ins = ins;
|
620 | this.out = NO;
|
621 | this.f = project;
|
622 | }
|
623 | MapOp.prototype._start = function (out) {
|
624 | this.out = out;
|
625 | this.ins._add(this);
|
626 | };
|
627 | MapOp.prototype._stop = function () {
|
628 | this.ins._remove(this);
|
629 | this.out = NO;
|
630 | };
|
631 | MapOp.prototype._n = function (t) {
|
632 | var u = this.out;
|
633 | if (u === NO)
|
634 | return;
|
635 | var r = _try(this, t, u);
|
636 | if (r === NO)
|
637 | return;
|
638 | u._n(r);
|
639 | };
|
640 | MapOp.prototype._e = function (err) {
|
641 | var u = this.out;
|
642 | if (u === NO)
|
643 | return;
|
644 | u._e(err);
|
645 | };
|
646 | MapOp.prototype._c = function () {
|
647 | var u = this.out;
|
648 | if (u === NO)
|
649 | return;
|
650 | u._c();
|
651 | };
|
652 | return MapOp;
|
653 | }());
|
654 | var Remember = (function () {
|
655 | function Remember(ins) {
|
656 | this.type = 'remember';
|
657 | this.ins = ins;
|
658 | this.out = NO;
|
659 | }
|
660 | Remember.prototype._start = function (out) {
|
661 | this.out = out;
|
662 | this.ins._add(out);
|
663 | };
|
664 | Remember.prototype._stop = function () {
|
665 | this.ins._remove(this.out);
|
666 | this.out = NO;
|
667 | };
|
668 | return Remember;
|
669 | }());
|
670 | var ReplaceError = (function () {
|
671 | function ReplaceError(replacer, ins) {
|
672 | this.type = 'replaceError';
|
673 | this.ins = ins;
|
674 | this.out = NO;
|
675 | this.f = replacer;
|
676 | }
|
677 | ReplaceError.prototype._start = function (out) {
|
678 | this.out = out;
|
679 | this.ins._add(this);
|
680 | };
|
681 | ReplaceError.prototype._stop = function () {
|
682 | this.ins._remove(this);
|
683 | this.out = NO;
|
684 | };
|
685 | ReplaceError.prototype._n = function (t) {
|
686 | var u = this.out;
|
687 | if (u === NO)
|
688 | return;
|
689 | u._n(t);
|
690 | };
|
691 | ReplaceError.prototype._e = function (err) {
|
692 | var u = this.out;
|
693 | if (u === NO)
|
694 | return;
|
695 | try {
|
696 | this.ins._remove(this);
|
697 | (this.ins = this.f(err))._add(this);
|
698 | }
|
699 | catch (e) {
|
700 | u._e(e);
|
701 | }
|
702 | };
|
703 | ReplaceError.prototype._c = function () {
|
704 | var u = this.out;
|
705 | if (u === NO)
|
706 | return;
|
707 | u._c();
|
708 | };
|
709 | return ReplaceError;
|
710 | }());
|
711 | var StartWith = (function () {
|
712 | function StartWith(ins, val) {
|
713 | this.type = 'startWith';
|
714 | this.ins = ins;
|
715 | this.out = NO;
|
716 | this.val = val;
|
717 | }
|
718 | StartWith.prototype._start = function (out) {
|
719 | this.out = out;
|
720 | this.out._n(this.val);
|
721 | this.ins._add(out);
|
722 | };
|
723 | StartWith.prototype._stop = function () {
|
724 | this.ins._remove(this.out);
|
725 | this.out = NO;
|
726 | };
|
727 | return StartWith;
|
728 | }());
|
729 | var Take = (function () {
|
730 | function Take(max, ins) {
|
731 | this.type = 'take';
|
732 | this.ins = ins;
|
733 | this.out = NO;
|
734 | this.max = max;
|
735 | this.taken = 0;
|
736 | }
|
737 | Take.prototype._start = function (out) {
|
738 | this.out = out;
|
739 | this.taken = 0;
|
740 | if (this.max <= 0)
|
741 | out._c();
|
742 | else
|
743 | this.ins._add(this);
|
744 | };
|
745 | Take.prototype._stop = function () {
|
746 | this.ins._remove(this);
|
747 | this.out = NO;
|
748 | };
|
749 | Take.prototype._n = function (t) {
|
750 | var u = this.out;
|
751 | if (u === NO)
|
752 | return;
|
753 | var m = ++this.taken;
|
754 | if (m < this.max)
|
755 | u._n(t);
|
756 | else if (m === this.max) {
|
757 | u._n(t);
|
758 | u._c();
|
759 | }
|
760 | };
|
761 | Take.prototype._e = function (err) {
|
762 | var u = this.out;
|
763 | if (u === NO)
|
764 | return;
|
765 | u._e(err);
|
766 | };
|
767 | Take.prototype._c = function () {
|
768 | var u = this.out;
|
769 | if (u === NO)
|
770 | return;
|
771 | u._c();
|
772 | };
|
773 | return Take;
|
774 | }());
|
775 | var Stream = (function () {
|
776 | function Stream(producer) {
|
777 | this._prod = producer || NO;
|
778 | this._ils = [];
|
779 | this._stopID = NO;
|
780 | this._dl = NO;
|
781 | this._d = false;
|
782 | this._target = NO;
|
783 | this._err = NO;
|
784 | }
|
785 | Stream.prototype._n = function (t) {
|
786 | var a = this._ils;
|
787 | var L = a.length;
|
788 | if (this._d)
|
789 | this._dl._n(t);
|
790 | if (L == 1)
|
791 | a[0]._n(t);
|
792 | else if (L == 0)
|
793 | return;
|
794 | else {
|
795 | var b = cp(a);
|
796 | for (var i = 0; i < L; i++)
|
797 | b[i]._n(t);
|
798 | }
|
799 | };
|
800 | Stream.prototype._e = function (err) {
|
801 | if (this._err !== NO)
|
802 | return;
|
803 | this._err = err;
|
804 | var a = this._ils;
|
805 | var L = a.length;
|
806 | this._x();
|
807 | if (this._d)
|
808 | this._dl._e(err);
|
809 | if (L == 1)
|
810 | a[0]._e(err);
|
811 | else if (L == 0)
|
812 | return;
|
813 | else {
|
814 | var b = cp(a);
|
815 | for (var i = 0; i < L; i++)
|
816 | b[i]._e(err);
|
817 | }
|
818 | if (!this._d && L == 0)
|
819 | throw this._err;
|
820 | };
|
821 | Stream.prototype._c = function () {
|
822 | var a = this._ils;
|
823 | var L = a.length;
|
824 | this._x();
|
825 | if (this._d)
|
826 | this._dl._c();
|
827 | if (L == 1)
|
828 | a[0]._c();
|
829 | else if (L == 0)
|
830 | return;
|
831 | else {
|
832 | var b = cp(a);
|
833 | for (var i = 0; i < L; i++)
|
834 | b[i]._c();
|
835 | }
|
836 | };
|
837 | Stream.prototype._x = function () {
|
838 | if (this._ils.length === 0)
|
839 | return;
|
840 | if (this._prod !== NO)
|
841 | this._prod._stop();
|
842 | this._err = NO;
|
843 | this._ils = [];
|
844 | };
|
845 | Stream.prototype._stopNow = function () {
|
846 | // WARNING: code that calls this method should
|
847 | // first check if this._prod is valid (not `NO`)
|
848 | this._prod._stop();
|
849 | this._err = NO;
|
850 | this._stopID = NO;
|
851 | };
|
852 | Stream.prototype._add = function (il) {
|
853 | var ta = this._target;
|
854 | if (ta !== NO)
|
855 | return ta._add(il);
|
856 | var a = this._ils;
|
857 | a.push(il);
|
858 | if (a.length > 1)
|
859 | return;
|
860 | if (this._stopID !== NO) {
|
861 | clearTimeout(this._stopID);
|
862 | this._stopID = NO;
|
863 | }
|
864 | else {
|
865 | var p = this._prod;
|
866 | if (p !== NO)
|
867 | p._start(this);
|
868 | }
|
869 | };
|
870 | Stream.prototype._remove = function (il) {
|
871 | var _this = this;
|
872 | var ta = this._target;
|
873 | if (ta !== NO)
|
874 | return ta._remove(il);
|
875 | var a = this._ils;
|
876 | var i = a.indexOf(il);
|
877 | if (i > -1) {
|
878 | a.splice(i, 1);
|
879 | if (this._prod !== NO && a.length <= 0) {
|
880 | this._err = NO;
|
881 | this._stopID = setTimeout(function () { return _this._stopNow(); });
|
882 | }
|
883 | else if (a.length === 1) {
|
884 | this._pruneCycles();
|
885 | }
|
886 | }
|
887 | };
|
888 | // If all paths stemming from `this` stream eventually end at `this`
|
889 | // stream, then we remove the single listener of `this` stream, to
|
890 | // force it to end its execution and dispose resources. This method
|
891 | // assumes as a precondition that this._ils has just one listener.
|
892 | Stream.prototype._pruneCycles = function () {
|
893 | if (this._hasNoSinks(this, []))
|
894 | this._remove(this._ils[0]);
|
895 | };
|
896 | // Checks whether *there is no* path starting from `x` that leads to an end
|
897 | // listener (sink) in the stream graph, following edges A->B where B is a
|
898 | // listener of A. This means these paths constitute a cycle somehow. Is given
|
899 | // a trace of all visited nodes so far.
|
900 | Stream.prototype._hasNoSinks = function (x, trace) {
|
901 | if (trace.indexOf(x) !== -1)
|
902 | return true;
|
903 | else if (x.out === this)
|
904 | return true;
|
905 | else if (x.out && x.out !== NO)
|
906 | return this._hasNoSinks(x.out, trace.concat(x));
|
907 | else if (x._ils) {
|
908 | for (var i = 0, N = x._ils.length; i < N; i++)
|
909 | if (!this._hasNoSinks(x._ils[i], trace.concat(x)))
|
910 | return false;
|
911 | return true;
|
912 | }
|
913 | else
|
914 | return false;
|
915 | };
|
916 | Stream.prototype.ctor = function () {
|
917 | return this instanceof MemoryStream ? MemoryStream : Stream;
|
918 | };
|
919 | /**
|
920 | * Adds a Listener to the Stream.
|
921 | *
|
922 | * @param {Listener} listener
|
923 | */
|
924 | Stream.prototype.addListener = function (listener) {
|
925 | listener._n = listener.next || noop;
|
926 | listener._e = listener.error || noop;
|
927 | listener._c = listener.complete || noop;
|
928 | this._add(listener);
|
929 | };
|
930 | /**
|
931 | * Removes a Listener from the Stream, assuming the Listener was added to it.
|
932 | *
|
933 | * @param {Listener<T>} listener
|
934 | */
|
935 | Stream.prototype.removeListener = function (listener) {
|
936 | this._remove(listener);
|
937 | };
|
938 | /**
|
939 | * Adds a Listener to the Stream returning a Subscription to remove that
|
940 | * listener.
|
941 | *
|
942 | * @param {Listener} listener
|
943 | * @returns {Subscription}
|
944 | */
|
945 | Stream.prototype.subscribe = function (listener) {
|
946 | this.addListener(listener);
|
947 | return new StreamSub(this, listener);
|
948 | };
|
949 | /**
|
950 | * Add interop between most.js and RxJS 5
|
951 | *
|
952 | * @returns {Stream}
|
953 | */
|
954 | Stream.prototype[symbol_observable_1.default] = function () {
|
955 | return this;
|
956 | };
|
957 | /**
|
958 | * Creates a new Stream given a Producer.
|
959 | *
|
960 | * @factory true
|
961 | * @param {Producer} producer An optional Producer that dictates how to
|
962 | * start, generate events, and stop the Stream.
|
963 | * @return {Stream}
|
964 | */
|
965 | Stream.create = function (producer) {
|
966 | if (producer) {
|
967 | if (typeof producer.start !== 'function'
|
968 | || typeof producer.stop !== 'function')
|
969 | throw new Error('producer requires both start and stop functions');
|
970 | internalizeProducer(producer); // mutates the input
|
971 | }
|
972 | return new Stream(producer);
|
973 | };
|
974 | /**
|
975 | * Creates a new MemoryStream given a Producer.
|
976 | *
|
977 | * @factory true
|
978 | * @param {Producer} producer An optional Producer that dictates how to
|
979 | * start, generate events, and stop the Stream.
|
980 | * @return {MemoryStream}
|
981 | */
|
982 | Stream.createWithMemory = function (producer) {
|
983 | if (producer)
|
984 | internalizeProducer(producer); // mutates the input
|
985 | return new MemoryStream(producer);
|
986 | };
|
987 | /**
|
988 | * Creates a Stream that does nothing when started. It never emits any event.
|
989 | *
|
990 | * Marble diagram:
|
991 | *
|
992 | * ```text
|
993 | * never
|
994 | * -----------------------
|
995 | * ```
|
996 | *
|
997 | * @factory true
|
998 | * @return {Stream}
|
999 | */
|
1000 | Stream.never = function () {
|
1001 | return new Stream({ _start: noop, _stop: noop });
|
1002 | };
|
1003 | /**
|
1004 | * Creates a Stream that immediately emits the "complete" notification when
|
1005 | * started, and that's it.
|
1006 | *
|
1007 | * Marble diagram:
|
1008 | *
|
1009 | * ```text
|
1010 | * empty
|
1011 | * -|
|
1012 | * ```
|
1013 | *
|
1014 | * @factory true
|
1015 | * @return {Stream}
|
1016 | */
|
1017 | Stream.empty = function () {
|
1018 | return new Stream({
|
1019 | _start: function (il) { il._c(); },
|
1020 | _stop: noop,
|
1021 | });
|
1022 | };
|
1023 | /**
|
1024 | * Creates a Stream that immediately emits an "error" notification with the
|
1025 | * value you passed as the `error` argument when the stream starts, and that's
|
1026 | * it.
|
1027 | *
|
1028 | * Marble diagram:
|
1029 | *
|
1030 | * ```text
|
1031 | * throw(X)
|
1032 | * -X
|
1033 | * ```
|
1034 | *
|
1035 | * @factory true
|
1036 | * @param error The error event to emit on the created stream.
|
1037 | * @return {Stream}
|
1038 | */
|
1039 | Stream.throw = function (error) {
|
1040 | return new Stream({
|
1041 | _start: function (il) { il._e(error); },
|
1042 | _stop: noop,
|
1043 | });
|
1044 | };
|
1045 | /**
|
1046 | * Creates a stream from an Array, Promise, or an Observable.
|
1047 | *
|
1048 | * @factory true
|
1049 | * @param {Array|PromiseLike|Observable} input The input to make a stream from.
|
1050 | * @return {Stream}
|
1051 | */
|
1052 | Stream.from = function (input) {
|
1053 | if (typeof input[symbol_observable_1.default] === 'function')
|
1054 | return Stream.fromObservable(input);
|
1055 | else if (typeof input.then === 'function')
|
1056 | return Stream.fromPromise(input);
|
1057 | else if (Array.isArray(input))
|
1058 | return Stream.fromArray(input);
|
1059 | throw new TypeError("Type of input to from() must be an Array, Promise, or Observable");
|
1060 | };
|
1061 | /**
|
1062 | * Creates a Stream that immediately emits the arguments that you give to
|
1063 | * *of*, then completes.
|
1064 | *
|
1065 | * Marble diagram:
|
1066 | *
|
1067 | * ```text
|
1068 | * of(1,2,3)
|
1069 | * 123|
|
1070 | * ```
|
1071 | *
|
1072 | * @factory true
|
1073 | * @param a The first value you want to emit as an event on the stream.
|
1074 | * @param b The second value you want to emit as an event on the stream. One
|
1075 | * or more of these values may be given as arguments.
|
1076 | * @return {Stream}
|
1077 | */
|
1078 | Stream.of = function () {
|
1079 | var items = [];
|
1080 | for (var _i = 0; _i < arguments.length; _i++) {
|
1081 | items[_i] = arguments[_i];
|
1082 | }
|
1083 | return Stream.fromArray(items);
|
1084 | };
|
1085 | /**
|
1086 | * Converts an array to a stream. The returned stream will emit synchronously
|
1087 | * all the items in the array, and then complete.
|
1088 | *
|
1089 | * Marble diagram:
|
1090 | *
|
1091 | * ```text
|
1092 | * fromArray([1,2,3])
|
1093 | * 123|
|
1094 | * ```
|
1095 | *
|
1096 | * @factory true
|
1097 | * @param {Array} array The array to be converted as a stream.
|
1098 | * @return {Stream}
|
1099 | */
|
1100 | Stream.fromArray = function (array) {
|
1101 | return new Stream(new FromArray(array));
|
1102 | };
|
1103 | /**
|
1104 | * Converts a promise to a stream. The returned stream will emit the resolved
|
1105 | * value of the promise, and then complete. However, if the promise is
|
1106 | * rejected, the stream will emit the corresponding error.
|
1107 | *
|
1108 | * Marble diagram:
|
1109 | *
|
1110 | * ```text
|
1111 | * fromPromise( ----42 )
|
1112 | * -----------------42|
|
1113 | * ```
|
1114 | *
|
1115 | * @factory true
|
1116 | * @param {PromiseLike} promise The promise to be converted as a stream.
|
1117 | * @return {Stream}
|
1118 | */
|
1119 | Stream.fromPromise = function (promise) {
|
1120 | return new Stream(new FromPromise(promise));
|
1121 | };
|
1122 | /**
|
1123 | * Converts an Observable into a Stream.
|
1124 | *
|
1125 | * @factory true
|
1126 | * @param {any} observable The observable to be converted as a stream.
|
1127 | * @return {Stream}
|
1128 | */
|
1129 | Stream.fromObservable = function (obs) {
|
1130 | if (obs.endWhen)
|
1131 | return obs;
|
1132 | return new Stream(new FromObservable(obs));
|
1133 | };
|
1134 | /**
|
1135 | * Creates a stream that periodically emits incremental numbers, every
|
1136 | * `period` milliseconds.
|
1137 | *
|
1138 | * Marble diagram:
|
1139 | *
|
1140 | * ```text
|
1141 | * periodic(1000)
|
1142 | * ---0---1---2---3---4---...
|
1143 | * ```
|
1144 | *
|
1145 | * @factory true
|
1146 | * @param {number} period The interval in milliseconds to use as a rate of
|
1147 | * emission.
|
1148 | * @return {Stream}
|
1149 | */
|
1150 | Stream.periodic = function (period) {
|
1151 | return new Stream(new Periodic(period));
|
1152 | };
|
1153 | Stream.prototype._map = function (project) {
|
1154 | return new (this.ctor())(new MapOp(project, this));
|
1155 | };
|
1156 | /**
|
1157 | * Transforms each event from the input Stream through a `project` function,
|
1158 | * to get a Stream that emits those transformed events.
|
1159 | *
|
1160 | * Marble diagram:
|
1161 | *
|
1162 | * ```text
|
1163 | * --1---3--5-----7------
|
1164 | * map(i => i * 10)
|
1165 | * --10--30-50----70-----
|
1166 | * ```
|
1167 | *
|
1168 | * @param {Function} project A function of type `(t: T) => U` that takes event
|
1169 | * `t` of type `T` from the input Stream and produces an event of type `U`, to
|
1170 | * be emitted on the output Stream.
|
1171 | * @return {Stream}
|
1172 | */
|
1173 | Stream.prototype.map = function (project) {
|
1174 | return this._map(project);
|
1175 | };
|
1176 | /**
|
1177 | * It's like `map`, but transforms each input event to always the same
|
1178 | * constant value on the output Stream.
|
1179 | *
|
1180 | * Marble diagram:
|
1181 | *
|
1182 | * ```text
|
1183 | * --1---3--5-----7-----
|
1184 | * mapTo(10)
|
1185 | * --10--10-10----10----
|
1186 | * ```
|
1187 | *
|
1188 | * @param projectedValue A value to emit on the output Stream whenever the
|
1189 | * input Stream emits any value.
|
1190 | * @return {Stream}
|
1191 | */
|
1192 | Stream.prototype.mapTo = function (projectedValue) {
|
1193 | var s = this.map(function () { return projectedValue; });
|
1194 | var op = s._prod;
|
1195 | op.type = 'mapTo';
|
1196 | return s;
|
1197 | };
|
1198 | /**
|
1199 | * Only allows events that pass the test given by the `passes` argument.
|
1200 | *
|
1201 | * Each event from the input stream is given to the `passes` function. If the
|
1202 | * function returns `true`, the event is forwarded to the output stream,
|
1203 | * otherwise it is ignored and not forwarded.
|
1204 | *
|
1205 | * Marble diagram:
|
1206 | *
|
1207 | * ```text
|
1208 | * --1---2--3-----4-----5---6--7-8--
|
1209 | * filter(i => i % 2 === 0)
|
1210 | * ------2--------4---------6----8--
|
1211 | * ```
|
1212 | *
|
1213 | * @param {Function} passes A function of type `(t: T) +> boolean` that takes
|
1214 | * an event from the input stream and checks if it passes, by returning a
|
1215 | * boolean.
|
1216 | * @return {Stream}
|
1217 | */
|
1218 | Stream.prototype.filter = function (passes) {
|
1219 | var p = this._prod;
|
1220 | if (p instanceof Filter)
|
1221 | return new Stream(new Filter(and(p.f, passes), p.ins));
|
1222 | return new Stream(new Filter(passes, this));
|
1223 | };
|
1224 | /**
|
1225 | * Lets the first `amount` many events from the input stream pass to the
|
1226 | * output stream, then makes the output stream complete.
|
1227 | *
|
1228 | * Marble diagram:
|
1229 | *
|
1230 | * ```text
|
1231 | * --a---b--c----d---e--
|
1232 | * take(3)
|
1233 | * --a---b--c|
|
1234 | * ```
|
1235 | *
|
1236 | * @param {number} amount How many events to allow from the input stream
|
1237 | * before completing the output stream.
|
1238 | * @return {Stream}
|
1239 | */
|
1240 | Stream.prototype.take = function (amount) {
|
1241 | return new (this.ctor())(new Take(amount, this));
|
1242 | };
|
1243 | /**
|
1244 | * Ignores the first `amount` many events from the input stream, and then
|
1245 | * after that starts forwarding events from the input stream to the output
|
1246 | * stream.
|
1247 | *
|
1248 | * Marble diagram:
|
1249 | *
|
1250 | * ```text
|
1251 | * --a---b--c----d---e--
|
1252 | * drop(3)
|
1253 | * --------------d---e--
|
1254 | * ```
|
1255 | *
|
1256 | * @param {number} amount How many events to ignore from the input stream
|
1257 | * before forwarding all events from the input stream to the output stream.
|
1258 | * @return {Stream}
|
1259 | */
|
1260 | Stream.prototype.drop = function (amount) {
|
1261 | return new Stream(new Drop(amount, this));
|
1262 | };
|
1263 | /**
|
1264 | * When the input stream completes, the output stream will emit the last event
|
1265 | * emitted by the input stream, and then will also complete.
|
1266 | *
|
1267 | * Marble diagram:
|
1268 | *
|
1269 | * ```text
|
1270 | * --a---b--c--d----|
|
1271 | * last()
|
1272 | * -----------------d|
|
1273 | * ```
|
1274 | *
|
1275 | * @return {Stream}
|
1276 | */
|
1277 | Stream.prototype.last = function () {
|
1278 | return new Stream(new Last(this));
|
1279 | };
|
1280 | /**
|
1281 | * Prepends the given `initial` value to the sequence of events emitted by the
|
1282 | * input stream. The returned stream is a MemoryStream, which means it is
|
1283 | * already `remember()`'d.
|
1284 | *
|
1285 | * Marble diagram:
|
1286 | *
|
1287 | * ```text
|
1288 | * ---1---2-----3---
|
1289 | * startWith(0)
|
1290 | * 0--1---2-----3---
|
1291 | * ```
|
1292 | *
|
1293 | * @param initial The value or event to prepend.
|
1294 | * @return {MemoryStream}
|
1295 | */
|
1296 | Stream.prototype.startWith = function (initial) {
|
1297 | return new MemoryStream(new StartWith(this, initial));
|
1298 | };
|
1299 | /**
|
1300 | * Uses another stream to determine when to complete the current stream.
|
1301 | *
|
1302 | * When the given `other` stream emits an event or completes, the output
|
1303 | * stream will complete. Before that happens, the output stream will behaves
|
1304 | * like the input stream.
|
1305 | *
|
1306 | * Marble diagram:
|
1307 | *
|
1308 | * ```text
|
1309 | * ---1---2-----3--4----5----6---
|
1310 | * endWhen( --------a--b--| )
|
1311 | * ---1---2-----3--4--|
|
1312 | * ```
|
1313 | *
|
1314 | * @param other Some other stream that is used to know when should the output
|
1315 | * stream of this operator complete.
|
1316 | * @return {Stream}
|
1317 | */
|
1318 | Stream.prototype.endWhen = function (other) {
|
1319 | return new (this.ctor())(new EndWhen(other, this));
|
1320 | };
|
1321 | /**
|
1322 | * "Folds" the stream onto itself.
|
1323 | *
|
1324 | * Combines events from the past throughout
|
1325 | * the entire execution of the input stream, allowing you to accumulate them
|
1326 | * together. It's essentially like `Array.prototype.reduce`. The returned
|
1327 | * stream is a MemoryStream, which means it is already `remember()`'d.
|
1328 | *
|
1329 | * The output stream starts by emitting the `seed` which you give as argument.
|
1330 | * Then, when an event happens on the input stream, it is combined with that
|
1331 | * seed value through the `accumulate` function, and the output value is
|
1332 | * emitted on the output stream. `fold` remembers that output value as `acc`
|
1333 | * ("accumulator"), and then when a new input event `t` happens, `acc` will be
|
1334 | * combined with that to produce the new `acc` and so forth.
|
1335 | *
|
1336 | * Marble diagram:
|
1337 | *
|
1338 | * ```text
|
1339 | * ------1-----1--2----1----1------
|
1340 | * fold((acc, x) => acc + x, 3)
|
1341 | * 3-----4-----5--7----8----9------
|
1342 | * ```
|
1343 | *
|
1344 | * @param {Function} accumulate A function of type `(acc: R, t: T) => R` that
|
1345 | * takes the previous accumulated value `acc` and the incoming event from the
|
1346 | * input stream and produces the new accumulated value.
|
1347 | * @param seed The initial accumulated value, of type `R`.
|
1348 | * @return {MemoryStream}
|
1349 | */
|
1350 | Stream.prototype.fold = function (accumulate, seed) {
|
1351 | return new MemoryStream(new Fold(accumulate, seed, this));
|
1352 | };
|
1353 | /**
|
1354 | * Replaces an error with another stream.
|
1355 | *
|
1356 | * When (and if) an error happens on the input stream, instead of forwarding
|
1357 | * that error to the output stream, *replaceError* will call the `replace`
|
1358 | * function which returns the stream that the output stream will replicate.
|
1359 | * And, in case that new stream also emits an error, `replace` will be called
|
1360 | * again to get another stream to start replicating.
|
1361 | *
|
1362 | * Marble diagram:
|
1363 | *
|
1364 | * ```text
|
1365 | * --1---2-----3--4-----X
|
1366 | * replaceError( () => --10--| )
|
1367 | * --1---2-----3--4--------10--|
|
1368 | * ```
|
1369 | *
|
1370 | * @param {Function} replace A function of type `(err) => Stream` that takes
|
1371 | * the error that occurred on the input stream or on the previous replacement
|
1372 | * stream and returns a new stream. The output stream will behave like the
|
1373 | * stream that this function returns.
|
1374 | * @return {Stream}
|
1375 | */
|
1376 | Stream.prototype.replaceError = function (replace) {
|
1377 | return new (this.ctor())(new ReplaceError(replace, this));
|
1378 | };
|
1379 | /**
|
1380 | * Flattens a "stream of streams", handling only one nested stream at a time
|
1381 | * (no concurrency).
|
1382 | *
|
1383 | * If the input stream is a stream that emits streams, then this operator will
|
1384 | * return an output stream which is a flat stream: emits regular events. The
|
1385 | * flattening happens without concurrency. It works like this: when the input
|
1386 | * stream emits a nested stream, *flatten* will start imitating that nested
|
1387 | * one. However, as soon as the next nested stream is emitted on the input
|
1388 | * stream, *flatten* will forget the previous nested one it was imitating, and
|
1389 | * will start imitating the new nested one.
|
1390 | *
|
1391 | * Marble diagram:
|
1392 | *
|
1393 | * ```text
|
1394 | * --+--------+---------------
|
1395 | * \ \
|
1396 | * \ ----1----2---3--
|
1397 | * --a--b----c----d--------
|
1398 | * flatten
|
1399 | * -----a--b------1----2---3--
|
1400 | * ```
|
1401 | *
|
1402 | * @return {Stream}
|
1403 | */
|
1404 | Stream.prototype.flatten = function () {
|
1405 | var p = this._prod;
|
1406 | return new Stream(new Flatten(this));
|
1407 | };
|
1408 | /**
|
1409 | * Passes the input stream to a custom operator, to produce an output stream.
|
1410 | *
|
1411 | * *compose* is a handy way of using an existing function in a chained style.
|
1412 | * Instead of writing `outStream = f(inStream)` you can write
|
1413 | * `outStream = inStream.compose(f)`.
|
1414 | *
|
1415 | * @param {function} operator A function that takes a stream as input and
|
1416 | * returns a stream as well.
|
1417 | * @return {Stream}
|
1418 | */
|
1419 | Stream.prototype.compose = function (operator) {
|
1420 | return operator(this);
|
1421 | };
|
1422 | /**
|
1423 | * Returns an output stream that behaves like the input stream, but also
|
1424 | * remembers the most recent event that happens on the input stream, so that a
|
1425 | * newly added listener will immediately receive that memorised event.
|
1426 | *
|
1427 | * @return {MemoryStream}
|
1428 | */
|
1429 | Stream.prototype.remember = function () {
|
1430 | return new MemoryStream(new Remember(this));
|
1431 | };
|
1432 | /**
|
1433 | * Returns an output stream that identically behaves like the input stream,
|
1434 | * but also runs a `spy` function fo each event, to help you debug your app.
|
1435 | *
|
1436 | * *debug* takes a `spy` function as argument, and runs that for each event
|
1437 | * happening on the input stream. If you don't provide the `spy` argument,
|
1438 | * then *debug* will just `console.log` each event. This helps you to
|
1439 | * understand the flow of events through some operator chain.
|
1440 | *
|
1441 | * Please note that if the output stream has no listeners, then it will not
|
1442 | * start, which means `spy` will never run because no actual event happens in
|
1443 | * that case.
|
1444 | *
|
1445 | * Marble diagram:
|
1446 | *
|
1447 | * ```text
|
1448 | * --1----2-----3-----4--
|
1449 | * debug
|
1450 | * --1----2-----3-----4--
|
1451 | * ```
|
1452 | *
|
1453 | * @param {function} labelOrSpy A string to use as the label when printing
|
1454 | * debug information on the console, or a 'spy' function that takes an event
|
1455 | * as argument, and does not need to return anything.
|
1456 | * @return {Stream}
|
1457 | */
|
1458 | Stream.prototype.debug = function (labelOrSpy) {
|
1459 | return new (this.ctor())(new Debug(this, labelOrSpy));
|
1460 | };
|
1461 | /**
|
1462 | * *imitate* changes this current Stream to emit the same events that the
|
1463 | * `other` given Stream does. This method returns nothing.
|
1464 | *
|
1465 | * This method exists to allow one thing: **circular dependency of streams**.
|
1466 | * For instance, let's imagine that for some reason you need to create a
|
1467 | * circular dependency where stream `first$` depends on stream `second$`
|
1468 | * which in turn depends on `first$`:
|
1469 | *
|
1470 | * <!-- skip-example -->
|
1471 | * ```js
|
1472 | * import delay from 'xstream/extra/delay'
|
1473 | *
|
1474 | * var first$ = second$.map(x => x * 10).take(3);
|
1475 | * var second$ = first$.map(x => x + 1).startWith(1).compose(delay(100));
|
1476 | * ```
|
1477 | *
|
1478 | * However, that is invalid JavaScript, because `second$` is undefined
|
1479 | * on the first line. This is how *imitate* can help solve it:
|
1480 | *
|
1481 | * ```js
|
1482 | * import delay from 'xstream/extra/delay'
|
1483 | *
|
1484 | * var secondProxy$ = xs.create();
|
1485 | * var first$ = secondProxy$.map(x => x * 10).take(3);
|
1486 | * var second$ = first$.map(x => x + 1).startWith(1).compose(delay(100));
|
1487 | * secondProxy$.imitate(second$);
|
1488 | * ```
|
1489 | *
|
1490 | * We create `secondProxy$` before the others, so it can be used in the
|
1491 | * declaration of `first$`. Then, after both `first$` and `second$` are
|
1492 | * defined, we hook `secondProxy$` with `second$` with `imitate()` to tell
|
1493 | * that they are "the same". `imitate` will not trigger the start of any
|
1494 | * stream, it just binds `secondProxy$` and `second$` together.
|
1495 | *
|
1496 | * The following is an example where `imitate()` is important in Cycle.js
|
1497 | * applications. A parent component contains some child components. A child
|
1498 | * has an action stream which is given to the parent to define its state:
|
1499 | *
|
1500 | * <!-- skip-example -->
|
1501 | * ```js
|
1502 | * const childActionProxy$ = xs.create();
|
1503 | * const parent = Parent({...sources, childAction$: childActionProxy$});
|
1504 | * const childAction$ = parent.state$.map(s => s.child.action$).flatten();
|
1505 | * childActionProxy$.imitate(childAction$);
|
1506 | * ```
|
1507 | *
|
1508 | * Note, though, that **`imitate()` does not support MemoryStreams**. If we
|
1509 | * would attempt to imitate a MemoryStream in a circular dependency, we would
|
1510 | * either get a race condition (where the symptom would be "nothing happens")
|
1511 | * or an infinite cyclic emission of values. It's useful to think about
|
1512 | * MemoryStreams as cells in a spreadsheet. It doesn't make any sense to
|
1513 | * define a spreadsheet cell `A1` with a formula that depends on `B1` and
|
1514 | * cell `B1` defined with a formula that depends on `A1`.
|
1515 | *
|
1516 | * If you find yourself wanting to use `imitate()` with a
|
1517 | * MemoryStream, you should rework your code around `imitate()` to use a
|
1518 | * Stream instead. Look for the stream in the circular dependency that
|
1519 | * represents an event stream, and that would be a candidate for creating a
|
1520 | * proxy Stream which then imitates the target Stream.
|
1521 | *
|
1522 | * @param {Stream} target The other stream to imitate on the current one. Must
|
1523 | * not be a MemoryStream.
|
1524 | */
|
1525 | Stream.prototype.imitate = function (target) {
|
1526 | if (target instanceof MemoryStream)
|
1527 | throw new Error('A MemoryStream was given to imitate(), but it only ' +
|
1528 | 'supports a Stream. Read more about this restriction here: ' +
|
1529 | 'https://github.com/staltz/xstream#faq');
|
1530 | this._target = target;
|
1531 | for (var ils = this._ils, N = ils.length, i = 0; i < N; i++)
|
1532 | target._add(ils[i]);
|
1533 | this._ils = [];
|
1534 | };
|
1535 | /**
|
1536 | * Forces the Stream to emit the given value to its listeners.
|
1537 | *
|
1538 | * As the name indicates, if you use this, you are most likely doing something
|
1539 | * The Wrong Way. Please try to understand the reactive way before using this
|
1540 | * method. Use it only when you know what you are doing.
|
1541 | *
|
1542 | * @param value The "next" value you want to broadcast to all listeners of
|
1543 | * this Stream.
|
1544 | */
|
1545 | Stream.prototype.shamefullySendNext = function (value) {
|
1546 | this._n(value);
|
1547 | };
|
1548 | /**
|
1549 | * Forces the Stream to emit the given error to its listeners.
|
1550 | *
|
1551 | * As the name indicates, if you use this, you are most likely doing something
|
1552 | * The Wrong Way. Please try to understand the reactive way before using this
|
1553 | * method. Use it only when you know what you are doing.
|
1554 | *
|
1555 | * @param {any} error The error you want to broadcast to all the listeners of
|
1556 | * this Stream.
|
1557 | */
|
1558 | Stream.prototype.shamefullySendError = function (error) {
|
1559 | this._e(error);
|
1560 | };
|
1561 | /**
|
1562 | * Forces the Stream to emit the "completed" event to its listeners.
|
1563 | *
|
1564 | * As the name indicates, if you use this, you are most likely doing something
|
1565 | * The Wrong Way. Please try to understand the reactive way before using this
|
1566 | * method. Use it only when you know what you are doing.
|
1567 | */
|
1568 | Stream.prototype.shamefullySendComplete = function () {
|
1569 | this._c();
|
1570 | };
|
1571 | /**
|
1572 | * Adds a "debug" listener to the stream. There can only be one debug
|
1573 | * listener, that's why this is 'setDebugListener'. To remove the debug
|
1574 | * listener, just call setDebugListener(null).
|
1575 | *
|
1576 | * A debug listener is like any other listener. The only difference is that a
|
1577 | * debug listener is "stealthy": its presence/absence does not trigger the
|
1578 | * start/stop of the stream (or the producer inside the stream). This is
|
1579 | * useful so you can inspect what is going on without changing the behavior
|
1580 | * of the program. If you have an idle stream and you add a normal listener to
|
1581 | * it, the stream will start executing. But if you set a debug listener on an
|
1582 | * idle stream, it won't start executing (not until the first normal listener
|
1583 | * is added).
|
1584 | *
|
1585 | * As the name indicates, we don't recommend using this method to build app
|
1586 | * logic. In fact, in most cases the debug operator works just fine. Only use
|
1587 | * this one if you know what you're doing.
|
1588 | *
|
1589 | * @param {Listener<T>} listener
|
1590 | */
|
1591 | Stream.prototype.setDebugListener = function (listener) {
|
1592 | if (!listener) {
|
1593 | this._d = false;
|
1594 | this._dl = NO;
|
1595 | }
|
1596 | else {
|
1597 | this._d = true;
|
1598 | listener._n = listener.next || noop;
|
1599 | listener._e = listener.error || noop;
|
1600 | listener._c = listener.complete || noop;
|
1601 | this._dl = listener;
|
1602 | }
|
1603 | };
|
1604 | return Stream;
|
1605 | }());
|
1606 | /**
|
1607 | * Blends multiple streams together, emitting events from all of them
|
1608 | * concurrently.
|
1609 | *
|
1610 | * *merge* takes multiple streams as arguments, and creates a stream that
|
1611 | * behaves like each of the argument streams, in parallel.
|
1612 | *
|
1613 | * Marble diagram:
|
1614 | *
|
1615 | * ```text
|
1616 | * --1----2-----3--------4---
|
1617 | * ----a-----b----c---d------
|
1618 | * merge
|
1619 | * --1-a--2--b--3-c---d--4---
|
1620 | * ```
|
1621 | *
|
1622 | * @factory true
|
1623 | * @param {Stream} stream1 A stream to merge together with other streams.
|
1624 | * @param {Stream} stream2 A stream to merge together with other streams. Two
|
1625 | * or more streams may be given as arguments.
|
1626 | * @return {Stream}
|
1627 | */
|
1628 | Stream.merge = function merge() {
|
1629 | var streams = [];
|
1630 | for (var _i = 0; _i < arguments.length; _i++) {
|
1631 | streams[_i] = arguments[_i];
|
1632 | }
|
1633 | return new Stream(new Merge(streams));
|
1634 | };
|
1635 | /**
|
1636 | * Combines multiple input streams together to return a stream whose events
|
1637 | * are arrays that collect the latest events from each input stream.
|
1638 | *
|
1639 | * *combine* internally remembers the most recent event from each of the input
|
1640 | * streams. When any of the input streams emits an event, that event together
|
1641 | * with all the other saved events are combined into an array. That array will
|
1642 | * be emitted on the output stream. It's essentially a way of joining together
|
1643 | * the events from multiple streams.
|
1644 | *
|
1645 | * Marble diagram:
|
1646 | *
|
1647 | * ```text
|
1648 | * --1----2-----3--------4---
|
1649 | * ----a-----b-----c--d------
|
1650 | * combine
|
1651 | * ----1a-2a-2b-3b-3c-3d-4d--
|
1652 | * ```
|
1653 | *
|
1654 | * Note: to minimize garbage collection, *combine* uses the same array
|
1655 | * instance for each emission. If you need to compare emissions over time,
|
1656 | * cache the values with `map` first:
|
1657 | *
|
1658 | * ```js
|
1659 | * import pairwise from 'xstream/extra/pairwise'
|
1660 | *
|
1661 | * const stream1 = xs.of(1);
|
1662 | * const stream2 = xs.of(2);
|
1663 | *
|
1664 | * xs.combine(stream1, stream2).map(
|
1665 | * combinedEmissions => ([ ...combinedEmissions ])
|
1666 | * ).compose(pairwise)
|
1667 | * ```
|
1668 | *
|
1669 | * @factory true
|
1670 | * @param {Stream} stream1 A stream to combine together with other streams.
|
1671 | * @param {Stream} stream2 A stream to combine together with other streams.
|
1672 | * Multiple streams, not just two, may be given as arguments.
|
1673 | * @return {Stream}
|
1674 | */
|
1675 | Stream.combine = function combine() {
|
1676 | var streams = [];
|
1677 | for (var _i = 0; _i < arguments.length; _i++) {
|
1678 | streams[_i] = arguments[_i];
|
1679 | }
|
1680 | return new Stream(new Combine(streams));
|
1681 | };
|
1682 | exports.Stream = Stream;
|
1683 | var MemoryStream = (function (_super) {
|
1684 | __extends(MemoryStream, _super);
|
1685 | function MemoryStream(producer) {
|
1686 | var _this = _super.call(this, producer) || this;
|
1687 | _this._has = false;
|
1688 | return _this;
|
1689 | }
|
1690 | MemoryStream.prototype._n = function (x) {
|
1691 | this._v = x;
|
1692 | this._has = true;
|
1693 | _super.prototype._n.call(this, x);
|
1694 | };
|
1695 | MemoryStream.prototype._add = function (il) {
|
1696 | var ta = this._target;
|
1697 | if (ta !== NO)
|
1698 | return ta._add(il);
|
1699 | var a = this._ils;
|
1700 | a.push(il);
|
1701 | if (a.length > 1) {
|
1702 | if (this._has)
|
1703 | il._n(this._v);
|
1704 | return;
|
1705 | }
|
1706 | if (this._stopID !== NO) {
|
1707 | if (this._has)
|
1708 | il._n(this._v);
|
1709 | clearTimeout(this._stopID);
|
1710 | this._stopID = NO;
|
1711 | }
|
1712 | else if (this._has)
|
1713 | il._n(this._v);
|
1714 | else {
|
1715 | var p = this._prod;
|
1716 | if (p !== NO)
|
1717 | p._start(this);
|
1718 | }
|
1719 | };
|
1720 | MemoryStream.prototype._stopNow = function () {
|
1721 | this._has = false;
|
1722 | _super.prototype._stopNow.call(this);
|
1723 | };
|
1724 | MemoryStream.prototype._x = function () {
|
1725 | this._has = false;
|
1726 | _super.prototype._x.call(this);
|
1727 | };
|
1728 | MemoryStream.prototype.map = function (project) {
|
1729 | return this._map(project);
|
1730 | };
|
1731 | MemoryStream.prototype.mapTo = function (projectedValue) {
|
1732 | return _super.prototype.mapTo.call(this, projectedValue);
|
1733 | };
|
1734 | MemoryStream.prototype.take = function (amount) {
|
1735 | return _super.prototype.take.call(this, amount);
|
1736 | };
|
1737 | MemoryStream.prototype.endWhen = function (other) {
|
1738 | return _super.prototype.endWhen.call(this, other);
|
1739 | };
|
1740 | MemoryStream.prototype.replaceError = function (replace) {
|
1741 | return _super.prototype.replaceError.call(this, replace);
|
1742 | };
|
1743 | MemoryStream.prototype.remember = function () {
|
1744 | return this;
|
1745 | };
|
1746 | MemoryStream.prototype.debug = function (labelOrSpy) {
|
1747 | return _super.prototype.debug.call(this, labelOrSpy);
|
1748 | };
|
1749 | return MemoryStream;
|
1750 | }(Stream));
|
1751 | exports.MemoryStream = MemoryStream;
|
1752 | Object.defineProperty(exports, "__esModule", { value: true });
|
1753 | exports.default = Stream;
|
1754 | //# sourceMappingURL=index.js.map |
\ | No newline at end of file |