1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
3 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
4 | (factory((global.most = {})));
|
5 | }(this, (function (exports) { 'use strict';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function Stream (source) {
|
12 | this.source = source;
|
13 | }
|
14 |
|
15 | Stream.prototype.run = function (sink, scheduler) {
|
16 | return this.source.run(sink, scheduler)
|
17 | };
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function cons (x, a) {
|
26 | var l = a.length;
|
27 | var b = new Array(l + 1);
|
28 | b[0] = x;
|
29 | for (var i = 0; i < l; ++i) {
|
30 | b[i + 1] = a[i];
|
31 | }
|
32 | return b
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 | function append (x, a) {
|
38 | var l = a.length;
|
39 | var b = new Array(l + 1);
|
40 | for (var i = 0; i < l; ++i) {
|
41 | b[i] = a[i];
|
42 | }
|
43 |
|
44 | b[l] = x;
|
45 | return b
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 | function drop (n, a) {
|
51 | if (n < 0) {
|
52 | throw new TypeError('n must be >= 0')
|
53 | }
|
54 |
|
55 | var l = a.length;
|
56 | if (n === 0 || l === 0) {
|
57 | return a
|
58 | }
|
59 |
|
60 | if (n >= l) {
|
61 | return []
|
62 | }
|
63 |
|
64 | return unsafeDrop(n, a, l - n)
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 | function unsafeDrop (n, a, l) {
|
70 | var b = new Array(l);
|
71 | for (var i = 0; i < l; ++i) {
|
72 | b[i] = a[n + i];
|
73 | }
|
74 | return b
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 | function tail (a) {
|
80 | return drop(1, a)
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 | function copy (a) {
|
86 | var l = a.length;
|
87 | var b = new Array(l);
|
88 | for (var i = 0; i < l; ++i) {
|
89 | b[i] = a[i];
|
90 | }
|
91 | return b
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 | function map (f, a) {
|
97 | var l = a.length;
|
98 | var b = new Array(l);
|
99 | for (var i = 0; i < l; ++i) {
|
100 | b[i] = f(a[i]);
|
101 | }
|
102 | return b
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 | function reduce (f, z, a) {
|
108 | var r = z;
|
109 | for (var i = 0, l = a.length; i < l; ++i) {
|
110 | r = f(r, a[i], i);
|
111 | }
|
112 | return r
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 | function replace (x, i, a) {
|
118 | if (i < 0) {
|
119 | throw new TypeError('i must be >= 0')
|
120 | }
|
121 |
|
122 | var l = a.length;
|
123 | var b = new Array(l);
|
124 | for (var j = 0; j < l; ++j) {
|
125 | b[j] = i === j ? x : a[j];
|
126 | }
|
127 | return b
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 | function remove (i, a) {
|
133 | if (i < 0) {
|
134 | throw new TypeError('i must be >= 0')
|
135 | }
|
136 |
|
137 | var l = a.length;
|
138 | if (l === 0 || i >= l) {
|
139 | return a
|
140 | }
|
141 |
|
142 | if (l === 1) {
|
143 | return []
|
144 | }
|
145 |
|
146 | return unsafeRemove(i, a, l - 1)
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 | function unsafeRemove (i, a, l) {
|
152 | var b = new Array(l);
|
153 | var j;
|
154 | for (j = 0; j < i; ++j) {
|
155 | b[j] = a[j];
|
156 | }
|
157 | for (j = i; j < l; ++j) {
|
158 | b[j] = a[j + 1];
|
159 | }
|
160 |
|
161 | return b
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 | function removeAll (f, a) {
|
167 | var l = a.length;
|
168 | var b = new Array(l);
|
169 | var j = 0;
|
170 | for (var x = (void 0), i = 0; i < l; ++i) {
|
171 | x = a[i];
|
172 | if (!f(x)) {
|
173 | b[j] = x;
|
174 | ++j;
|
175 | }
|
176 | }
|
177 |
|
178 | b.length = j;
|
179 | return b
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 | function findIndex (x, a) {
|
185 | for (var i = 0, l = a.length; i < l; ++i) {
|
186 | if (x === a[i]) {
|
187 | return i
|
188 | }
|
189 | }
|
190 | return -1
|
191 | }
|
192 |
|
193 |
|
194 |
|
195 | function isArrayLike (x) {
|
196 | return x != null && typeof x.length === 'number' && typeof x !== 'function'
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | var id = function (x) { return x; };
|
203 |
|
204 |
|
205 | var compose = function (f, g) { return function (x) { return f(g(x)); }; };
|
206 |
|
207 |
|
208 | var apply = function (f, x) { return f(x); };
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | function Disposable (dispose, data) {
|
221 | this._dispose = dispose;
|
222 | this._data = data;
|
223 | }
|
224 |
|
225 | Disposable.prototype.dispose = function () {
|
226 | return this._dispose(this._data)
|
227 | };
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | function SettableDisposable () {
|
234 | this.disposable = void 0;
|
235 | this.disposed = false;
|
236 | this._resolve = void 0;
|
237 |
|
238 | var self = this;
|
239 | this.result = new Promise(function (resolve) {
|
240 | self._resolve = resolve;
|
241 | });
|
242 | }
|
243 |
|
244 | SettableDisposable.prototype.setDisposable = function (disposable) {
|
245 | if (this.disposable !== void 0) {
|
246 | throw new Error('setDisposable called more than once')
|
247 | }
|
248 |
|
249 | this.disposable = disposable;
|
250 |
|
251 | if (this.disposed) {
|
252 | this._resolve(disposable.dispose());
|
253 | }
|
254 | };
|
255 |
|
256 | SettableDisposable.prototype.dispose = function () {
|
257 | if (this.disposed) {
|
258 | return this.result
|
259 | }
|
260 |
|
261 | this.disposed = true;
|
262 |
|
263 | if (this.disposable !== void 0) {
|
264 | this.result = this.disposable.dispose();
|
265 | }
|
266 |
|
267 | return this.result
|
268 | };
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | function isPromise (p) {
|
275 | return p !== null && typeof p === 'object' && typeof p.then === 'function'
|
276 | }
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | var map$1 = map;
|
282 | var identity = id;
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | function tryDispose (t, disposable, sink) {
|
293 | var result = disposeSafely(disposable);
|
294 | return isPromise(result)
|
295 | ? result.catch(function (e) {
|
296 | sink.error(t, e);
|
297 | })
|
298 | : result
|
299 | }
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 | function create (dispose, data) {
|
309 | return once(new Disposable(dispose, data))
|
310 | }
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | function empty$1 () {
|
318 | return new Disposable(identity, void 0)
|
319 | }
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | function all (disposables) {
|
327 | return create(disposeAll, disposables)
|
328 | }
|
329 |
|
330 | function disposeAll (disposables) {
|
331 | return Promise.all(map$1(disposeSafely, disposables))
|
332 | }
|
333 |
|
334 | function disposeSafely (disposable) {
|
335 | try {
|
336 | return disposable.dispose()
|
337 | } catch (e) {
|
338 | return Promise.reject(e)
|
339 | }
|
340 | }
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 | function settable () {
|
355 | return new SettableDisposable()
|
356 | }
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 | function once (disposable) {
|
365 | return new Disposable(disposeMemoized, memoized(disposable))
|
366 | }
|
367 |
|
368 | function disposeMemoized (memoized) {
|
369 | if (!memoized.disposed) {
|
370 | memoized.disposed = true;
|
371 | memoized.value = disposeSafely(memoized.disposable);
|
372 | memoized.disposable = void 0;
|
373 | }
|
374 |
|
375 | return memoized.value
|
376 | }
|
377 |
|
378 | function memoized (disposable) {
|
379 | return { disposed: false, disposable: disposable, value: void 0 }
|
380 | }
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | function fatalError (e) {
|
387 | setTimeout(function () {
|
388 | throw e
|
389 | }, 0);
|
390 | }
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | function PropagateTask (run, value, sink) {
|
397 | this._run = run;
|
398 | this.value = value;
|
399 | this.sink = sink;
|
400 | this.active = true;
|
401 | }
|
402 |
|
403 | PropagateTask.event = function (value, sink) {
|
404 | return new PropagateTask(emit, value, sink)
|
405 | };
|
406 |
|
407 | PropagateTask.end = function (value, sink) {
|
408 | return new PropagateTask(end, value, sink)
|
409 | };
|
410 |
|
411 | PropagateTask.error = function (value, sink) {
|
412 | return new PropagateTask(error, value, sink)
|
413 | };
|
414 |
|
415 | PropagateTask.prototype.dispose = function () {
|
416 | this.active = false;
|
417 | };
|
418 |
|
419 | PropagateTask.prototype.run = function (t) {
|
420 | if (!this.active) {
|
421 | return
|
422 | }
|
423 | this._run(t, this.value, this.sink);
|
424 | };
|
425 |
|
426 | PropagateTask.prototype.error = function (t, e) {
|
427 | if (!this.active) {
|
428 | return fatalError(e)
|
429 | }
|
430 | this.sink.error(t, e);
|
431 | };
|
432 |
|
433 | function error (t, e, sink) {
|
434 | sink.error(t, e);
|
435 | }
|
436 |
|
437 | function emit (t, x, sink) {
|
438 | sink.event(t, x);
|
439 | }
|
440 |
|
441 | function end (t, x, sink) {
|
442 | sink.end(t, x);
|
443 | }
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 | function of (x) {
|
455 | return new Stream(new Just(x))
|
456 | }
|
457 |
|
458 | function Just (x) {
|
459 | this.value = x;
|
460 | }
|
461 |
|
462 | Just.prototype.run = function (sink, scheduler) {
|
463 | return scheduler.asap(new PropagateTask(runJust, this.value, sink))
|
464 | };
|
465 |
|
466 | function runJust (t, x, sink) {
|
467 | sink.event(t, x);
|
468 | sink.end(t, void 0);
|
469 | }
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 | function empty () {
|
476 | return EMPTY
|
477 | }
|
478 |
|
479 | function EmptySource () {}
|
480 |
|
481 | EmptySource.prototype.run = function (sink, scheduler) {
|
482 | var task = PropagateTask.end(void 0, sink);
|
483 | scheduler.asap(task);
|
484 |
|
485 | return create(disposeEmpty, task)
|
486 | };
|
487 |
|
488 | function disposeEmpty (task) {
|
489 | return task.dispose()
|
490 | }
|
491 |
|
492 | var EMPTY = new Stream(new EmptySource());
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 | function never () {
|
499 | return NEVER
|
500 | }
|
501 |
|
502 | function NeverSource () {}
|
503 |
|
504 | NeverSource.prototype.run = function () {
|
505 | return empty$1()
|
506 | };
|
507 |
|
508 | var NEVER = new Stream(new NeverSource());
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 | function fromArray (a) {
|
515 | return new Stream(new ArraySource(a))
|
516 | }
|
517 |
|
518 | function ArraySource (a) {
|
519 | this.array = a;
|
520 | }
|
521 |
|
522 | ArraySource.prototype.run = function (sink, scheduler) {
|
523 | return scheduler.asap(new PropagateTask(runProducer, this.array, sink))
|
524 | };
|
525 |
|
526 | function runProducer (t, array, sink) {
|
527 | for (var i = 0, l = array.length; i < l && this.active; ++i) {
|
528 | sink.event(t, array[i]);
|
529 | }
|
530 |
|
531 | this.active && sink.end(t);
|
532 | }
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 | var iteratorSymbol;
|
540 |
|
541 |
|
542 | if (typeof Set === 'function' && typeof new Set()['@@iterator'] === 'function') {
|
543 | iteratorSymbol = '@@iterator';
|
544 | } else {
|
545 | iteratorSymbol = typeof Symbol === 'function' && Symbol.iterator ||
|
546 | '_es6shim_iterator_';
|
547 | }
|
548 |
|
549 | function isIterable (o) {
|
550 | return typeof o[iteratorSymbol] === 'function'
|
551 | }
|
552 |
|
553 | function getIterator (o) {
|
554 | return o[iteratorSymbol]()
|
555 | }
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
|
561 | function fromIterable (iterable) {
|
562 | return new Stream(new IterableSource(iterable))
|
563 | }
|
564 |
|
565 | function IterableSource (iterable) {
|
566 | this.iterable = iterable;
|
567 | }
|
568 |
|
569 | IterableSource.prototype.run = function (sink, scheduler) {
|
570 | return scheduler.asap(new PropagateTask(runProducer$1, getIterator(this.iterable), sink))
|
571 | };
|
572 |
|
573 | function runProducer$1 (t, iterator, sink) {
|
574 | var r = iterator.next();
|
575 |
|
576 | while (!r.done && this.active) {
|
577 | sink.event(t, r.value);
|
578 | r = iterator.next();
|
579 | }
|
580 |
|
581 | sink.end(t, r.value);
|
582 | }
|
583 |
|
584 | function symbolObservablePonyfill(root) {
|
585 | var result;
|
586 | var Symbol = root.Symbol;
|
587 |
|
588 | if (typeof Symbol === 'function') {
|
589 | if (Symbol.observable) {
|
590 | result = Symbol.observable;
|
591 | } else {
|
592 | result = Symbol('observable');
|
593 | Symbol.observable = result;
|
594 | }
|
595 | } else {
|
596 | result = '@@observable';
|
597 | }
|
598 |
|
599 | return result;
|
600 | }
|
601 |
|
602 |
|
603 | var root;
|
604 |
|
605 | if (typeof self !== 'undefined') {
|
606 | root = self;
|
607 | } else if (typeof window !== 'undefined') {
|
608 | root = window;
|
609 | } else if (typeof global !== 'undefined') {
|
610 | root = global;
|
611 | } else if (typeof module !== 'undefined') {
|
612 | root = module;
|
613 | } else {
|
614 | root = Function('return this')();
|
615 | }
|
616 |
|
617 | var result = symbolObservablePonyfill(root);
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 | function getObservable (o) {
|
624 | var obs = null;
|
625 | if (o) {
|
626 |
|
627 | var method = o[result];
|
628 | if (typeof method === 'function') {
|
629 | obs = method.call(o);
|
630 | if (!(obs && typeof obs.subscribe === 'function')) {
|
631 | throw new TypeError('invalid observable ' + obs)
|
632 | }
|
633 | }
|
634 | }
|
635 |
|
636 | return obs
|
637 | }
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 | function tryEvent (t, x, sink) {
|
644 | try {
|
645 | sink.event(t, x);
|
646 | } catch (e) {
|
647 | sink.error(t, e);
|
648 | }
|
649 | }
|
650 |
|
651 | function tryEnd (t, x, sink) {
|
652 | try {
|
653 | sink.end(t, x);
|
654 | } catch (e) {
|
655 | sink.error(t, e);
|
656 | }
|
657 | }
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 | function fromObservable (observable) {
|
664 | return new Stream(new ObservableSource(observable))
|
665 | }
|
666 |
|
667 | function ObservableSource (observable) {
|
668 | this.observable = observable;
|
669 | }
|
670 |
|
671 | ObservableSource.prototype.run = function (sink, scheduler) {
|
672 | var sub = this.observable.subscribe(new SubscriberSink(sink, scheduler));
|
673 | if (typeof sub === 'function') {
|
674 | return create(sub)
|
675 | } else if (sub && typeof sub.unsubscribe === 'function') {
|
676 | return create(unsubscribe, sub)
|
677 | }
|
678 |
|
679 | throw new TypeError('Observable returned invalid subscription ' + String(sub))
|
680 | };
|
681 |
|
682 | function SubscriberSink (sink, scheduler) {
|
683 | this.sink = sink;
|
684 | this.scheduler = scheduler;
|
685 | }
|
686 |
|
687 | SubscriberSink.prototype.next = function (x) {
|
688 | tryEvent(this.scheduler.now(), x, this.sink);
|
689 | };
|
690 |
|
691 | SubscriberSink.prototype.complete = function (x) {
|
692 | tryEnd(this.scheduler.now(), x, this.sink);
|
693 | };
|
694 |
|
695 | SubscriberSink.prototype.error = function (e) {
|
696 | this.sink.error(this.scheduler.now(), e);
|
697 | };
|
698 |
|
699 | function unsubscribe (subscription) {
|
700 | return subscription.unsubscribe()
|
701 | }
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 | function from (a) {
|
708 | if (a instanceof Stream) {
|
709 | return a
|
710 | }
|
711 |
|
712 | var observable = getObservable(a);
|
713 | if (observable != null) {
|
714 | return fromObservable(observable)
|
715 | }
|
716 |
|
717 | if (Array.isArray(a) || isArrayLike(a)) {
|
718 | return fromArray(a)
|
719 | }
|
720 |
|
721 | if (isIterable(a)) {
|
722 | return fromIterable(a)
|
723 | }
|
724 |
|
725 | throw new TypeError('from(x) must be observable, iterable, or array-like: ' + a)
|
726 | }
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 | function periodic (period, deprecatedValue) {
|
739 | return new Stream(new Periodic(period, deprecatedValue))
|
740 | }
|
741 |
|
742 | function Periodic (period, value) {
|
743 | this.period = period;
|
744 | this.value = value;
|
745 | }
|
746 |
|
747 | Periodic.prototype.run = function (sink, scheduler) {
|
748 | return scheduler.periodic(this.period, PropagateTask.event(this.value, sink))
|
749 | };
|
750 |
|
751 |
|
752 |
|
753 |
|
754 |
|
755 | function ScheduledTask (delay, period, task, scheduler) {
|
756 | this.time = delay;
|
757 | this.period = period;
|
758 | this.task = task;
|
759 | this.scheduler = scheduler;
|
760 | this.active = true;
|
761 | }
|
762 |
|
763 | ScheduledTask.prototype.run = function () {
|
764 | return this.task.run(this.time)
|
765 | };
|
766 |
|
767 | ScheduledTask.prototype.error = function (e) {
|
768 | return this.task.error(this.time, e)
|
769 | };
|
770 |
|
771 | ScheduledTask.prototype.dispose = function () {
|
772 | this.scheduler.cancel(this);
|
773 | return this.task.dispose()
|
774 | };
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 | function defer (task) {
|
781 | return Promise.resolve(task).then(runTask)
|
782 | }
|
783 |
|
784 | function runTask (task) {
|
785 | try {
|
786 | return task.run()
|
787 | } catch (e) {
|
788 | return task.error(e)
|
789 | }
|
790 | }
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 | function Scheduler (timer, timeline) {
|
797 | this.timer = timer;
|
798 | this.timeline = timeline;
|
799 |
|
800 | this._timer = null;
|
801 | this._nextArrival = Infinity;
|
802 |
|
803 | var self = this;
|
804 | this._runReadyTasksBound = function () {
|
805 | self._runReadyTasks(self.now());
|
806 | };
|
807 | }
|
808 |
|
809 | Scheduler.prototype.now = function () {
|
810 | return this.timer.now()
|
811 | };
|
812 |
|
813 | Scheduler.prototype.asap = function (task) {
|
814 | return this.schedule(0, -1, task)
|
815 | };
|
816 |
|
817 | Scheduler.prototype.delay = function (delay, task) {
|
818 | return this.schedule(delay, -1, task)
|
819 | };
|
820 |
|
821 | Scheduler.prototype.periodic = function (period, task) {
|
822 | return this.schedule(0, period, task)
|
823 | };
|
824 |
|
825 | Scheduler.prototype.schedule = function (delay, period, task) {
|
826 | var now = this.now();
|
827 | var st = new ScheduledTask(now + Math.max(0, delay), period, task, this);
|
828 |
|
829 | this.timeline.add(st);
|
830 | this._scheduleNextRun(now);
|
831 | return st
|
832 | };
|
833 |
|
834 | Scheduler.prototype.cancel = function (task) {
|
835 | task.active = false;
|
836 | if (this.timeline.remove(task)) {
|
837 | this._reschedule();
|
838 | }
|
839 | };
|
840 |
|
841 | Scheduler.prototype.cancelAll = function (f) {
|
842 | this.timeline.removeAll(f);
|
843 | this._reschedule();
|
844 | };
|
845 |
|
846 | Scheduler.prototype._reschedule = function () {
|
847 | if (this.timeline.isEmpty()) {
|
848 | this._unschedule();
|
849 | } else {
|
850 | this._scheduleNextRun(this.now());
|
851 | }
|
852 | };
|
853 |
|
854 | Scheduler.prototype._unschedule = function () {
|
855 | this.timer.clearTimer(this._timer);
|
856 | this._timer = null;
|
857 | };
|
858 |
|
859 | Scheduler.prototype._scheduleNextRun = function (now) {
|
860 | if (this.timeline.isEmpty()) {
|
861 | return
|
862 | }
|
863 |
|
864 | var nextArrival = this.timeline.nextArrival();
|
865 |
|
866 | if (this._timer === null) {
|
867 | this._scheduleNextArrival(nextArrival, now);
|
868 | } else if (nextArrival < this._nextArrival) {
|
869 | this._unschedule();
|
870 | this._scheduleNextArrival(nextArrival, now);
|
871 | }
|
872 | };
|
873 |
|
874 | Scheduler.prototype._scheduleNextArrival = function (nextArrival, now) {
|
875 | this._nextArrival = nextArrival;
|
876 | var delay = Math.max(0, nextArrival - now);
|
877 | this._timer = this.timer.setTimer(this._runReadyTasksBound, delay);
|
878 | };
|
879 |
|
880 | Scheduler.prototype._runReadyTasks = function (now) {
|
881 | this._timer = null;
|
882 | this.timeline.runTasks(now, runTask);
|
883 | this._scheduleNextRun(this.now());
|
884 | };
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 | function ClockTimer () {}
|
893 |
|
894 | ClockTimer.prototype.now = Date.now;
|
895 |
|
896 | ClockTimer.prototype.setTimer = function (f, dt) {
|
897 | return dt <= 0 ? runAsap(f) : setTimeout(f, dt)
|
898 | };
|
899 |
|
900 | ClockTimer.prototype.clearTimer = function (t) {
|
901 | return t instanceof Asap ? t.cancel() : clearTimeout(t)
|
902 | };
|
903 |
|
904 | function Asap (f) {
|
905 | this.f = f;
|
906 | this.active = true;
|
907 | }
|
908 |
|
909 | Asap.prototype.run = function () {
|
910 | return this.active && this.f()
|
911 | };
|
912 |
|
913 | Asap.prototype.error = function (e) {
|
914 | throw e
|
915 | };
|
916 |
|
917 | Asap.prototype.cancel = function () {
|
918 | this.active = false;
|
919 | };
|
920 |
|
921 | function runAsap (f) {
|
922 | var task = new Asap(f);
|
923 | defer(task);
|
924 | return task
|
925 | }
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 | function Timeline () {
|
932 | this.tasks = [];
|
933 | }
|
934 |
|
935 | Timeline.prototype.nextArrival = function () {
|
936 | return this.isEmpty() ? Infinity : this.tasks[0].time
|
937 | };
|
938 |
|
939 | Timeline.prototype.isEmpty = function () {
|
940 | return this.tasks.length === 0
|
941 | };
|
942 |
|
943 | Timeline.prototype.add = function (st) {
|
944 | insertByTime(st, this.tasks);
|
945 | };
|
946 |
|
947 | Timeline.prototype.remove = function (st) {
|
948 | var i = binarySearch(st.time, this.tasks);
|
949 |
|
950 | if (i >= 0 && i < this.tasks.length) {
|
951 | var at = findIndex(st, this.tasks[i].events);
|
952 | if (at >= 0) {
|
953 | this.tasks[i].events.splice(at, 1);
|
954 | return true
|
955 | }
|
956 | }
|
957 |
|
958 | return false
|
959 | };
|
960 |
|
961 | Timeline.prototype.removeAll = function (f) {
|
962 | var this$1 = this;
|
963 |
|
964 | for (var i = 0, l = this.tasks.length; i < l; ++i) {
|
965 | removeAllFrom(f, this$1.tasks[i]);
|
966 | }
|
967 | };
|
968 |
|
969 | Timeline.prototype.runTasks = function (t, runTask) {
|
970 | var this$1 = this;
|
971 |
|
972 | var tasks = this.tasks;
|
973 | var l = tasks.length;
|
974 | var i = 0;
|
975 |
|
976 | while (i < l && tasks[i].time <= t) {
|
977 | ++i;
|
978 | }
|
979 |
|
980 | this.tasks = tasks.slice(i);
|
981 |
|
982 |
|
983 | for (var j = 0; j < i; ++j) {
|
984 | this$1.tasks = runTasks(runTask, tasks[j], this$1.tasks);
|
985 | }
|
986 | };
|
987 |
|
988 | function runTasks (runTask, timeslot, tasks) {
|
989 | var events = timeslot.events;
|
990 | for (var i = 0; i < events.length; ++i) {
|
991 | var task = events[i];
|
992 |
|
993 | if (task.active) {
|
994 | runTask(task);
|
995 |
|
996 |
|
997 |
|
998 | if (task.period >= 0 && task.active) {
|
999 | task.time = task.time + task.period;
|
1000 | insertByTime(task, tasks);
|
1001 | }
|
1002 | }
|
1003 | }
|
1004 |
|
1005 | return tasks
|
1006 | }
|
1007 |
|
1008 | function insertByTime (task, timeslots) {
|
1009 | var l = timeslots.length;
|
1010 |
|
1011 | if (l === 0) {
|
1012 | timeslots.push(newTimeslot(task.time, [task]));
|
1013 | return
|
1014 | }
|
1015 |
|
1016 | var i = binarySearch(task.time, timeslots);
|
1017 |
|
1018 | if (i >= l) {
|
1019 | timeslots.push(newTimeslot(task.time, [task]));
|
1020 | } else if (task.time === timeslots[i].time) {
|
1021 | timeslots[i].events.push(task);
|
1022 | } else {
|
1023 | timeslots.splice(i, 0, newTimeslot(task.time, [task]));
|
1024 | }
|
1025 | }
|
1026 |
|
1027 | function removeAllFrom (f, timeslot) {
|
1028 | timeslot.events = removeAll(f, timeslot.events);
|
1029 | }
|
1030 |
|
1031 | function binarySearch (t, sortedArray) {
|
1032 | var lo = 0;
|
1033 | var hi = sortedArray.length;
|
1034 | var mid, y;
|
1035 |
|
1036 | while (lo < hi) {
|
1037 | mid = Math.floor((lo + hi) / 2);
|
1038 | y = sortedArray[mid];
|
1039 |
|
1040 | if (t === y.time) {
|
1041 | return mid
|
1042 | } else if (t < y.time) {
|
1043 | hi = mid;
|
1044 | } else {
|
1045 | lo = mid + 1;
|
1046 | }
|
1047 | }
|
1048 | return hi
|
1049 | }
|
1050 |
|
1051 | function newTimeslot (t, events) {
|
1052 | return { time: t, events: events }
|
1053 | }
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 | var defaultScheduler = new Scheduler(new ClockTimer(), new Timeline());
|
1060 |
|
1061 |
|
1062 |
|
1063 |
|
1064 |
|
1065 | function subscribe (subscriber, stream) {
|
1066 | if (Object(subscriber) !== subscriber) {
|
1067 | throw new TypeError('subscriber must be an object')
|
1068 | }
|
1069 |
|
1070 | var disposable = settable();
|
1071 | var observer = new SubscribeObserver(fatalError, subscriber, disposable);
|
1072 |
|
1073 | disposable.setDisposable(stream.source.run(observer, defaultScheduler));
|
1074 |
|
1075 | return new Subscription(disposable)
|
1076 | }
|
1077 |
|
1078 | function SubscribeObserver (fatalError$$1, subscriber, disposable) {
|
1079 | this.fatalError = fatalError$$1;
|
1080 | this.subscriber = subscriber;
|
1081 | this.disposable = disposable;
|
1082 | }
|
1083 |
|
1084 | SubscribeObserver.prototype.event = function (t, x) {
|
1085 | if (!this.disposable.disposed && typeof this.subscriber.next === 'function') {
|
1086 | this.subscriber.next(x);
|
1087 | }
|
1088 | };
|
1089 |
|
1090 | SubscribeObserver.prototype.end = function (t, x) {
|
1091 | if (!this.disposable.disposed) {
|
1092 | var s = this.subscriber;
|
1093 | var fatalError$$1 = this.fatalError;
|
1094 | Promise.resolve(this.disposable.dispose()).then(function () {
|
1095 | if (typeof s.complete === 'function') {
|
1096 | s.complete(x);
|
1097 | }
|
1098 | }).catch(function (e) {
|
1099 | throwError(e, s, fatalError$$1);
|
1100 | });
|
1101 | }
|
1102 | };
|
1103 |
|
1104 | SubscribeObserver.prototype.error = function (t, e) {
|
1105 | var s = this.subscriber;
|
1106 | var fatalError$$1 = this.fatalError;
|
1107 | Promise.resolve(this.disposable.dispose()).then(function () {
|
1108 | throwError(e, s, fatalError$$1);
|
1109 | });
|
1110 | };
|
1111 |
|
1112 | function Subscription (disposable) {
|
1113 | this.disposable = disposable;
|
1114 | }
|
1115 |
|
1116 | Subscription.prototype.unsubscribe = function () {
|
1117 | this.disposable.dispose();
|
1118 | };
|
1119 |
|
1120 | function throwError (e1, subscriber, throwError) {
|
1121 | if (typeof subscriber.error === 'function') {
|
1122 | try {
|
1123 | subscriber.error(e1);
|
1124 | } catch (e2) {
|
1125 | throwError(e2);
|
1126 | }
|
1127 | } else {
|
1128 | throwError(e1);
|
1129 | }
|
1130 | }
|
1131 |
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 | function thru (f, stream) {
|
1137 | return f(stream)
|
1138 | }
|
1139 |
|
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 | function EventTargetSource (event, source, capture) {
|
1145 | this.event = event;
|
1146 | this.source = source;
|
1147 | this.capture = capture;
|
1148 | }
|
1149 |
|
1150 | EventTargetSource.prototype.run = function (sink, scheduler) {
|
1151 | function addEvent (e) {
|
1152 | tryEvent(scheduler.now(), e, sink);
|
1153 | }
|
1154 |
|
1155 | this.source.addEventListener(this.event, addEvent, this.capture);
|
1156 |
|
1157 | return create(disposeEventTarget,
|
1158 | { target: this, addEvent: addEvent })
|
1159 | };
|
1160 |
|
1161 | function disposeEventTarget (info) {
|
1162 | var target = info.target;
|
1163 | target.source.removeEventListener(target.event, info.addEvent, target.capture);
|
1164 | }
|
1165 |
|
1166 |
|
1167 |
|
1168 |
|
1169 |
|
1170 | function DeferredSink (sink) {
|
1171 | this.sink = sink;
|
1172 | this.events = [];
|
1173 | this.active = true;
|
1174 | }
|
1175 |
|
1176 | DeferredSink.prototype.event = function (t, x) {
|
1177 | if (!this.active) {
|
1178 | return
|
1179 | }
|
1180 |
|
1181 | if (this.events.length === 0) {
|
1182 | defer(new PropagateAllTask(this.sink, t, this.events));
|
1183 | }
|
1184 |
|
1185 | this.events.push({ time: t, value: x });
|
1186 | };
|
1187 |
|
1188 | DeferredSink.prototype.end = function (t, x) {
|
1189 | if (!this.active) {
|
1190 | return
|
1191 | }
|
1192 |
|
1193 | this._end(new EndTask(t, x, this.sink));
|
1194 | };
|
1195 |
|
1196 | DeferredSink.prototype.error = function (t, e) {
|
1197 | this._end(new ErrorTask(t, e, this.sink));
|
1198 | };
|
1199 |
|
1200 | DeferredSink.prototype._end = function (task) {
|
1201 | this.active = false;
|
1202 | defer(task);
|
1203 | };
|
1204 |
|
1205 | function PropagateAllTask (sink, time, events) {
|
1206 | this.sink = sink;
|
1207 | this.events = events;
|
1208 | this.time = time;
|
1209 | }
|
1210 |
|
1211 | PropagateAllTask.prototype.run = function () {
|
1212 | var this$1 = this;
|
1213 |
|
1214 | var events = this.events;
|
1215 | var sink = this.sink;
|
1216 | var event;
|
1217 |
|
1218 | for (var i = 0, l = events.length; i < l; ++i) {
|
1219 | event = events[i];
|
1220 | this$1.time = event.time;
|
1221 | sink.event(event.time, event.value);
|
1222 | }
|
1223 |
|
1224 | events.length = 0;
|
1225 | };
|
1226 |
|
1227 | PropagateAllTask.prototype.error = function (e) {
|
1228 | this.sink.error(this.time, e);
|
1229 | };
|
1230 |
|
1231 | function EndTask (t, x, sink) {
|
1232 | this.time = t;
|
1233 | this.value = x;
|
1234 | this.sink = sink;
|
1235 | }
|
1236 |
|
1237 | EndTask.prototype.run = function () {
|
1238 | this.sink.end(this.time, this.value);
|
1239 | };
|
1240 |
|
1241 | EndTask.prototype.error = function (e) {
|
1242 | this.sink.error(this.time, e);
|
1243 | };
|
1244 |
|
1245 | function ErrorTask (t, e, sink) {
|
1246 | this.time = t;
|
1247 | this.value = e;
|
1248 | this.sink = sink;
|
1249 | }
|
1250 |
|
1251 | ErrorTask.prototype.run = function () {
|
1252 | this.sink.error(this.time, this.value);
|
1253 | };
|
1254 |
|
1255 | ErrorTask.prototype.error = function (e) {
|
1256 | throw e
|
1257 | };
|
1258 |
|
1259 |
|
1260 |
|
1261 |
|
1262 |
|
1263 | function EventEmitterSource (event, source) {
|
1264 | this.event = event;
|
1265 | this.source = source;
|
1266 | }
|
1267 |
|
1268 | EventEmitterSource.prototype.run = function (sink, scheduler) {
|
1269 |
|
1270 |
|
1271 |
|
1272 |
|
1273 |
|
1274 | var dsink = new DeferredSink(sink);
|
1275 |
|
1276 | function addEventVariadic (a) {
|
1277 | var arguments$1 = arguments;
|
1278 |
|
1279 | var l = arguments.length;
|
1280 | if (l > 1) {
|
1281 | var arr = new Array(l);
|
1282 | for (var i = 0; i < l; ++i) {
|
1283 | arr[i] = arguments$1[i];
|
1284 | }
|
1285 | tryEvent(scheduler.now(), arr, dsink);
|
1286 | } else {
|
1287 | tryEvent(scheduler.now(), a, dsink);
|
1288 | }
|
1289 | }
|
1290 |
|
1291 | this.source.addListener(this.event, addEventVariadic);
|
1292 |
|
1293 | return create(disposeEventEmitter, { target: this, addEvent: addEventVariadic })
|
1294 | };
|
1295 |
|
1296 | function disposeEventEmitter (info) {
|
1297 | var target = info.target;
|
1298 | target.source.removeListener(target.event, info.addEvent);
|
1299 | }
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 | function fromEvent (event, source, capture) {
|
1315 | var s;
|
1316 |
|
1317 | if (typeof source.addEventListener === 'function' && typeof source.removeEventListener === 'function') {
|
1318 | if (arguments.length < 3) {
|
1319 | capture = false;
|
1320 | }
|
1321 |
|
1322 | s = new EventTargetSource(event, source, capture);
|
1323 | } else if (typeof source.addListener === 'function' && typeof source.removeListener === 'function') {
|
1324 | s = new EventEmitterSource(event, source);
|
1325 | } else {
|
1326 | throw new Error('source must support addEventListener/removeEventListener or addListener/removeListener')
|
1327 | }
|
1328 |
|
1329 | return new Stream(s)
|
1330 | }
|
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 | function withDefaultScheduler (source) {
|
1337 | return withScheduler(source, defaultScheduler)
|
1338 | }
|
1339 |
|
1340 | function withScheduler (source, scheduler) {
|
1341 | return new Promise(function (resolve, reject) {
|
1342 | runSource(source, scheduler, resolve, reject);
|
1343 | })
|
1344 | }
|
1345 |
|
1346 | function runSource (source, scheduler, resolve, reject) {
|
1347 | var disposable = settable();
|
1348 | var observer = new Drain(resolve, reject, disposable);
|
1349 |
|
1350 | disposable.setDisposable(source.run(observer, scheduler));
|
1351 | }
|
1352 |
|
1353 | function Drain (end, error, disposable) {
|
1354 | this._end = end;
|
1355 | this._error = error;
|
1356 | this._disposable = disposable;
|
1357 | this.active = true;
|
1358 | }
|
1359 |
|
1360 | Drain.prototype.event = function (t, x) {};
|
1361 |
|
1362 | Drain.prototype.end = function (t, x) {
|
1363 | if (!this.active) {
|
1364 | return
|
1365 | }
|
1366 | this.active = false;
|
1367 | disposeThen(this._end, this._error, this._disposable, x);
|
1368 | };
|
1369 |
|
1370 | Drain.prototype.error = function (t, e) {
|
1371 | this.active = false;
|
1372 | disposeThen(this._error, this._error, this._disposable, e);
|
1373 | };
|
1374 |
|
1375 | function disposeThen (end, error, disposable, x) {
|
1376 | Promise.resolve(disposable.dispose()).then(function () {
|
1377 | end(x);
|
1378 | }, error);
|
1379 | }
|
1380 |
|
1381 |
|
1382 |
|
1383 |
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 |
|
1389 |
|
1390 |
|
1391 | function Pipe (sink) {
|
1392 | this.sink = sink;
|
1393 | }
|
1394 |
|
1395 | Pipe.prototype.event = function (t, x) {
|
1396 | return this.sink.event(t, x)
|
1397 | };
|
1398 |
|
1399 | Pipe.prototype.end = function (t, x) {
|
1400 | return this.sink.end(t, x)
|
1401 | };
|
1402 |
|
1403 | Pipe.prototype.error = function (t, e) {
|
1404 | return this.sink.error(t, e)
|
1405 | };
|
1406 |
|
1407 |
|
1408 |
|
1409 |
|
1410 |
|
1411 | function Filter (p, source) {
|
1412 | this.p = p;
|
1413 | this.source = source;
|
1414 | }
|
1415 |
|
1416 |
|
1417 |
|
1418 |
|
1419 |
|
1420 |
|
1421 |
|
1422 | Filter.create = function createFilter (p, source) {
|
1423 | if (source instanceof Filter) {
|
1424 | return new Filter(and(source.p, p), source.source)
|
1425 | }
|
1426 |
|
1427 | return new Filter(p, source)
|
1428 | };
|
1429 |
|
1430 | Filter.prototype.run = function (sink, scheduler) {
|
1431 | return this.source.run(new FilterSink(this.p, sink), scheduler)
|
1432 | };
|
1433 |
|
1434 | function FilterSink (p, sink) {
|
1435 | this.p = p;
|
1436 | this.sink = sink;
|
1437 | }
|
1438 |
|
1439 | FilterSink.prototype.end = Pipe.prototype.end;
|
1440 | FilterSink.prototype.error = Pipe.prototype.error;
|
1441 |
|
1442 | FilterSink.prototype.event = function (t, x) {
|
1443 | var p = this.p;
|
1444 | p(x) && this.sink.event(t, x);
|
1445 | };
|
1446 |
|
1447 | function and (p, q) {
|
1448 | return function (x) {
|
1449 | return p(x) && q(x)
|
1450 | }
|
1451 | }
|
1452 |
|
1453 |
|
1454 |
|
1455 |
|
1456 |
|
1457 | function FilterMap (p, f, source) {
|
1458 | this.p = p;
|
1459 | this.f = f;
|
1460 | this.source = source;
|
1461 | }
|
1462 |
|
1463 | FilterMap.prototype.run = function (sink, scheduler) {
|
1464 | return this.source.run(new FilterMapSink(this.p, this.f, sink), scheduler)
|
1465 | };
|
1466 |
|
1467 | function FilterMapSink (p, f, sink) {
|
1468 | this.p = p;
|
1469 | this.f = f;
|
1470 | this.sink = sink;
|
1471 | }
|
1472 |
|
1473 | FilterMapSink.prototype.event = function (t, x) {
|
1474 | var f = this.f;
|
1475 | var p = this.p;
|
1476 | p(x) && this.sink.event(t, f(x));
|
1477 | };
|
1478 |
|
1479 | FilterMapSink.prototype.end = Pipe.prototype.end;
|
1480 | FilterMapSink.prototype.error = Pipe.prototype.error;
|
1481 |
|
1482 |
|
1483 |
|
1484 |
|
1485 |
|
1486 | function Map (f, source) {
|
1487 | this.f = f;
|
1488 | this.source = source;
|
1489 | }
|
1490 |
|
1491 |
|
1492 |
|
1493 |
|
1494 |
|
1495 |
|
1496 |
|
1497 |
|
1498 | Map.create = function createMap (f, source) {
|
1499 | if (source instanceof Map) {
|
1500 | return new Map(compose(f, source.f), source.source)
|
1501 | }
|
1502 |
|
1503 | if (source instanceof Filter) {
|
1504 | return new FilterMap(source.p, f, source.source)
|
1505 | }
|
1506 |
|
1507 | return new Map(f, source)
|
1508 | };
|
1509 |
|
1510 | Map.prototype.run = function (sink, scheduler) {
|
1511 | return this.source.run(new MapSink(this.f, sink), scheduler)
|
1512 | };
|
1513 |
|
1514 | function MapSink (f, sink) {
|
1515 | this.f = f;
|
1516 | this.sink = sink;
|
1517 | }
|
1518 |
|
1519 | MapSink.prototype.end = Pipe.prototype.end;
|
1520 | MapSink.prototype.error = Pipe.prototype.error;
|
1521 |
|
1522 | MapSink.prototype.event = function (t, x) {
|
1523 | var f = this.f;
|
1524 | this.sink.event(t, f(x));
|
1525 | };
|
1526 |
|
1527 |
|
1528 |
|
1529 |
|
1530 |
|
1531 |
|
1532 |
|
1533 |
|
1534 |
|
1535 |
|
1536 |
|
1537 | function map$2 (f, stream) {
|
1538 | return new Stream(Map.create(f, stream.source))
|
1539 | }
|
1540 |
|
1541 |
|
1542 |
|
1543 |
|
1544 |
|
1545 |
|
1546 |
|
1547 | function constant (x, stream) {
|
1548 | return map$2(function () {
|
1549 | return x
|
1550 | }, stream)
|
1551 | }
|
1552 |
|
1553 |
|
1554 |
|
1555 |
|
1556 |
|
1557 |
|
1558 |
|
1559 |
|
1560 | function tap (f, stream) {
|
1561 | return new Stream(new Tap(f, stream.source))
|
1562 | }
|
1563 |
|
1564 | function Tap (f, source) {
|
1565 | this.source = source;
|
1566 | this.f = f;
|
1567 | }
|
1568 |
|
1569 | Tap.prototype.run = function (sink, scheduler) {
|
1570 | return this.source.run(new TapSink(this.f, sink), scheduler)
|
1571 | };
|
1572 |
|
1573 | function TapSink (f, sink) {
|
1574 | this.sink = sink;
|
1575 | this.f = f;
|
1576 | }
|
1577 |
|
1578 | TapSink.prototype.end = Pipe.prototype.end;
|
1579 | TapSink.prototype.error = Pipe.prototype.error;
|
1580 |
|
1581 | TapSink.prototype.event = function (t, x) {
|
1582 | var f = this.f;
|
1583 | f(x);
|
1584 | this.sink.event(t, x);
|
1585 | };
|
1586 |
|
1587 |
|
1588 |
|
1589 |
|
1590 |
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 |
|
1599 | function observe (f, stream) {
|
1600 | return drain(tap(f, stream))
|
1601 | }
|
1602 |
|
1603 |
|
1604 |
|
1605 |
|
1606 |
|
1607 |
|
1608 |
|
1609 | function drain (stream) {
|
1610 | return withDefaultScheduler(stream.source)
|
1611 | }
|
1612 |
|
1613 |
|
1614 |
|
1615 |
|
1616 |
|
1617 |
|
1618 |
|
1619 |
|
1620 |
|
1621 |
|
1622 |
|
1623 |
|
1624 |
|
1625 |
|
1626 |
|
1627 |
|
1628 | function loop (stepper, seed, stream) {
|
1629 | return new Stream(new Loop(stepper, seed, stream.source))
|
1630 | }
|
1631 |
|
1632 | function Loop (stepper, seed, source) {
|
1633 | this.step = stepper;
|
1634 | this.seed = seed;
|
1635 | this.source = source;
|
1636 | }
|
1637 |
|
1638 | Loop.prototype.run = function (sink, scheduler) {
|
1639 | return this.source.run(new LoopSink(this.step, this.seed, sink), scheduler)
|
1640 | };
|
1641 |
|
1642 | function LoopSink (stepper, seed, sink) {
|
1643 | this.step = stepper;
|
1644 | this.seed = seed;
|
1645 | this.sink = sink;
|
1646 | }
|
1647 |
|
1648 | LoopSink.prototype.error = Pipe.prototype.error;
|
1649 |
|
1650 | LoopSink.prototype.event = function (t, x) {
|
1651 | var result = this.step(this.seed, x);
|
1652 | this.seed = result.seed;
|
1653 | this.sink.event(t, result.value);
|
1654 | };
|
1655 |
|
1656 | LoopSink.prototype.end = function (t) {
|
1657 | this.sink.end(t, this.seed);
|
1658 | };
|
1659 |
|
1660 |
|
1661 |
|
1662 |
|
1663 |
|
1664 |
|
1665 |
|
1666 |
|
1667 |
|
1668 |
|
1669 |
|
1670 |
|
1671 |
|
1672 | function scan (f, initial, stream) {
|
1673 | return new Stream(new Scan(f, initial, stream.source))
|
1674 | }
|
1675 |
|
1676 | function Scan (f, z, source) {
|
1677 | this.source = source;
|
1678 | this.f = f;
|
1679 | this.value = z;
|
1680 | }
|
1681 |
|
1682 | Scan.prototype.run = function (sink, scheduler) {
|
1683 | var d1 = scheduler.asap(PropagateTask.event(this.value, sink));
|
1684 | var d2 = this.source.run(new ScanSink(this.f, this.value, sink), scheduler);
|
1685 | return all([d1, d2])
|
1686 | };
|
1687 |
|
1688 | function ScanSink (f, z, sink) {
|
1689 | this.f = f;
|
1690 | this.value = z;
|
1691 | this.sink = sink;
|
1692 | }
|
1693 |
|
1694 | ScanSink.prototype.event = function (t, x) {
|
1695 | var f = this.f;
|
1696 | this.value = f(this.value, x);
|
1697 | this.sink.event(t, this.value);
|
1698 | };
|
1699 |
|
1700 | ScanSink.prototype.error = Pipe.prototype.error;
|
1701 | ScanSink.prototype.end = Pipe.prototype.end;
|
1702 |
|
1703 |
|
1704 |
|
1705 |
|
1706 |
|
1707 |
|
1708 |
|
1709 |
|
1710 |
|
1711 |
|
1712 | function reduce$1 (f, initial, stream) {
|
1713 | return withDefaultScheduler(new Reduce(f, initial, stream.source))
|
1714 | }
|
1715 |
|
1716 | function Reduce (f, z, source) {
|
1717 | this.source = source;
|
1718 | this.f = f;
|
1719 | this.value = z;
|
1720 | }
|
1721 |
|
1722 | Reduce.prototype.run = function (sink, scheduler) {
|
1723 | return this.source.run(new ReduceSink(this.f, this.value, sink), scheduler)
|
1724 | };
|
1725 |
|
1726 | function ReduceSink (f, z, sink) {
|
1727 | this.f = f;
|
1728 | this.value = z;
|
1729 | this.sink = sink;
|
1730 | }
|
1731 |
|
1732 | ReduceSink.prototype.event = function (t, x) {
|
1733 | var f = this.f;
|
1734 | this.value = f(this.value, x);
|
1735 | this.sink.event(t, this.value);
|
1736 | };
|
1737 |
|
1738 | ReduceSink.prototype.error = Pipe.prototype.error;
|
1739 |
|
1740 | ReduceSink.prototype.end = function (t) {
|
1741 | this.sink.end(t, this.value);
|
1742 | };
|
1743 |
|
1744 |
|
1745 |
|
1746 |
|
1747 |
|
1748 |
|
1749 |
|
1750 |
|
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 |
|
1758 | function unfold (f, seed) {
|
1759 | return new Stream(new UnfoldSource(f, seed))
|
1760 | }
|
1761 |
|
1762 | function UnfoldSource (f, seed) {
|
1763 | this.f = f;
|
1764 | this.value = seed;
|
1765 | }
|
1766 |
|
1767 | UnfoldSource.prototype.run = function (sink, scheduler) {
|
1768 | return new Unfold(this.f, this.value, sink, scheduler)
|
1769 | };
|
1770 |
|
1771 | function Unfold (f, x, sink, scheduler) {
|
1772 | this.f = f;
|
1773 | this.sink = sink;
|
1774 | this.scheduler = scheduler;
|
1775 | this.active = true;
|
1776 |
|
1777 | var self = this;
|
1778 | function err (e) {
|
1779 | self.sink.error(self.scheduler.now(), e);
|
1780 | }
|
1781 |
|
1782 | function start (unfold) {
|
1783 | return stepUnfold(unfold, x)
|
1784 | }
|
1785 |
|
1786 | Promise.resolve(this).then(start).catch(err);
|
1787 | }
|
1788 |
|
1789 | Unfold.prototype.dispose = function () {
|
1790 | this.active = false;
|
1791 | };
|
1792 |
|
1793 | function stepUnfold (unfold, x) {
|
1794 | var f = unfold.f;
|
1795 | return Promise.resolve(f(x)).then(function (tuple) {
|
1796 | return continueUnfold(unfold, tuple)
|
1797 | })
|
1798 | }
|
1799 |
|
1800 | function continueUnfold (unfold, tuple) {
|
1801 | if (tuple.done) {
|
1802 | unfold.sink.end(unfold.scheduler.now(), tuple.value);
|
1803 | return tuple.value
|
1804 | }
|
1805 |
|
1806 | unfold.sink.event(unfold.scheduler.now(), tuple.value);
|
1807 |
|
1808 | if (!unfold.active) {
|
1809 | return tuple.value
|
1810 | }
|
1811 | return stepUnfold(unfold, tuple.seed)
|
1812 | }
|
1813 |
|
1814 |
|
1815 |
|
1816 |
|
1817 |
|
1818 |
|
1819 |
|
1820 |
|
1821 |
|
1822 |
|
1823 |
|
1824 |
|
1825 | function iterate (f, x) {
|
1826 | return new Stream(new IterateSource(f, x))
|
1827 | }
|
1828 |
|
1829 | function IterateSource (f, x) {
|
1830 | this.f = f;
|
1831 | this.value = x;
|
1832 | }
|
1833 |
|
1834 | IterateSource.prototype.run = function (sink, scheduler) {
|
1835 | return new Iterate(this.f, this.value, sink, scheduler)
|
1836 | };
|
1837 |
|
1838 | function Iterate (f, initial, sink, scheduler) {
|
1839 | this.f = f;
|
1840 | this.sink = sink;
|
1841 | this.scheduler = scheduler;
|
1842 | this.active = true;
|
1843 |
|
1844 | var x = initial;
|
1845 |
|
1846 | var self = this;
|
1847 | function err (e) {
|
1848 | self.sink.error(self.scheduler.now(), e);
|
1849 | }
|
1850 |
|
1851 | function start (iterate) {
|
1852 | return stepIterate(iterate, x)
|
1853 | }
|
1854 |
|
1855 | Promise.resolve(this).then(start).catch(err);
|
1856 | }
|
1857 |
|
1858 | Iterate.prototype.dispose = function () {
|
1859 | this.active = false;
|
1860 | };
|
1861 |
|
1862 | function stepIterate (iterate, x) {
|
1863 | iterate.sink.event(iterate.scheduler.now(), x);
|
1864 |
|
1865 | if (!iterate.active) {
|
1866 | return x
|
1867 | }
|
1868 |
|
1869 | var f = iterate.f;
|
1870 | return Promise.resolve(f(x)).then(function (y) {
|
1871 | return continueIterate(iterate, y)
|
1872 | })
|
1873 | }
|
1874 |
|
1875 | function continueIterate (iterate, x) {
|
1876 | return !iterate.active ? iterate.value : stepIterate(iterate, x)
|
1877 | }
|
1878 |
|
1879 |
|
1880 |
|
1881 |
|
1882 |
|
1883 |
|
1884 |
|
1885 |
|
1886 |
|
1887 |
|
1888 |
|
1889 | function generate (f /*, ...args */) {
|
1890 | return new Stream(new GenerateSource(f, tail(arguments)))
|
1891 | }
|
1892 |
|
1893 | function GenerateSource (f, args) {
|
1894 | this.f = f;
|
1895 | this.args = args;
|
1896 | }
|
1897 |
|
1898 | GenerateSource.prototype.run = function (sink, scheduler) {
|
1899 | return new Generate(this.f.apply(void 0, this.args), sink, scheduler)
|
1900 | };
|
1901 |
|
1902 | function Generate (iterator, sink, scheduler) {
|
1903 | this.iterator = iterator;
|
1904 | this.sink = sink;
|
1905 | this.scheduler = scheduler;
|
1906 | this.active = true;
|
1907 |
|
1908 | var self = this;
|
1909 | function err (e) {
|
1910 | self.sink.error(self.scheduler.now(), e);
|
1911 | }
|
1912 |
|
1913 | Promise.resolve(this).then(next).catch(err);
|
1914 | }
|
1915 |
|
1916 | function next (generate, x) {
|
1917 | return generate.active ? handle(generate, generate.iterator.next(x)) : x
|
1918 | }
|
1919 |
|
1920 | function handle (generate, result) {
|
1921 | if (result.done) {
|
1922 | return generate.sink.end(generate.scheduler.now(), result.value)
|
1923 | }
|
1924 |
|
1925 | return Promise.resolve(result.value).then(function (x) {
|
1926 | return emit$1(generate, x)
|
1927 | }, function (e) {
|
1928 | return error$1(generate, e)
|
1929 | })
|
1930 | }
|
1931 |
|
1932 | function emit$1 (generate, x) {
|
1933 | generate.sink.event(generate.scheduler.now(), x);
|
1934 | return next(generate, x)
|
1935 | }
|
1936 |
|
1937 | function error$1 (generate, e) {
|
1938 | return handle(generate, generate.iterator.throw(e))
|
1939 | }
|
1940 |
|
1941 | Generate.prototype.dispose = function () {
|
1942 | this.active = false;
|
1943 | };
|
1944 |
|
1945 |
|
1946 |
|
1947 |
|
1948 |
|
1949 | function continueWith (f, stream) {
|
1950 | return new Stream(new ContinueWith(f, stream.source))
|
1951 | }
|
1952 |
|
1953 | function ContinueWith (f, source) {
|
1954 | this.f = f;
|
1955 | this.source = source;
|
1956 | }
|
1957 |
|
1958 | ContinueWith.prototype.run = function (sink, scheduler) {
|
1959 | return new ContinueWithSink(this.f, this.source, sink, scheduler)
|
1960 | };
|
1961 |
|
1962 | function ContinueWithSink (f, source, sink, scheduler) {
|
1963 | this.f = f;
|
1964 | this.sink = sink;
|
1965 | this.scheduler = scheduler;
|
1966 | this.active = true;
|
1967 | this.disposable = once(source.run(this, scheduler));
|
1968 | }
|
1969 |
|
1970 | ContinueWithSink.prototype.error = Pipe.prototype.error;
|
1971 |
|
1972 | ContinueWithSink.prototype.event = function (t, x) {
|
1973 | if (!this.active) {
|
1974 | return
|
1975 | }
|
1976 | this.sink.event(t, x);
|
1977 | };
|
1978 |
|
1979 | ContinueWithSink.prototype.end = function (t, x) {
|
1980 | if (!this.active) {
|
1981 | return
|
1982 | }
|
1983 |
|
1984 | tryDispose(t, this.disposable, this.sink);
|
1985 | this._startNext(t, x, this.sink);
|
1986 | };
|
1987 |
|
1988 | ContinueWithSink.prototype._startNext = function (t, x, sink) {
|
1989 | try {
|
1990 | this.disposable = this._continue(this.f, x, sink);
|
1991 | } catch (e) {
|
1992 | sink.error(t, e);
|
1993 | }
|
1994 | };
|
1995 |
|
1996 | ContinueWithSink.prototype._continue = function (f, x, sink) {
|
1997 | return f(x).source.run(sink, this.scheduler)
|
1998 | };
|
1999 |
|
2000 | ContinueWithSink.prototype.dispose = function () {
|
2001 | this.active = false;
|
2002 | return this.disposable.dispose()
|
2003 | };
|
2004 |
|
2005 |
|
2006 |
|
2007 |
|
2008 |
|
2009 |
|
2010 |
|
2011 |
|
2012 |
|
2013 |
|
2014 | function cons$1 (x, stream) {
|
2015 | return concat(of(x), stream)
|
2016 | }
|
2017 |
|
2018 |
|
2019 |
|
2020 |
|
2021 |
|
2022 |
|
2023 |
|
2024 | function concat (left, right) {
|
2025 | return continueWith(function () {
|
2026 | return right
|
2027 | }, left)
|
2028 | }
|
2029 |
|
2030 |
|
2031 |
|
2032 |
|
2033 |
|
2034 | function IndexSink (i, sink) {
|
2035 | this.sink = sink;
|
2036 | this.index = i;
|
2037 | this.active = true;
|
2038 | this.value = void 0;
|
2039 | }
|
2040 |
|
2041 | IndexSink.prototype.event = function (t, x) {
|
2042 | if (!this.active) {
|
2043 | return
|
2044 | }
|
2045 | this.value = x;
|
2046 | this.sink.event(t, this);
|
2047 | };
|
2048 |
|
2049 | IndexSink.prototype.end = function (t, x) {
|
2050 | if (!this.active) {
|
2051 | return
|
2052 | }
|
2053 | this.active = false;
|
2054 | this.sink.end(t, { index: this.index, value: x });
|
2055 | };
|
2056 |
|
2057 | IndexSink.prototype.error = Pipe.prototype.error;
|
2058 |
|
2059 |
|
2060 |
|
2061 |
|
2062 |
|
2063 | function invoke (f, args) {
|
2064 |
|
2065 | switch (args.length) {
|
2066 | case 0: return f()
|
2067 | case 1: return f(args[0])
|
2068 | case 2: return f(args[0], args[1])
|
2069 | case 3: return f(args[0], args[1], args[2])
|
2070 | case 4: return f(args[0], args[1], args[2], args[3])
|
2071 | case 5: return f(args[0], args[1], args[2], args[3], args[4])
|
2072 | default:
|
2073 | return f.apply(void 0, args)
|
2074 | }
|
2075 | }
|
2076 |
|
2077 |
|
2078 |
|
2079 |
|
2080 |
|
2081 | var map$3 = map;
|
2082 | var tail$1 = tail;
|
2083 |
|
2084 |
|
2085 |
|
2086 |
|
2087 |
|
2088 |
|
2089 |
|
2090 | function combine (f /*, ...streams */) {
|
2091 | return combineArray(f, tail$1(arguments))
|
2092 | }
|
2093 |
|
2094 |
|
2095 |
|
2096 |
|
2097 |
|
2098 |
|
2099 |
|
2100 |
|
2101 | function combineArray (f, streams) {
|
2102 | var l = streams.length;
|
2103 | return l === 0 ? empty()
|
2104 | : l === 1 ? map$2(f, streams[0])
|
2105 | : new Stream(combineSources(f, streams))
|
2106 | }
|
2107 |
|
2108 | function combineSources (f, streams) {
|
2109 | return new Combine(f, map$3(getSource, streams))
|
2110 | }
|
2111 |
|
2112 | function getSource (stream) {
|
2113 | return stream.source
|
2114 | }
|
2115 |
|
2116 | function Combine (f, sources) {
|
2117 | this.f = f;
|
2118 | this.sources = sources;
|
2119 | }
|
2120 |
|
2121 | Combine.prototype.run = function (sink, scheduler) {
|
2122 | var this$1 = this;
|
2123 |
|
2124 | var l = this.sources.length;
|
2125 | var disposables = new Array(l);
|
2126 | var sinks = new Array(l);
|
2127 |
|
2128 | var mergeSink = new CombineSink(disposables, sinks, sink, this.f);
|
2129 |
|
2130 | for (var indexSink, i = 0; i < l; ++i) {
|
2131 | indexSink = sinks[i] = new IndexSink(i, mergeSink);
|
2132 | disposables[i] = this$1.sources[i].run(indexSink, scheduler);
|
2133 | }
|
2134 |
|
2135 | return all(disposables)
|
2136 | };
|
2137 |
|
2138 | function CombineSink (disposables, sinks, sink, f) {
|
2139 | var this$1 = this;
|
2140 |
|
2141 | this.sink = sink;
|
2142 | this.disposables = disposables;
|
2143 | this.sinks = sinks;
|
2144 | this.f = f;
|
2145 |
|
2146 | var l = sinks.length;
|
2147 | this.awaiting = l;
|
2148 | this.values = new Array(l);
|
2149 | this.hasValue = new Array(l);
|
2150 | for (var i = 0; i < l; ++i) {
|
2151 | this$1.hasValue[i] = false;
|
2152 | }
|
2153 |
|
2154 | this.activeCount = sinks.length;
|
2155 | }
|
2156 |
|
2157 | CombineSink.prototype.error = Pipe.prototype.error;
|
2158 |
|
2159 | CombineSink.prototype.event = function (t, indexedValue) {
|
2160 | var i = indexedValue.index;
|
2161 | var awaiting = this._updateReady(i);
|
2162 |
|
2163 | this.values[i] = indexedValue.value;
|
2164 | if (awaiting === 0) {
|
2165 | this.sink.event(t, invoke(this.f, this.values));
|
2166 | }
|
2167 | };
|
2168 |
|
2169 | CombineSink.prototype._updateReady = function (index) {
|
2170 | if (this.awaiting > 0) {
|
2171 | if (!this.hasValue[index]) {
|
2172 | this.hasValue[index] = true;
|
2173 | this.awaiting -= 1;
|
2174 | }
|
2175 | }
|
2176 | return this.awaiting
|
2177 | };
|
2178 |
|
2179 | CombineSink.prototype.end = function (t, indexedValue) {
|
2180 | tryDispose(t, this.disposables[indexedValue.index], this.sink);
|
2181 | if (--this.activeCount === 0) {
|
2182 | this.sink.end(t, indexedValue.value);
|
2183 | }
|
2184 | };
|
2185 |
|
2186 |
|
2187 |
|
2188 |
|
2189 |
|
2190 |
|
2191 |
|
2192 |
|
2193 |
|
2194 |
|
2195 |
|
2196 |
|
2197 |
|
2198 |
|
2199 |
|
2200 | function ap (fs, xs) {
|
2201 | return combine(apply, fs, xs)
|
2202 | }
|
2203 |
|
2204 |
|
2205 |
|
2206 |
|
2207 |
|
2208 |
|
2209 |
|
2210 |
|
2211 |
|
2212 |
|
2213 |
|
2214 |
|
2215 | function transduce (transducer, stream) {
|
2216 | return new Stream(new Transduce(transducer, stream.source))
|
2217 | }
|
2218 |
|
2219 | function Transduce (transducer, source) {
|
2220 | this.transducer = transducer;
|
2221 | this.source = source;
|
2222 | }
|
2223 |
|
2224 | Transduce.prototype.run = function (sink, scheduler) {
|
2225 | var xf = this.transducer(new Transformer(sink));
|
2226 | return this.source.run(new TransduceSink(getTxHandler(xf), sink), scheduler)
|
2227 | };
|
2228 |
|
2229 | function TransduceSink (adapter, sink) {
|
2230 | this.xf = adapter;
|
2231 | this.sink = sink;
|
2232 | }
|
2233 |
|
2234 | TransduceSink.prototype.event = function (t, x) {
|
2235 | var next = this.xf.step(t, x);
|
2236 |
|
2237 | return this.xf.isReduced(next)
|
2238 | ? this.sink.end(t, this.xf.getResult(next))
|
2239 | : next
|
2240 | };
|
2241 |
|
2242 | TransduceSink.prototype.end = function (t, x) {
|
2243 | return this.xf.result(x)
|
2244 | };
|
2245 |
|
2246 | TransduceSink.prototype.error = function (t, e) {
|
2247 | return this.sink.error(t, e)
|
2248 | };
|
2249 |
|
2250 | function Transformer (sink) {
|
2251 | this.time = -Infinity;
|
2252 | this.sink = sink;
|
2253 | }
|
2254 |
|
2255 | Transformer.prototype['@@transducer/init'] = Transformer.prototype.init = function () {};
|
2256 |
|
2257 | Transformer.prototype['@@transducer/step'] = Transformer.prototype.step = function (t, x) {
|
2258 | if (!isNaN(t)) {
|
2259 | this.time = Math.max(t, this.time);
|
2260 | }
|
2261 | return this.sink.event(this.time, x)
|
2262 | };
|
2263 |
|
2264 | Transformer.prototype['@@transducer/result'] = Transformer.prototype.result = function (x) {
|
2265 | return this.sink.end(this.time, x)
|
2266 | };
|
2267 |
|
2268 |
|
2269 |
|
2270 |
|
2271 |
|
2272 |
|
2273 |
|
2274 | function getTxHandler (tx) {
|
2275 | return typeof tx['@@transducer/step'] === 'function'
|
2276 | ? new TxAdapter(tx)
|
2277 | : new LegacyTxAdapter(tx)
|
2278 | }
|
2279 |
|
2280 |
|
2281 |
|
2282 |
|
2283 |
|
2284 |
|
2285 | function TxAdapter (tx) {
|
2286 | this.tx = tx;
|
2287 | }
|
2288 |
|
2289 | TxAdapter.prototype.step = function (t, x) {
|
2290 | return this.tx['@@transducer/step'](t, x)
|
2291 | };
|
2292 | TxAdapter.prototype.result = function (x) {
|
2293 | return this.tx['@@transducer/result'](x)
|
2294 | };
|
2295 | TxAdapter.prototype.isReduced = function (x) {
|
2296 | return x != null && x['@@transducer/reduced']
|
2297 | };
|
2298 | TxAdapter.prototype.getResult = function (x) {
|
2299 | return x['@@transducer/value']
|
2300 | };
|
2301 |
|
2302 |
|
2303 |
|
2304 |
|
2305 |
|
2306 |
|
2307 | function LegacyTxAdapter (tx) {
|
2308 | this.tx = tx;
|
2309 | }
|
2310 |
|
2311 | LegacyTxAdapter.prototype.step = function (t, x) {
|
2312 | return this.tx.step(t, x)
|
2313 | };
|
2314 | LegacyTxAdapter.prototype.result = function (x) {
|
2315 | return this.tx.result(x)
|
2316 | };
|
2317 | LegacyTxAdapter.prototype.isReduced = function (x) {
|
2318 | return x != null && x.__transducers_reduced__
|
2319 | };
|
2320 | LegacyTxAdapter.prototype.getResult = function (x) {
|
2321 | return x.value
|
2322 | };
|
2323 |
|
2324 |
|
2325 |
|
2326 |
|
2327 |
|
2328 |
|
2329 |
|
2330 |
|
2331 |
|
2332 | function LinkedList () {
|
2333 | this.head = null;
|
2334 | this.length = 0;
|
2335 | }
|
2336 |
|
2337 |
|
2338 |
|
2339 |
|
2340 |
|
2341 | LinkedList.prototype.add = function (x) {
|
2342 | if (this.head !== null) {
|
2343 | this.head.prev = x;
|
2344 | x.next = this.head;
|
2345 | }
|
2346 | this.head = x;
|
2347 | ++this.length;
|
2348 | };
|
2349 |
|
2350 |
|
2351 |
|
2352 |
|
2353 |
|
2354 | LinkedList.prototype.remove = function (x) {
|
2355 | --this.length;
|
2356 | if (x === this.head) {
|
2357 | this.head = this.head.next;
|
2358 | }
|
2359 | if (x.next !== null) {
|
2360 | x.next.prev = x.prev;
|
2361 | x.next = null;
|
2362 | }
|
2363 | if (x.prev !== null) {
|
2364 | x.prev.next = x.next;
|
2365 | x.prev = null;
|
2366 | }
|
2367 | };
|
2368 |
|
2369 |
|
2370 |
|
2371 |
|
2372 | LinkedList.prototype.isEmpty = function () {
|
2373 | return this.length === 0
|
2374 | };
|
2375 |
|
2376 |
|
2377 |
|
2378 |
|
2379 |
|
2380 |
|
2381 | LinkedList.prototype.dispose = function () {
|
2382 | if (this.isEmpty()) {
|
2383 | return Promise.resolve()
|
2384 | }
|
2385 |
|
2386 | var promises = [];
|
2387 | var x = this.head;
|
2388 | this.head = null;
|
2389 | this.length = 0;
|
2390 |
|
2391 | while (x !== null) {
|
2392 | promises.push(x.dispose());
|
2393 | x = x.next;
|
2394 | }
|
2395 |
|
2396 | return Promise.all(promises)
|
2397 | };
|
2398 |
|
2399 |
|
2400 |
|
2401 |
|
2402 |
|
2403 | function mergeConcurrently (concurrency, stream) {
|
2404 | return mergeMapConcurrently(id, concurrency, stream)
|
2405 | }
|
2406 |
|
2407 | function mergeMapConcurrently (f, concurrency, stream) {
|
2408 | return new Stream(new MergeConcurrently(f, concurrency, stream.source))
|
2409 | }
|
2410 |
|
2411 | function MergeConcurrently (f, concurrency, source) {
|
2412 | this.f = f;
|
2413 | this.concurrency = concurrency;
|
2414 | this.source = source;
|
2415 | }
|
2416 |
|
2417 | MergeConcurrently.prototype.run = function (sink, scheduler) {
|
2418 | return new Outer(this.f, this.concurrency, this.source, sink, scheduler)
|
2419 | };
|
2420 |
|
2421 | function Outer (f, concurrency, source, sink, scheduler) {
|
2422 | this.f = f;
|
2423 | this.concurrency = concurrency;
|
2424 | this.sink = sink;
|
2425 | this.scheduler = scheduler;
|
2426 | this.pending = [];
|
2427 | this.current = new LinkedList();
|
2428 | this.disposable = once(source.run(this, scheduler));
|
2429 | this.active = true;
|
2430 | }
|
2431 |
|
2432 | Outer.prototype.event = function (t, x) {
|
2433 | this._addInner(t, x);
|
2434 | };
|
2435 |
|
2436 | Outer.prototype._addInner = function (t, x) {
|
2437 | if (this.current.length < this.concurrency) {
|
2438 | this._startInner(t, x);
|
2439 | } else {
|
2440 | this.pending.push(x);
|
2441 | }
|
2442 | };
|
2443 |
|
2444 | Outer.prototype._startInner = function (t, x) {
|
2445 | try {
|
2446 | this._initInner(t, x);
|
2447 | } catch (e) {
|
2448 | this.error(t, e);
|
2449 | }
|
2450 | };
|
2451 |
|
2452 | Outer.prototype._initInner = function (t, x) {
|
2453 | var innerSink = new Inner(t, this, this.sink);
|
2454 | innerSink.disposable = mapAndRun(this.f, x, innerSink, this.scheduler);
|
2455 | this.current.add(innerSink);
|
2456 | };
|
2457 |
|
2458 | function mapAndRun (f, x, sink, scheduler) {
|
2459 | return f(x).source.run(sink, scheduler)
|
2460 | }
|
2461 |
|
2462 | Outer.prototype.end = function (t, x) {
|
2463 | this.active = false;
|
2464 | tryDispose(t, this.disposable, this.sink);
|
2465 | this._checkEnd(t, x);
|
2466 | };
|
2467 |
|
2468 | Outer.prototype.error = function (t, e) {
|
2469 | this.active = false;
|
2470 | this.sink.error(t, e);
|
2471 | };
|
2472 |
|
2473 | Outer.prototype.dispose = function () {
|
2474 | this.active = false;
|
2475 | this.pending.length = 0;
|
2476 | return Promise.all([this.disposable.dispose(), this.current.dispose()])
|
2477 | };
|
2478 |
|
2479 | Outer.prototype._endInner = function (t, x, inner) {
|
2480 | this.current.remove(inner);
|
2481 | tryDispose(t, inner, this);
|
2482 |
|
2483 | if (this.pending.length === 0) {
|
2484 | this._checkEnd(t, x);
|
2485 | } else {
|
2486 | this._startInner(t, this.pending.shift());
|
2487 | }
|
2488 | };
|
2489 |
|
2490 | Outer.prototype._checkEnd = function (t, x) {
|
2491 | if (!this.active && this.current.isEmpty()) {
|
2492 | this.sink.end(t, x);
|
2493 | }
|
2494 | };
|
2495 |
|
2496 | function Inner (time, outer, sink) {
|
2497 | this.prev = this.next = null;
|
2498 | this.time = time;
|
2499 | this.outer = outer;
|
2500 | this.sink = sink;
|
2501 | this.disposable = void 0;
|
2502 | }
|
2503 |
|
2504 | Inner.prototype.event = function (t, x) {
|
2505 | this.sink.event(Math.max(t, this.time), x);
|
2506 | };
|
2507 |
|
2508 | Inner.prototype.end = function (t, x) {
|
2509 | this.outer._endInner(Math.max(t, this.time), x, this);
|
2510 | };
|
2511 |
|
2512 | Inner.prototype.error = function (t, e) {
|
2513 | this.outer.error(Math.max(t, this.time), e);
|
2514 | };
|
2515 |
|
2516 | Inner.prototype.dispose = function () {
|
2517 | return this.disposable.dispose()
|
2518 | };
|
2519 |
|
2520 |
|
2521 |
|
2522 |
|
2523 |
|
2524 |
|
2525 |
|
2526 |
|
2527 |
|
2528 |
|
2529 |
|
2530 |
|
2531 | function flatMap (f, stream) {
|
2532 | return mergeMapConcurrently(f, Infinity, stream)
|
2533 | }
|
2534 |
|
2535 |
|
2536 |
|
2537 |
|
2538 |
|
2539 |
|
2540 |
|
2541 | function join (stream) {
|
2542 | return mergeConcurrently(Infinity, stream)
|
2543 | }
|
2544 |
|
2545 |
|
2546 |
|
2547 |
|
2548 |
|
2549 |
|
2550 |
|
2551 |
|
2552 |
|
2553 |
|
2554 |
|
2555 |
|
2556 |
|
2557 |
|
2558 |
|
2559 |
|
2560 | function concatMap (f, stream) {
|
2561 | return mergeMapConcurrently(f, 1, stream)
|
2562 | }
|
2563 |
|
2564 |
|
2565 |
|
2566 |
|
2567 |
|
2568 | var copy$1 = copy;
|
2569 | var reduce$2 = reduce;
|
2570 |
|
2571 |
|
2572 |
|
2573 |
|
2574 |
|
2575 |
|
2576 | function merge (/* ...streams*/) {
|
2577 | return mergeArray(copy$1(arguments))
|
2578 | }
|
2579 |
|
2580 |
|
2581 |
|
2582 |
|
2583 |
|
2584 |
|
2585 |
|
2586 | function mergeArray (streams) {
|
2587 | var l = streams.length;
|
2588 | return l === 0 ? empty()
|
2589 | : l === 1 ? streams[0]
|
2590 | : new Stream(mergeSources(streams))
|
2591 | }
|
2592 |
|
2593 |
|
2594 |
|
2595 |
|
2596 |
|
2597 |
|
2598 |
|
2599 |
|
2600 |
|
2601 |
|
2602 | function mergeSources (streams) {
|
2603 | return new Merge(reduce$2(appendSources, [], streams))
|
2604 | }
|
2605 |
|
2606 | function appendSources (sources, stream) {
|
2607 | var source = stream.source;
|
2608 | return source instanceof Merge
|
2609 | ? sources.concat(source.sources)
|
2610 | : sources.concat(source)
|
2611 | }
|
2612 |
|
2613 | function Merge (sources) {
|
2614 | this.sources = sources;
|
2615 | }
|
2616 |
|
2617 | Merge.prototype.run = function (sink, scheduler) {
|
2618 | var this$1 = this;
|
2619 |
|
2620 | var l = this.sources.length;
|
2621 | var disposables = new Array(l);
|
2622 | var sinks = new Array(l);
|
2623 |
|
2624 | var mergeSink = new MergeSink(disposables, sinks, sink);
|
2625 |
|
2626 | for (var indexSink, i = 0; i < l; ++i) {
|
2627 | indexSink = sinks[i] = new IndexSink(i, mergeSink);
|
2628 | disposables[i] = this$1.sources[i].run(indexSink, scheduler);
|
2629 | }
|
2630 |
|
2631 | return all(disposables)
|
2632 | };
|
2633 |
|
2634 | function MergeSink (disposables, sinks, sink) {
|
2635 | this.sink = sink;
|
2636 | this.disposables = disposables;
|
2637 | this.activeCount = sinks.length;
|
2638 | }
|
2639 |
|
2640 | MergeSink.prototype.error = Pipe.prototype.error;
|
2641 |
|
2642 | MergeSink.prototype.event = function (t, indexValue) {
|
2643 | this.sink.event(t, indexValue.value);
|
2644 | };
|
2645 |
|
2646 | MergeSink.prototype.end = function (t, indexedValue) {
|
2647 | tryDispose(t, this.disposables[indexedValue.index], this.sink);
|
2648 | if (--this.activeCount === 0) {
|
2649 | this.sink.end(t, indexedValue.value);
|
2650 | }
|
2651 | };
|
2652 |
|
2653 |
|
2654 |
|
2655 |
|
2656 |
|
2657 |
|
2658 |
|
2659 |
|
2660 |
|
2661 |
|
2662 |
|
2663 |
|
2664 |
|
2665 | function sample (f, sampler /*, ...streams */) {
|
2666 | return sampleArray(f, sampler, drop(2, arguments))
|
2667 | }
|
2668 |
|
2669 |
|
2670 |
|
2671 |
|
2672 |
|
2673 |
|
2674 |
|
2675 |
|
2676 | function sampleWith (sampler, stream) {
|
2677 | return new Stream(new Sampler(id, sampler.source, [stream.source]))
|
2678 | }
|
2679 |
|
2680 | function sampleArray (f, sampler, streams) {
|
2681 | return new Stream(new Sampler(f, sampler.source, map(getSource$1, streams)))
|
2682 | }
|
2683 |
|
2684 | function getSource$1 (stream) {
|
2685 | return stream.source
|
2686 | }
|
2687 |
|
2688 | function Sampler (f, sampler, sources) {
|
2689 | this.f = f;
|
2690 | this.sampler = sampler;
|
2691 | this.sources = sources;
|
2692 | }
|
2693 |
|
2694 | Sampler.prototype.run = function (sink, scheduler) {
|
2695 | var this$1 = this;
|
2696 |
|
2697 | var l = this.sources.length;
|
2698 | var disposables = new Array(l + 1);
|
2699 | var sinks = new Array(l);
|
2700 |
|
2701 | var sampleSink = new SampleSink(this.f, sinks, sink);
|
2702 |
|
2703 | for (var hold, i = 0; i < l; ++i) {
|
2704 | hold = sinks[i] = new Hold(sampleSink);
|
2705 | disposables[i] = this$1.sources[i].run(hold, scheduler);
|
2706 | }
|
2707 |
|
2708 | disposables[i] = this.sampler.run(sampleSink, scheduler);
|
2709 |
|
2710 | return all(disposables)
|
2711 | };
|
2712 |
|
2713 | function Hold (sink) {
|
2714 | this.sink = sink;
|
2715 | this.hasValue = false;
|
2716 | }
|
2717 |
|
2718 | Hold.prototype.event = function (t, x) {
|
2719 | this.value = x;
|
2720 | this.hasValue = true;
|
2721 | this.sink._notify(this);
|
2722 | };
|
2723 |
|
2724 | Hold.prototype.end = function () {};
|
2725 | Hold.prototype.error = Pipe.prototype.error;
|
2726 |
|
2727 | function SampleSink (f, sinks, sink) {
|
2728 | this.f = f;
|
2729 | this.sinks = sinks;
|
2730 | this.sink = sink;
|
2731 | this.active = false;
|
2732 | }
|
2733 |
|
2734 | SampleSink.prototype._notify = function () {
|
2735 | if (!this.active) {
|
2736 | this.active = this.sinks.every(hasValue);
|
2737 | }
|
2738 | };
|
2739 |
|
2740 | SampleSink.prototype.event = function (t) {
|
2741 | if (this.active) {
|
2742 | this.sink.event(t, invoke(this.f, map(getValue, this.sinks)));
|
2743 | }
|
2744 | };
|
2745 |
|
2746 | SampleSink.prototype.end = Pipe.prototype.end;
|
2747 | SampleSink.prototype.error = Pipe.prototype.error;
|
2748 |
|
2749 | function hasValue (hold) {
|
2750 | return hold.hasValue
|
2751 | }
|
2752 |
|
2753 | function getValue (hold) {
|
2754 | return hold.value
|
2755 | }
|
2756 |
|
2757 |
|
2758 |
|
2759 |
|
2760 |
|
2761 |
|
2762 |
|
2763 | function Queue (capPow2) {
|
2764 | this._capacity = capPow2 || 32;
|
2765 | this._length = 0;
|
2766 | this._head = 0;
|
2767 | }
|
2768 |
|
2769 | Queue.prototype.push = function (x) {
|
2770 | var len = this._length;
|
2771 | this._checkCapacity(len + 1);
|
2772 |
|
2773 | var i = (this._head + len) & (this._capacity - 1);
|
2774 | this[i] = x;
|
2775 | this._length = len + 1;
|
2776 | };
|
2777 |
|
2778 | Queue.prototype.shift = function () {
|
2779 | var head = this._head;
|
2780 | var x = this[head];
|
2781 |
|
2782 | this[head] = void 0;
|
2783 | this._head = (head + 1) & (this._capacity - 1);
|
2784 | this._length--;
|
2785 | return x
|
2786 | };
|
2787 |
|
2788 | Queue.prototype.isEmpty = function () {
|
2789 | return this._length === 0
|
2790 | };
|
2791 |
|
2792 | Queue.prototype.length = function () {
|
2793 | return this._length
|
2794 | };
|
2795 |
|
2796 | Queue.prototype._checkCapacity = function (size) {
|
2797 | if (this._capacity < size) {
|
2798 | this._ensureCapacity(this._capacity << 1);
|
2799 | }
|
2800 | };
|
2801 |
|
2802 | Queue.prototype._ensureCapacity = function (capacity) {
|
2803 | var oldCapacity = this._capacity;
|
2804 | this._capacity = capacity;
|
2805 |
|
2806 | var last = this._head + this._length;
|
2807 |
|
2808 | if (last > oldCapacity) {
|
2809 | copy$2(this, 0, this, oldCapacity, last & (oldCapacity - 1));
|
2810 | }
|
2811 | };
|
2812 |
|
2813 | function copy$2 (src, srcIndex, dst, dstIndex, len) {
|
2814 | for (var j = 0; j < len; ++j) {
|
2815 | dst[j + dstIndex] = src[j + srcIndex];
|
2816 | src[j + srcIndex] = void 0;
|
2817 | }
|
2818 | }
|
2819 |
|
2820 |
|
2821 |
|
2822 |
|
2823 |
|
2824 | var map$4 = map;
|
2825 | var tail$2 = tail;
|
2826 |
|
2827 |
|
2828 |
|
2829 |
|
2830 |
|
2831 |
|
2832 |
|
2833 |
|
2834 |
|
2835 | function zip (f /*, ...streams */) {
|
2836 | return zipArray(f, tail$2(arguments))
|
2837 | }
|
2838 |
|
2839 |
|
2840 |
|
2841 |
|
2842 |
|
2843 |
|
2844 |
|
2845 |
|
2846 |
|
2847 |
|
2848 | function zipArray (f, streams) {
|
2849 | return streams.length === 0 ? empty()
|
2850 | : streams.length === 1 ? map$2(f, streams[0])
|
2851 | : new Stream(new Zip(f, map$4(getSource$2, streams)))
|
2852 | }
|
2853 |
|
2854 | function getSource$2 (stream) {
|
2855 | return stream.source
|
2856 | }
|
2857 |
|
2858 | function Zip (f, sources) {
|
2859 | this.f = f;
|
2860 | this.sources = sources;
|
2861 | }
|
2862 |
|
2863 | Zip.prototype.run = function (sink, scheduler) {
|
2864 | var this$1 = this;
|
2865 |
|
2866 | var l = this.sources.length;
|
2867 | var disposables = new Array(l);
|
2868 | var sinks = new Array(l);
|
2869 | var buffers = new Array(l);
|
2870 |
|
2871 | var zipSink = new ZipSink(this.f, buffers, sinks, sink);
|
2872 |
|
2873 | for (var indexSink, i = 0; i < l; ++i) {
|
2874 | buffers[i] = new Queue();
|
2875 | indexSink = sinks[i] = new IndexSink(i, zipSink);
|
2876 | disposables[i] = this$1.sources[i].run(indexSink, scheduler);
|
2877 | }
|
2878 |
|
2879 | return all(disposables)
|
2880 | };
|
2881 |
|
2882 | function ZipSink (f, buffers, sinks, sink) {
|
2883 | this.f = f;
|
2884 | this.sinks = sinks;
|
2885 | this.sink = sink;
|
2886 | this.buffers = buffers;
|
2887 | }
|
2888 |
|
2889 | ZipSink.prototype.event = function (t, indexedValue) {
|
2890 | var buffers = this.buffers;
|
2891 | var buffer = buffers[indexedValue.index];
|
2892 |
|
2893 | buffer.push(indexedValue.value);
|
2894 |
|
2895 | if (buffer.length() === 1) {
|
2896 | if (!ready(this.buffers)) {
|
2897 | return
|
2898 | }
|
2899 |
|
2900 | emitZipped(this.f, t, buffers, this.sink);
|
2901 |
|
2902 | if (ended(this.buffers, this.sinks)) {
|
2903 | this.sink.end(t, void 0);
|
2904 | }
|
2905 | }
|
2906 | };
|
2907 |
|
2908 | ZipSink.prototype.end = function (t, indexedValue) {
|
2909 | var buffer = this.buffers[indexedValue.index];
|
2910 | if (buffer.isEmpty()) {
|
2911 | this.sink.end(t, indexedValue.value);
|
2912 | }
|
2913 | };
|
2914 |
|
2915 | ZipSink.prototype.error = Pipe.prototype.error;
|
2916 |
|
2917 | function emitZipped (f, t, buffers, sink) {
|
2918 | sink.event(t, invoke(f, map$4(head, buffers)));
|
2919 | }
|
2920 |
|
2921 | function head (buffer) {
|
2922 | return buffer.shift()
|
2923 | }
|
2924 |
|
2925 | function ended (buffers, sinks) {
|
2926 | for (var i = 0, l = buffers.length; i < l; ++i) {
|
2927 | if (buffers[i].isEmpty() && !sinks[i].active) {
|
2928 | return true
|
2929 | }
|
2930 | }
|
2931 | return false
|
2932 | }
|
2933 |
|
2934 | function ready (buffers) {
|
2935 | for (var i = 0, l = buffers.length; i < l; ++i) {
|
2936 | if (buffers[i].isEmpty()) {
|
2937 | return false
|
2938 | }
|
2939 | }
|
2940 | return true
|
2941 | }
|
2942 |
|
2943 |
|
2944 |
|
2945 |
|
2946 |
|
2947 |
|
2948 |
|
2949 |
|
2950 |
|
2951 |
|
2952 |
|
2953 | function switchLatest (stream) {
|
2954 | return new Stream(new Switch(stream.source))
|
2955 | }
|
2956 |
|
2957 | function Switch (source) {
|
2958 | this.source = source;
|
2959 | }
|
2960 |
|
2961 | Switch.prototype.run = function (sink, scheduler) {
|
2962 | var switchSink = new SwitchSink(sink, scheduler);
|
2963 | return all([switchSink, this.source.run(switchSink, scheduler)])
|
2964 | };
|
2965 |
|
2966 | function SwitchSink (sink, scheduler) {
|
2967 | this.sink = sink;
|
2968 | this.scheduler = scheduler;
|
2969 | this.current = null;
|
2970 | this.ended = false;
|
2971 | }
|
2972 |
|
2973 | SwitchSink.prototype.event = function (t, stream) {
|
2974 | this._disposeCurrent(t);
|
2975 | this.current = new Segment(t, Infinity, this, this.sink);
|
2976 | this.current.disposable = stream.source.run(this.current, this.scheduler);
|
2977 | };
|
2978 |
|
2979 | SwitchSink.prototype.end = function (t, x) {
|
2980 | this.ended = true;
|
2981 | this._checkEnd(t, x);
|
2982 | };
|
2983 |
|
2984 | SwitchSink.prototype.error = function (t, e) {
|
2985 | this.ended = true;
|
2986 | this.sink.error(t, e);
|
2987 | };
|
2988 |
|
2989 | SwitchSink.prototype.dispose = function () {
|
2990 | return this._disposeCurrent(this.scheduler.now())
|
2991 | };
|
2992 |
|
2993 | SwitchSink.prototype._disposeCurrent = function (t) {
|
2994 | if (this.current !== null) {
|
2995 | return this.current._dispose(t)
|
2996 | }
|
2997 | };
|
2998 |
|
2999 | SwitchSink.prototype._disposeInner = function (t, inner) {
|
3000 | inner._dispose(t);
|
3001 | if (inner === this.current) {
|
3002 | this.current = null;
|
3003 | }
|
3004 | };
|
3005 |
|
3006 | SwitchSink.prototype._checkEnd = function (t, x) {
|
3007 | if (this.ended && this.current === null) {
|
3008 | this.sink.end(t, x);
|
3009 | }
|
3010 | };
|
3011 |
|
3012 | SwitchSink.prototype._endInner = function (t, x, inner) {
|
3013 | this._disposeInner(t, inner);
|
3014 | this._checkEnd(t, x);
|
3015 | };
|
3016 |
|
3017 | SwitchSink.prototype._errorInner = function (t, e, inner) {
|
3018 | this._disposeInner(t, inner);
|
3019 | this.sink.error(t, e);
|
3020 | };
|
3021 |
|
3022 | function Segment (min, max, outer, sink) {
|
3023 | this.min = min;
|
3024 | this.max = max;
|
3025 | this.outer = outer;
|
3026 | this.sink = sink;
|
3027 | this.disposable = empty$1();
|
3028 | }
|
3029 |
|
3030 | Segment.prototype.event = function (t, x) {
|
3031 | if (t < this.max) {
|
3032 | this.sink.event(Math.max(t, this.min), x);
|
3033 | }
|
3034 | };
|
3035 |
|
3036 | Segment.prototype.end = function (t, x) {
|
3037 | this.outer._endInner(Math.max(t, this.min), x, this);
|
3038 | };
|
3039 |
|
3040 | Segment.prototype.error = function (t, e) {
|
3041 | this.outer._errorInner(Math.max(t, this.min), e, this);
|
3042 | };
|
3043 |
|
3044 | Segment.prototype._dispose = function (t) {
|
3045 | this.max = t;
|
3046 | tryDispose(t, this.disposable, this.sink);
|
3047 | };
|
3048 |
|
3049 |
|
3050 |
|
3051 |
|
3052 |
|
3053 |
|
3054 |
|
3055 |
|
3056 |
|
3057 |
|
3058 |
|
3059 | function filter (p, stream) {
|
3060 | return new Stream(Filter.create(p, stream.source))
|
3061 | }
|
3062 |
|
3063 |
|
3064 |
|
3065 |
|
3066 |
|
3067 |
|
3068 | function skipRepeats (stream) {
|
3069 | return skipRepeatsWith(same, stream)
|
3070 | }
|
3071 |
|
3072 |
|
3073 |
|
3074 |
|
3075 |
|
3076 |
|
3077 |
|
3078 | function skipRepeatsWith (equals, stream) {
|
3079 | return new Stream(new SkipRepeats(equals, stream.source))
|
3080 | }
|
3081 |
|
3082 | function SkipRepeats (equals, source) {
|
3083 | this.equals = equals;
|
3084 | this.source = source;
|
3085 | }
|
3086 |
|
3087 | SkipRepeats.prototype.run = function (sink, scheduler) {
|
3088 | return this.source.run(new SkipRepeatsSink(this.equals, sink), scheduler)
|
3089 | };
|
3090 |
|
3091 | function SkipRepeatsSink (equals, sink) {
|
3092 | this.equals = equals;
|
3093 | this.sink = sink;
|
3094 | this.value = void 0;
|
3095 | this.init = true;
|
3096 | }
|
3097 |
|
3098 | SkipRepeatsSink.prototype.end = Pipe.prototype.end;
|
3099 | SkipRepeatsSink.prototype.error = Pipe.prototype.error;
|
3100 |
|
3101 | SkipRepeatsSink.prototype.event = function (t, x) {
|
3102 | if (this.init) {
|
3103 | this.init = false;
|
3104 | this.value = x;
|
3105 | this.sink.event(t, x);
|
3106 | } else if (!this.equals(this.value, x)) {
|
3107 | this.value = x;
|
3108 | this.sink.event(t, x);
|
3109 | }
|
3110 | };
|
3111 |
|
3112 | function same (a, b) {
|
3113 | return a === b
|
3114 | }
|
3115 |
|
3116 |
|
3117 |
|
3118 |
|
3119 |
|
3120 |
|
3121 |
|
3122 |
|
3123 |
|
3124 |
|
3125 | function take (n, stream) {
|
3126 | return slice(0, n, stream)
|
3127 | }
|
3128 |
|
3129 |
|
3130 |
|
3131 |
|
3132 |
|
3133 |
|
3134 | function skip (n, stream) {
|
3135 | return slice(n, Infinity, stream)
|
3136 | }
|
3137 |
|
3138 |
|
3139 |
|
3140 |
|
3141 |
|
3142 |
|
3143 |
|
3144 |
|
3145 | function slice (start, end, stream) {
|
3146 | return end <= start ? empty()
|
3147 | : new Stream(sliceSource(start, end, stream.source))
|
3148 | }
|
3149 |
|
3150 | function sliceSource (start, end, source) {
|
3151 | return source instanceof Map ? commuteMapSlice(start, end, source)
|
3152 | : source instanceof Slice ? fuseSlice(start, end, source)
|
3153 | : new Slice(start, end, source)
|
3154 | }
|
3155 |
|
3156 | function commuteMapSlice (start, end, source) {
|
3157 | return Map.create(source.f, sliceSource(start, end, source.source))
|
3158 | }
|
3159 |
|
3160 | function fuseSlice (start, end, source) {
|
3161 | start += source.min;
|
3162 | end = Math.min(end + source.min, source.max);
|
3163 | return new Slice(start, end, source.source)
|
3164 | }
|
3165 |
|
3166 | function Slice (min, max, source) {
|
3167 | this.source = source;
|
3168 | this.min = min;
|
3169 | this.max = max;
|
3170 | }
|
3171 |
|
3172 | Slice.prototype.run = function (sink, scheduler) {
|
3173 | var disposable = settable();
|
3174 | var sliceSink = new SliceSink(this.min, this.max - this.min, sink, disposable);
|
3175 |
|
3176 | disposable.setDisposable(this.source.run(sliceSink, scheduler));
|
3177 | return disposable
|
3178 | };
|
3179 |
|
3180 | function SliceSink (skip, take, sink, disposable) {
|
3181 | this.sink = sink;
|
3182 | this.skip = skip;
|
3183 | this.take = take;
|
3184 | this.disposable = disposable;
|
3185 | }
|
3186 |
|
3187 | SliceSink.prototype.end = Pipe.prototype.end;
|
3188 | SliceSink.prototype.error = Pipe.prototype.error;
|
3189 |
|
3190 | SliceSink.prototype.event = function (t, x) {
|
3191 |
|
3192 | if (this.skip > 0) {
|
3193 | this.skip -= 1;
|
3194 | return
|
3195 | }
|
3196 |
|
3197 | if (this.take === 0) {
|
3198 | return
|
3199 | }
|
3200 |
|
3201 | this.take -= 1;
|
3202 | this.sink.event(t, x);
|
3203 | if (this.take === 0) {
|
3204 | this.disposable.dispose();
|
3205 | this.sink.end(t, x);
|
3206 | }
|
3207 | };
|
3208 |
|
3209 | function takeWhile (p, stream) {
|
3210 | return new Stream(new TakeWhile(p, stream.source))
|
3211 | }
|
3212 |
|
3213 | function TakeWhile (p, source) {
|
3214 | this.p = p;
|
3215 | this.source = source;
|
3216 | }
|
3217 |
|
3218 | TakeWhile.prototype.run = function (sink, scheduler) {
|
3219 | var disposable = settable();
|
3220 | var takeWhileSink = new TakeWhileSink(this.p, sink, disposable);
|
3221 |
|
3222 | disposable.setDisposable(this.source.run(takeWhileSink, scheduler));
|
3223 | return disposable
|
3224 | };
|
3225 |
|
3226 | function TakeWhileSink (p, sink, disposable) {
|
3227 | this.p = p;
|
3228 | this.sink = sink;
|
3229 | this.active = true;
|
3230 | this.disposable = disposable;
|
3231 | }
|
3232 |
|
3233 | TakeWhileSink.prototype.end = Pipe.prototype.end;
|
3234 | TakeWhileSink.prototype.error = Pipe.prototype.error;
|
3235 |
|
3236 | TakeWhileSink.prototype.event = function (t, x) {
|
3237 | if (!this.active) {
|
3238 | return
|
3239 | }
|
3240 |
|
3241 | var p = this.p;
|
3242 | this.active = p(x);
|
3243 | if (this.active) {
|
3244 | this.sink.event(t, x);
|
3245 | } else {
|
3246 | this.disposable.dispose();
|
3247 | this.sink.end(t, x);
|
3248 | }
|
3249 | };
|
3250 |
|
3251 | function skipWhile (p, stream) {
|
3252 | return new Stream(new SkipWhile(p, stream.source))
|
3253 | }
|
3254 |
|
3255 | function SkipWhile (p, source) {
|
3256 | this.p = p;
|
3257 | this.source = source;
|
3258 | }
|
3259 |
|
3260 | SkipWhile.prototype.run = function (sink, scheduler) {
|
3261 | return this.source.run(new SkipWhileSink(this.p, sink), scheduler)
|
3262 | };
|
3263 |
|
3264 | function SkipWhileSink (p, sink) {
|
3265 | this.p = p;
|
3266 | this.sink = sink;
|
3267 | this.skipping = true;
|
3268 | }
|
3269 |
|
3270 | SkipWhileSink.prototype.end = Pipe.prototype.end;
|
3271 | SkipWhileSink.prototype.error = Pipe.prototype.error;
|
3272 |
|
3273 | SkipWhileSink.prototype.event = function (t, x) {
|
3274 | if (this.skipping) {
|
3275 | var p = this.p;
|
3276 | this.skipping = p(x);
|
3277 | if (this.skipping) {
|
3278 | return
|
3279 | }
|
3280 | }
|
3281 |
|
3282 | this.sink.event(t, x);
|
3283 | };
|
3284 |
|
3285 | function skipAfter (p, stream) {
|
3286 | return new Stream(new SkipAfter(p, stream.source))
|
3287 | }
|
3288 |
|
3289 | function SkipAfter (p, source) {
|
3290 | this.p = p;
|
3291 | this.source = source;
|
3292 | }
|
3293 |
|
3294 | SkipAfter.prototype.run = function run (sink, scheduler) {
|
3295 | return this.source.run(new SkipAfterSink(this.p, sink), scheduler)
|
3296 | };
|
3297 |
|
3298 | function SkipAfterSink (p, sink) {
|
3299 | this.p = p;
|
3300 | this.sink = sink;
|
3301 | this.skipping = false;
|
3302 | }
|
3303 |
|
3304 | SkipAfterSink.prototype.event = function event (t, x) {
|
3305 | if (this.skipping) {
|
3306 | return
|
3307 | }
|
3308 |
|
3309 | var p = this.p;
|
3310 | this.skipping = p(x);
|
3311 | this.sink.event(t, x);
|
3312 |
|
3313 | if (this.skipping) {
|
3314 | this.sink.end(t, x);
|
3315 | }
|
3316 | };
|
3317 |
|
3318 | SkipAfterSink.prototype.end = Pipe.prototype.end;
|
3319 | SkipAfterSink.prototype.error = Pipe.prototype.error;
|
3320 |
|
3321 |
|
3322 |
|
3323 |
|
3324 |
|
3325 | function takeUntil (signal, stream) {
|
3326 | return new Stream(new Until(signal.source, stream.source))
|
3327 | }
|
3328 |
|
3329 | function skipUntil (signal, stream) {
|
3330 | return new Stream(new Since(signal.source, stream.source))
|
3331 | }
|
3332 |
|
3333 | function during (timeWindow, stream) {
|
3334 | return takeUntil(join(timeWindow), skipUntil(timeWindow, stream))
|
3335 | }
|
3336 |
|
3337 | function Until (maxSignal, source) {
|
3338 | this.maxSignal = maxSignal;
|
3339 | this.source = source;
|
3340 | }
|
3341 |
|
3342 | Until.prototype.run = function (sink, scheduler) {
|
3343 | var min = new Bound(-Infinity, sink);
|
3344 | var max = new UpperBound(this.maxSignal, sink, scheduler);
|
3345 | var disposable = this.source.run(new TimeWindowSink(min, max, sink), scheduler);
|
3346 |
|
3347 | return all([min, max, disposable])
|
3348 | };
|
3349 |
|
3350 | function Since (minSignal, source) {
|
3351 | this.minSignal = minSignal;
|
3352 | this.source = source;
|
3353 | }
|
3354 |
|
3355 | Since.prototype.run = function (sink, scheduler) {
|
3356 | var min = new LowerBound(this.minSignal, sink, scheduler);
|
3357 | var max = new Bound(Infinity, sink);
|
3358 | var disposable = this.source.run(new TimeWindowSink(min, max, sink), scheduler);
|
3359 |
|
3360 | return all([min, max, disposable])
|
3361 | };
|
3362 |
|
3363 | function Bound (value, sink) {
|
3364 | this.value = value;
|
3365 | this.sink = sink;
|
3366 | }
|
3367 |
|
3368 | Bound.prototype.error = Pipe.prototype.error;
|
3369 | Bound.prototype.event = noop;
|
3370 | Bound.prototype.end = noop;
|
3371 | Bound.prototype.dispose = noop;
|
3372 |
|
3373 | function TimeWindowSink (min, max, sink) {
|
3374 | this.min = min;
|
3375 | this.max = max;
|
3376 | this.sink = sink;
|
3377 | }
|
3378 |
|
3379 | TimeWindowSink.prototype.event = function (t, x) {
|
3380 | if (t >= this.min.value && t < this.max.value) {
|
3381 | this.sink.event(t, x);
|
3382 | }
|
3383 | };
|
3384 |
|
3385 | TimeWindowSink.prototype.error = Pipe.prototype.error;
|
3386 | TimeWindowSink.prototype.end = Pipe.prototype.end;
|
3387 |
|
3388 | function LowerBound (signal, sink, scheduler) {
|
3389 | this.value = Infinity;
|
3390 | this.sink = sink;
|
3391 | this.disposable = signal.run(this, scheduler);
|
3392 | }
|
3393 |
|
3394 | LowerBound.prototype.event = function (t /*, x */) {
|
3395 | if (t < this.value) {
|
3396 | this.value = t;
|
3397 | }
|
3398 | };
|
3399 |
|
3400 | LowerBound.prototype.end = noop;
|
3401 | LowerBound.prototype.error = Pipe.prototype.error;
|
3402 |
|
3403 | LowerBound.prototype.dispose = function () {
|
3404 | return this.disposable.dispose()
|
3405 | };
|
3406 |
|
3407 | function UpperBound (signal, sink, scheduler) {
|
3408 | this.value = Infinity;
|
3409 | this.sink = sink;
|
3410 | this.disposable = signal.run(this, scheduler);
|
3411 | }
|
3412 |
|
3413 | UpperBound.prototype.event = function (t, x) {
|
3414 | if (t < this.value) {
|
3415 | this.value = t;
|
3416 | this.sink.end(t, x);
|
3417 | }
|
3418 | };
|
3419 |
|
3420 | UpperBound.prototype.end = noop;
|
3421 | UpperBound.prototype.error = Pipe.prototype.error;
|
3422 |
|
3423 | UpperBound.prototype.dispose = function () {
|
3424 | return this.disposable.dispose()
|
3425 | };
|
3426 |
|
3427 | function noop () {}
|
3428 |
|
3429 |
|
3430 |
|
3431 |
|
3432 |
|
3433 |
|
3434 |
|
3435 |
|
3436 |
|
3437 |
|
3438 | function delay (delayTime, stream) {
|
3439 | return delayTime <= 0 ? stream
|
3440 | : new Stream(new Delay(delayTime, stream.source))
|
3441 | }
|
3442 |
|
3443 | function Delay (dt, source) {
|
3444 | this.dt = dt;
|
3445 | this.source = source;
|
3446 | }
|
3447 |
|
3448 | Delay.prototype.run = function (sink, scheduler) {
|
3449 | var delaySink = new DelaySink(this.dt, sink, scheduler);
|
3450 | return all([delaySink, this.source.run(delaySink, scheduler)])
|
3451 | };
|
3452 |
|
3453 | function DelaySink (dt, sink, scheduler) {
|
3454 | this.dt = dt;
|
3455 | this.sink = sink;
|
3456 | this.scheduler = scheduler;
|
3457 | }
|
3458 |
|
3459 | DelaySink.prototype.dispose = function () {
|
3460 | var self = this;
|
3461 | this.scheduler.cancelAll(function (task) {
|
3462 | return task.sink === self.sink
|
3463 | });
|
3464 | };
|
3465 |
|
3466 | DelaySink.prototype.event = function (t, x) {
|
3467 | this.scheduler.delay(this.dt, PropagateTask.event(x, this.sink));
|
3468 | };
|
3469 |
|
3470 | DelaySink.prototype.end = function (t, x) {
|
3471 | this.scheduler.delay(this.dt, PropagateTask.end(x, this.sink));
|
3472 | };
|
3473 |
|
3474 | DelaySink.prototype.error = Pipe.prototype.error;
|
3475 |
|
3476 |
|
3477 |
|
3478 |
|
3479 |
|
3480 | function timestamp (stream) {
|
3481 | return new Stream(new Timestamp(stream.source))
|
3482 | }
|
3483 |
|
3484 | function Timestamp (source) {
|
3485 | this.source = source;
|
3486 | }
|
3487 |
|
3488 | Timestamp.prototype.run = function (sink, scheduler) {
|
3489 | return this.source.run(new TimestampSink(sink), scheduler)
|
3490 | };
|
3491 |
|
3492 | function TimestampSink (sink) {
|
3493 | this.sink = sink;
|
3494 | }
|
3495 |
|
3496 | TimestampSink.prototype.end = Pipe.prototype.end;
|
3497 | TimestampSink.prototype.error = Pipe.prototype.error;
|
3498 |
|
3499 | TimestampSink.prototype.event = function (t, x) {
|
3500 | this.sink.event(t, { time: t, value: x });
|
3501 | };
|
3502 |
|
3503 |
|
3504 |
|
3505 |
|
3506 |
|
3507 |
|
3508 |
|
3509 |
|
3510 |
|
3511 |
|
3512 |
|
3513 | function throttle (period, stream) {
|
3514 | return new Stream(throttleSource(period, stream.source))
|
3515 | }
|
3516 |
|
3517 | function throttleSource (period, source) {
|
3518 | return source instanceof Map ? commuteMapThrottle(period, source)
|
3519 | : source instanceof Throttle ? fuseThrottle(period, source)
|
3520 | : new Throttle(period, source)
|
3521 | }
|
3522 |
|
3523 | function commuteMapThrottle (period, source) {
|
3524 | return Map.create(source.f, throttleSource(period, source.source))
|
3525 | }
|
3526 |
|
3527 | function fuseThrottle (period, source) {
|
3528 | return new Throttle(Math.max(period, source.period), source.source)
|
3529 | }
|
3530 |
|
3531 | function Throttle (period, source) {
|
3532 | this.period = period;
|
3533 | this.source = source;
|
3534 | }
|
3535 |
|
3536 | Throttle.prototype.run = function (sink, scheduler) {
|
3537 | return this.source.run(new ThrottleSink(this.period, sink), scheduler)
|
3538 | };
|
3539 |
|
3540 | function ThrottleSink (period, sink) {
|
3541 | this.time = 0;
|
3542 | this.period = period;
|
3543 | this.sink = sink;
|
3544 | }
|
3545 |
|
3546 | ThrottleSink.prototype.event = function (t, x) {
|
3547 | if (t >= this.time) {
|
3548 | this.time = t + this.period;
|
3549 | this.sink.event(t, x);
|
3550 | }
|
3551 | };
|
3552 |
|
3553 | ThrottleSink.prototype.end = Pipe.prototype.end;
|
3554 |
|
3555 | ThrottleSink.prototype.error = Pipe.prototype.error;
|
3556 |
|
3557 |
|
3558 |
|
3559 |
|
3560 |
|
3561 |
|
3562 |
|
3563 |
|
3564 | function debounce (period, stream) {
|
3565 | return new Stream(new Debounce(period, stream.source))
|
3566 | }
|
3567 |
|
3568 | function Debounce (dt, source) {
|
3569 | this.dt = dt;
|
3570 | this.source = source;
|
3571 | }
|
3572 |
|
3573 | Debounce.prototype.run = function (sink, scheduler) {
|
3574 | return new DebounceSink(this.dt, this.source, sink, scheduler)
|
3575 | };
|
3576 |
|
3577 | function DebounceSink (dt, source, sink, scheduler) {
|
3578 | this.dt = dt;
|
3579 | this.sink = sink;
|
3580 | this.scheduler = scheduler;
|
3581 | this.value = void 0;
|
3582 | this.timer = null;
|
3583 | this.disposable = source.run(this, scheduler);
|
3584 | }
|
3585 |
|
3586 | DebounceSink.prototype.event = function (t, x) {
|
3587 | this._clearTimer();
|
3588 | this.value = x;
|
3589 | this.timer = this.scheduler.delay(this.dt, PropagateTask.event(x, this.sink));
|
3590 | };
|
3591 |
|
3592 | DebounceSink.prototype.end = function (t, x) {
|
3593 | if (this._clearTimer()) {
|
3594 | this.sink.event(t, this.value);
|
3595 | this.value = void 0;
|
3596 | }
|
3597 | this.sink.end(t, x);
|
3598 | };
|
3599 |
|
3600 | DebounceSink.prototype.error = function (t, x) {
|
3601 | this._clearTimer();
|
3602 | this.sink.error(t, x);
|
3603 | };
|
3604 |
|
3605 | DebounceSink.prototype.dispose = function () {
|
3606 | this._clearTimer();
|
3607 | return this.disposable.dispose()
|
3608 | };
|
3609 |
|
3610 | DebounceSink.prototype._clearTimer = function () {
|
3611 | if (this.timer === null) {
|
3612 | return false
|
3613 | }
|
3614 | this.timer.dispose();
|
3615 | this.timer = null;
|
3616 | return true
|
3617 | };
|
3618 |
|
3619 |
|
3620 |
|
3621 |
|
3622 |
|
3623 |
|
3624 |
|
3625 |
|
3626 |
|
3627 |
|
3628 |
|
3629 |
|
3630 | function fromPromise (p) {
|
3631 | return awaitPromises(of(p))
|
3632 | }
|
3633 |
|
3634 |
|
3635 |
|
3636 |
|
3637 |
|
3638 |
|
3639 |
|
3640 |
|
3641 | function awaitPromises (stream) {
|
3642 | return new Stream(new Await(stream.source))
|
3643 | }
|
3644 |
|
3645 | function Await (source) {
|
3646 | this.source = source;
|
3647 | }
|
3648 |
|
3649 | Await.prototype.run = function (sink, scheduler) {
|
3650 | return this.source.run(new AwaitSink(sink, scheduler), scheduler)
|
3651 | };
|
3652 |
|
3653 | function AwaitSink (sink, scheduler) {
|
3654 | this.sink = sink;
|
3655 | this.scheduler = scheduler;
|
3656 | this.queue = Promise.resolve();
|
3657 | var self = this;
|
3658 |
|
3659 |
|
3660 | this._eventBound = function (x) {
|
3661 | self.sink.event(self.scheduler.now(), x);
|
3662 | };
|
3663 |
|
3664 | this._endBound = function (x) {
|
3665 | self.sink.end(self.scheduler.now(), x);
|
3666 | };
|
3667 |
|
3668 | this._errorBound = function (e) {
|
3669 | self.sink.error(self.scheduler.now(), e);
|
3670 | };
|
3671 | }
|
3672 |
|
3673 | AwaitSink.prototype.event = function (t, promise) {
|
3674 | var self = this;
|
3675 | this.queue = this.queue.then(function () {
|
3676 | return self._event(promise)
|
3677 | }).catch(this._errorBound);
|
3678 | };
|
3679 |
|
3680 | AwaitSink.prototype.end = function (t, x) {
|
3681 | var self = this;
|
3682 | this.queue = this.queue.then(function () {
|
3683 | return self._end(x)
|
3684 | }).catch(this._errorBound);
|
3685 | };
|
3686 |
|
3687 | AwaitSink.prototype.error = function (t, e) {
|
3688 | var self = this;
|
3689 |
|
3690 | this.queue = this.queue.then(function () {
|
3691 | return self._errorBound(e)
|
3692 | }).catch(fatalError);
|
3693 | };
|
3694 |
|
3695 | AwaitSink.prototype._event = function (promise) {
|
3696 | return promise.then(this._eventBound)
|
3697 | };
|
3698 |
|
3699 | AwaitSink.prototype._end = function (x) {
|
3700 | return Promise.resolve(x).then(this._endBound)
|
3701 | };
|
3702 |
|
3703 |
|
3704 |
|
3705 |
|
3706 |
|
3707 | function SafeSink (sink) {
|
3708 | this.sink = sink;
|
3709 | this.active = true;
|
3710 | }
|
3711 |
|
3712 | SafeSink.prototype.event = function (t, x) {
|
3713 | if (!this.active) {
|
3714 | return
|
3715 | }
|
3716 | this.sink.event(t, x);
|
3717 | };
|
3718 |
|
3719 | SafeSink.prototype.end = function (t, x) {
|
3720 | if (!this.active) {
|
3721 | return
|
3722 | }
|
3723 | this.disable();
|
3724 | this.sink.end(t, x);
|
3725 | };
|
3726 |
|
3727 | SafeSink.prototype.error = function (t, e) {
|
3728 | this.disable();
|
3729 | this.sink.error(t, e);
|
3730 | };
|
3731 |
|
3732 | SafeSink.prototype.disable = function () {
|
3733 | this.active = false;
|
3734 | return this.sink
|
3735 | };
|
3736 |
|
3737 |
|
3738 |
|
3739 |
|
3740 |
|
3741 |
|
3742 |
|
3743 |
|
3744 |
|
3745 |
|
3746 |
|
3747 |
|
3748 | function recoverWith (f, stream) {
|
3749 | return new Stream(new RecoverWith(f, stream.source))
|
3750 | }
|
3751 |
|
3752 | var flatMapError = recoverWith;
|
3753 |
|
3754 |
|
3755 |
|
3756 |
|
3757 |
|
3758 |
|
3759 | function throwError$1 (e) {
|
3760 | return new Stream(new ErrorSource(e))
|
3761 | }
|
3762 |
|
3763 | function ErrorSource (e) {
|
3764 | this.value = e;
|
3765 | }
|
3766 |
|
3767 | ErrorSource.prototype.run = function (sink, scheduler) {
|
3768 | return scheduler.asap(new PropagateTask(runError, this.value, sink))
|
3769 | };
|
3770 |
|
3771 | function runError (t, e, sink) {
|
3772 | sink.error(t, e);
|
3773 | }
|
3774 |
|
3775 | function RecoverWith (f, source) {
|
3776 | this.f = f;
|
3777 | this.source = source;
|
3778 | }
|
3779 |
|
3780 | RecoverWith.prototype.run = function (sink, scheduler) {
|
3781 | return new RecoverWithSink(this.f, this.source, sink, scheduler)
|
3782 | };
|
3783 |
|
3784 | function RecoverWithSink (f, source, sink, scheduler) {
|
3785 | this.f = f;
|
3786 | this.sink = new SafeSink(sink);
|
3787 | this.scheduler = scheduler;
|
3788 | this.disposable = source.run(this, scheduler);
|
3789 | }
|
3790 |
|
3791 | RecoverWithSink.prototype.event = function (t, x) {
|
3792 | tryEvent(t, x, this.sink);
|
3793 | };
|
3794 |
|
3795 | RecoverWithSink.prototype.end = function (t, x) {
|
3796 | tryEnd(t, x, this.sink);
|
3797 | };
|
3798 |
|
3799 | RecoverWithSink.prototype.error = function (t, e) {
|
3800 | var nextSink = this.sink.disable();
|
3801 |
|
3802 | tryDispose(t, this.disposable, this.sink);
|
3803 | this._startNext(t, e, nextSink);
|
3804 | };
|
3805 |
|
3806 | RecoverWithSink.prototype._startNext = function (t, x, sink) {
|
3807 | try {
|
3808 | this.disposable = this._continue(this.f, x, sink);
|
3809 | } catch (e) {
|
3810 | sink.error(t, e);
|
3811 | }
|
3812 | };
|
3813 |
|
3814 | RecoverWithSink.prototype._continue = function (f, x, sink) {
|
3815 | var stream = f(x);
|
3816 | return stream.source.run(sink, this.scheduler)
|
3817 | };
|
3818 |
|
3819 | RecoverWithSink.prototype.dispose = function () {
|
3820 | return this.disposable.dispose()
|
3821 | };
|
3822 |
|
3823 | var MulticastDisposable = function MulticastDisposable (source, sink) {
|
3824 | this.source = source;
|
3825 | this.sink = sink;
|
3826 | this.disposed = false;
|
3827 | };
|
3828 |
|
3829 | MulticastDisposable.prototype.dispose = function dispose () {
|
3830 | if (this.disposed) {
|
3831 | return
|
3832 | }
|
3833 | this.disposed = true;
|
3834 | var remaining = this.source.remove(this.sink);
|
3835 | return remaining === 0 && this.source._dispose()
|
3836 | };
|
3837 |
|
3838 | function tryEvent$1 (t, x, sink) {
|
3839 | try {
|
3840 | sink.event(t, x);
|
3841 | } catch (e) {
|
3842 | sink.error(t, e);
|
3843 | }
|
3844 | }
|
3845 |
|
3846 | function tryEnd$1 (t, x, sink) {
|
3847 | try {
|
3848 | sink.end(t, x);
|
3849 | } catch (e) {
|
3850 | sink.error(t, e);
|
3851 | }
|
3852 | }
|
3853 |
|
3854 | var dispose = function (disposable) { return disposable.dispose(); };
|
3855 |
|
3856 | var emptyDisposable = {
|
3857 | dispose: function dispose$1 () {}
|
3858 | };
|
3859 |
|
3860 | var MulticastSource = function MulticastSource (source) {
|
3861 | this.source = source;
|
3862 | this.sinks = [];
|
3863 | this._disposable = emptyDisposable;
|
3864 | };
|
3865 |
|
3866 | MulticastSource.prototype.run = function run (sink, scheduler) {
|
3867 | var n = this.add(sink);
|
3868 | if (n === 1) {
|
3869 | this._disposable = this.source.run(this, scheduler);
|
3870 | }
|
3871 | return new MulticastDisposable(this, sink)
|
3872 | };
|
3873 |
|
3874 | MulticastSource.prototype._dispose = function _dispose () {
|
3875 | var disposable = this._disposable;
|
3876 | this._disposable = emptyDisposable;
|
3877 | return Promise.resolve(disposable).then(dispose)
|
3878 | };
|
3879 |
|
3880 | MulticastSource.prototype.add = function add (sink) {
|
3881 | this.sinks = append(sink, this.sinks);
|
3882 | return this.sinks.length
|
3883 | };
|
3884 |
|
3885 | MulticastSource.prototype.remove = function remove$1 (sink) {
|
3886 | var i = findIndex(sink, this.sinks);
|
3887 |
|
3888 | if (i >= 0) {
|
3889 | this.sinks = remove(i, this.sinks);
|
3890 | }
|
3891 |
|
3892 | return this.sinks.length
|
3893 | };
|
3894 |
|
3895 | MulticastSource.prototype.event = function event (time, value) {
|
3896 | var s = this.sinks;
|
3897 | if (s.length === 1) {
|
3898 | return s[0].event(time, value)
|
3899 | }
|
3900 | for (var i = 0; i < s.length; ++i) {
|
3901 | tryEvent$1(time, value, s[i]);
|
3902 | }
|
3903 | };
|
3904 |
|
3905 | MulticastSource.prototype.end = function end (time, value) {
|
3906 | var s = this.sinks;
|
3907 | for (var i = 0; i < s.length; ++i) {
|
3908 | tryEnd$1(time, value, s[i]);
|
3909 | }
|
3910 | };
|
3911 |
|
3912 | MulticastSource.prototype.error = function error (time, err) {
|
3913 | var s = this.sinks;
|
3914 | for (var i = 0; i < s.length; ++i) {
|
3915 | s[i].error(time, err);
|
3916 | }
|
3917 | };
|
3918 |
|
3919 | function multicast (stream) {
|
3920 | var source = stream.source;
|
3921 | return source instanceof MulticastSource
|
3922 | ? stream
|
3923 | : new stream.constructor(new MulticastSource(source))
|
3924 | }
|
3925 |
|
3926 |
|
3927 |
|
3928 |
|
3929 |
|
3930 |
|
3931 | Stream.of = of;
|
3932 | Stream.empty = empty;
|
3933 |
|
3934 | Stream.from = from;
|
3935 |
|
3936 |
|
3937 |
|
3938 |
|
3939 | Stream.prototype.subscribe = function (subscriber) {
|
3940 | return subscribe(subscriber, this)
|
3941 | };
|
3942 |
|
3943 | Stream.prototype[result] = function () {
|
3944 | return this
|
3945 | };
|
3946 |
|
3947 |
|
3948 |
|
3949 |
|
3950 |
|
3951 |
|
3952 |
|
3953 |
|
3954 |
|
3955 |
|
3956 |
|
3957 | Stream.prototype.thru = function (f) {
|
3958 | return thru(f, this)
|
3959 | };
|
3960 |
|
3961 |
|
3962 |
|
3963 |
|
3964 |
|
3965 |
|
3966 |
|
3967 |
|
3968 |
|
3969 | Stream.prototype.observe = Stream.prototype.forEach = function (f) {
|
3970 | return observe(f, this)
|
3971 | };
|
3972 |
|
3973 |
|
3974 |
|
3975 |
|
3976 |
|
3977 |
|
3978 |
|
3979 |
|
3980 |
|
3981 | Stream.prototype.drain = function () {
|
3982 | return drain(this)
|
3983 | };
|
3984 |
|
3985 |
|
3986 |
|
3987 |
|
3988 |
|
3989 |
|
3990 |
|
3991 |
|
3992 |
|
3993 |
|
3994 |
|
3995 |
|
3996 |
|
3997 | Stream.prototype.loop = function (stepper, seed) {
|
3998 | return loop(stepper, seed, this)
|
3999 | };
|
4000 |
|
4001 |
|
4002 |
|
4003 |
|
4004 |
|
4005 |
|
4006 |
|
4007 |
|
4008 |
|
4009 |
|
4010 | Stream.prototype.scan = function (f, initial) {
|
4011 | return scan(f, initial, this)
|
4012 | };
|
4013 |
|
4014 |
|
4015 |
|
4016 |
|
4017 |
|
4018 |
|
4019 |
|
4020 |
|
4021 |
|
4022 | Stream.prototype.reduce = function (f, initial) {
|
4023 | return reduce$1(f, initial, this)
|
4024 | };
|
4025 |
|
4026 |
|
4027 |
|
4028 |
|
4029 |
|
4030 |
|
4031 | Stream.prototype.concat = function (tail$$1) {
|
4032 | return concat(this, tail$$1)
|
4033 | };
|
4034 |
|
4035 |
|
4036 |
|
4037 |
|
4038 |
|
4039 | Stream.prototype.startWith = function (x) {
|
4040 | return cons$1(x, this)
|
4041 | };
|
4042 |
|
4043 |
|
4044 |
|
4045 |
|
4046 |
|
4047 |
|
4048 |
|
4049 |
|
4050 |
|
4051 | Stream.prototype.map = function (f) {
|
4052 | return map$2(f, this)
|
4053 | };
|
4054 |
|
4055 |
|
4056 |
|
4057 |
|
4058 |
|
4059 |
|
4060 |
|
4061 | Stream.prototype.ap = function (xs) {
|
4062 | return ap(this, xs)
|
4063 | };
|
4064 |
|
4065 |
|
4066 |
|
4067 |
|
4068 |
|
4069 |
|
4070 | Stream.prototype.constant = function (x) {
|
4071 | return constant(x, this)
|
4072 | };
|
4073 |
|
4074 |
|
4075 |
|
4076 |
|
4077 |
|
4078 |
|
4079 |
|
4080 | Stream.prototype.tap = function (f) {
|
4081 | return tap(f, this)
|
4082 | };
|
4083 |
|
4084 |
|
4085 |
|
4086 |
|
4087 |
|
4088 |
|
4089 |
|
4090 |
|
4091 |
|
4092 | Stream.prototype.transduce = function (transducer) {
|
4093 | return transduce(transducer, this)
|
4094 | };
|
4095 |
|
4096 |
|
4097 |
|
4098 |
|
4099 |
|
4100 |
|
4101 |
|
4102 |
|
4103 |
|
4104 |
|
4105 | Stream.prototype.chain = function (f) {
|
4106 | return flatMap(f, this)
|
4107 | };
|
4108 |
|
4109 |
|
4110 | Stream.prototype.flatMap = Stream.prototype.chain;
|
4111 |
|
4112 | |
4113 |
|
4114 |
|
4115 |
|
4116 |
|
4117 | Stream.prototype.join = function () {
|
4118 | return join(this)
|
4119 | };
|
4120 |
|
4121 |
|
4122 |
|
4123 |
|
4124 |
|
4125 |
|
4126 |
|
4127 |
|
4128 | Stream.prototype.continueWith = function (f) {
|
4129 | return continueWith(f, this)
|
4130 | };
|
4131 |
|
4132 |
|
4133 | Stream.prototype.flatMapEnd = Stream.prototype.continueWith;
|
4134 |
|
4135 | Stream.prototype.concatMap = function (f) {
|
4136 | return concatMap(f, this)
|
4137 | };
|
4138 |
|
4139 |
|
4140 |
|
4141 |
|
4142 |
|
4143 |
|
4144 |
|
4145 |
|
4146 |
|
4147 |
|
4148 |
|
4149 |
|
4150 |
|
4151 | Stream.prototype.mergeConcurrently = function (concurrency) {
|
4152 | return mergeConcurrently(concurrency, this)
|
4153 | };
|
4154 |
|
4155 |
|
4156 |
|
4157 |
|
4158 |
|
4159 |
|
4160 |
|
4161 |
|
4162 |
|
4163 |
|
4164 | Stream.prototype.merge = function (/* ...streams*/) {
|
4165 | return mergeArray(cons(this, arguments))
|
4166 | };
|
4167 |
|
4168 |
|
4169 |
|
4170 |
|
4171 |
|
4172 |
|
4173 |
|
4174 |
|
4175 |
|
4176 |
|
4177 | Stream.prototype.combine = function (f /*, ...streams*/) {
|
4178 | return combineArray(f, replace(this, 0, arguments))
|
4179 | };
|
4180 |
|
4181 |
|
4182 |
|
4183 |
|
4184 |
|
4185 |
|
4186 |
|
4187 |
|
4188 |
|
4189 |
|
4190 | Stream.prototype.sampleWith = function (sampler) {
|
4191 | return sampleWith(sampler, this)
|
4192 | };
|
4193 |
|
4194 |
|
4195 |
|
4196 |
|
4197 |
|
4198 |
|
4199 |
|
4200 | Stream.prototype.sample = function (f /* ...streams */) {
|
4201 | return sampleArray(f, this, tail(arguments))
|
4202 | };
|
4203 |
|
4204 |
|
4205 |
|
4206 |
|
4207 |
|
4208 |
|
4209 |
|
4210 |
|
4211 |
|
4212 |
|
4213 |
|
4214 | Stream.prototype.zip = function (f /*, ...streams*/) {
|
4215 | return zipArray(f, replace(this, 0, arguments))
|
4216 | };
|
4217 |
|
4218 |
|
4219 |
|
4220 |
|
4221 |
|
4222 |
|
4223 |
|
4224 |
|
4225 |
|
4226 | Stream.prototype.switchLatest = function () {
|
4227 | return switchLatest(this)
|
4228 | };
|
4229 |
|
4230 |
|
4231 | Stream.prototype.switch = Stream.prototype.switchLatest;
|
4232 |
|
4233 |
|
4234 |
|
4235 |
|
4236 |
|
4237 |
|
4238 |
|
4239 |
|
4240 |
|
4241 |
|
4242 |
|
4243 | Stream.prototype.filter = function (p) {
|
4244 | return filter(p, this)
|
4245 | };
|
4246 |
|
4247 |
|
4248 |
|
4249 |
|
4250 |
|
4251 |
|
4252 |
|
4253 | Stream.prototype.skipRepeats = function () {
|
4254 | return skipRepeats(this)
|
4255 | };
|
4256 |
|
4257 |
|
4258 |
|
4259 |
|
4260 |
|
4261 |
|
4262 | Stream.prototype.skipRepeatsWith = function (equals) {
|
4263 | return skipRepeatsWith(equals, this)
|
4264 | };
|
4265 |
|
4266 |
|
4267 |
|
4268 |
|
4269 |
|
4270 |
|
4271 |
|
4272 |
|
4273 |
|
4274 |
|
4275 | Stream.prototype.take = function (n) {
|
4276 | return take(n, this)
|
4277 | };
|
4278 |
|
4279 |
|
4280 |
|
4281 |
|
4282 |
|
4283 |
|
4284 |
|
4285 | Stream.prototype.skip = function (n) {
|
4286 | return skip(n, this)
|
4287 | };
|
4288 |
|
4289 |
|
4290 |
|
4291 |
|
4292 |
|
4293 |
|
4294 |
|
4295 |
|
4296 |
|
4297 | Stream.prototype.slice = function (start, end) {
|
4298 | return slice(start, end, this)
|
4299 | };
|
4300 |
|
4301 |
|
4302 |
|
4303 |
|
4304 |
|
4305 |
|
4306 |
|
4307 |
|
4308 | Stream.prototype.takeWhile = function (p) {
|
4309 | return takeWhile(p, this)
|
4310 | };
|
4311 |
|
4312 |
|
4313 |
|
4314 |
|
4315 |
|
4316 |
|
4317 |
|
4318 |
|
4319 | Stream.prototype.skipWhile = function (p) {
|
4320 | return skipWhile(p, this)
|
4321 | };
|
4322 |
|
4323 |
|
4324 |
|
4325 |
|
4326 |
|
4327 |
|
4328 |
|
4329 |
|
4330 | Stream.prototype.skipAfter = function (p) {
|
4331 | return skipAfter(p, this)
|
4332 | };
|
4333 |
|
4334 |
|
4335 |
|
4336 |
|
4337 |
|
4338 |
|
4339 |
|
4340 |
|
4341 |
|
4342 |
|
4343 |
|
4344 |
|
4345 |
|
4346 | Stream.prototype.until = function (signal) {
|
4347 | return takeUntil(signal, this)
|
4348 | };
|
4349 |
|
4350 |
|
4351 | Stream.prototype.takeUntil = Stream.prototype.until;
|
4352 |
|
4353 | |
4354 |
|
4355 |
|
4356 |
|
4357 |
|
4358 |
|
4359 |
|
4360 |
|
4361 |
|
4362 | Stream.prototype.since = function (signal) {
|
4363 | return skipUntil(signal, this)
|
4364 | };
|
4365 |
|
4366 |
|
4367 | Stream.prototype.skipUntil = Stream.prototype.since;
|
4368 |
|
4369 | |
4370 |
|
4371 |
|
4372 |
|
4373 |
|
4374 |
|
4375 |
|
4376 |
|
4377 |
|
4378 |
|
4379 | Stream.prototype.during = function (timeWindow) {
|
4380 | return during(timeWindow, this)
|
4381 | };
|
4382 |
|
4383 |
|
4384 |
|
4385 |
|
4386 |
|
4387 |
|
4388 |
|
4389 |
|
4390 | Stream.prototype.delay = function (delayTime) {
|
4391 | return delay(delayTime, this)
|
4392 | };
|
4393 |
|
4394 |
|
4395 |
|
4396 |
|
4397 |
|
4398 |
|
4399 |
|
4400 |
|
4401 |
|
4402 | Stream.prototype.timestamp = function () {
|
4403 | return timestamp(this)
|
4404 | };
|
4405 |
|
4406 |
|
4407 |
|
4408 |
|
4409 |
|
4410 |
|
4411 |
|
4412 |
|
4413 |
|
4414 |
|
4415 |
|
4416 | Stream.prototype.throttle = function (period) {
|
4417 | return throttle(period, this)
|
4418 | };
|
4419 |
|
4420 |
|
4421 |
|
4422 |
|
4423 |
|
4424 |
|
4425 |
|
4426 |
|
4427 |
|
4428 | Stream.prototype.debounce = function (period) {
|
4429 | return debounce(period, this)
|
4430 | };
|
4431 |
|
4432 |
|
4433 |
|
4434 |
|
4435 |
|
4436 |
|
4437 |
|
4438 |
|
4439 |
|
4440 | Stream.prototype.awaitPromises = function () {
|
4441 | return awaitPromises(this)
|
4442 | };
|
4443 |
|
4444 |
|
4445 | Stream.prototype.await = Stream.prototype.awaitPromises;
|
4446 |
|
4447 |
|
4448 |
|
4449 |
|
4450 |
|
4451 |
|
4452 |
|
4453 |
|
4454 |
|
4455 |
|
4456 |
|
4457 |
|
4458 |
|
4459 | Stream.prototype.recoverWith = function (f) {
|
4460 | return flatMapError(f, this)
|
4461 | };
|
4462 |
|
4463 |
|
4464 | Stream.prototype.flatMapError = Stream.prototype.recoverWith;
|
4465 |
|
4466 |
|
4467 |
|
4468 |
|
4469 |
|
4470 |
|
4471 |
|
4472 |
|
4473 |
|
4474 | Stream.prototype.multicast = function () {
|
4475 | return multicast(this)
|
4476 | };
|
4477 |
|
4478 |
|
4479 |
|
4480 |
|
4481 | exports.Stream = Stream;
|
4482 | exports.of = of;
|
4483 | exports.just = of;
|
4484 | exports.empty = empty;
|
4485 | exports.never = never;
|
4486 | exports.from = from;
|
4487 | exports.periodic = periodic;
|
4488 | exports.observe = observe;
|
4489 | exports.forEach = observe;
|
4490 | exports.drain = drain;
|
4491 | exports.loop = loop;
|
4492 | exports.scan = scan;
|
4493 | exports.reduce = reduce$1;
|
4494 | exports.concat = concat;
|
4495 | exports.startWith = cons$1;
|
4496 | exports.map = map$2;
|
4497 | exports.constant = constant;
|
4498 | exports.tap = tap;
|
4499 | exports.ap = ap;
|
4500 | exports.transduce = transduce;
|
4501 | exports.flatMap = flatMap;
|
4502 | exports.chain = flatMap;
|
4503 | exports.join = join;
|
4504 | exports.continueWith = continueWith;
|
4505 | exports.flatMapEnd = continueWith;
|
4506 | exports.concatMap = concatMap;
|
4507 | exports.mergeConcurrently = mergeConcurrently;
|
4508 | exports.merge = merge;
|
4509 | exports.mergeArray = mergeArray;
|
4510 | exports.combine = combine;
|
4511 | exports.combineArray = combineArray;
|
4512 | exports.sample = sample;
|
4513 | exports.sampleArray = sampleArray;
|
4514 | exports.sampleWith = sampleWith;
|
4515 | exports.zip = zip;
|
4516 | exports.zipArray = zipArray;
|
4517 | exports.switchLatest = switchLatest;
|
4518 | exports.switch = switchLatest;
|
4519 | exports.filter = filter;
|
4520 | exports.skipRepeats = skipRepeats;
|
4521 | exports.distinct = skipRepeats;
|
4522 | exports.skipRepeatsWith = skipRepeatsWith;
|
4523 | exports.distinctBy = skipRepeatsWith;
|
4524 | exports.take = take;
|
4525 | exports.skip = skip;
|
4526 | exports.slice = slice;
|
4527 | exports.takeWhile = takeWhile;
|
4528 | exports.skipWhile = skipWhile;
|
4529 | exports.skipAfter = skipAfter;
|
4530 | exports.takeUntil = takeUntil;
|
4531 | exports.until = takeUntil;
|
4532 | exports.skipUntil = skipUntil;
|
4533 | exports.since = skipUntil;
|
4534 | exports.during = during;
|
4535 | exports.delay = delay;
|
4536 | exports.timestamp = timestamp;
|
4537 | exports.throttle = throttle;
|
4538 | exports.debounce = debounce;
|
4539 | exports.fromPromise = fromPromise;
|
4540 | exports.awaitPromises = awaitPromises;
|
4541 | exports.await = awaitPromises;
|
4542 | exports.recoverWith = recoverWith;
|
4543 | exports.flatMapError = flatMapError;
|
4544 | exports.throwError = throwError$1;
|
4545 | exports.multicast = multicast;
|
4546 | exports.defaultScheduler = defaultScheduler;
|
4547 | exports.PropagateTask = PropagateTask;
|
4548 | exports.fromEvent = fromEvent;
|
4549 | exports.unfold = unfold;
|
4550 | exports.iterate = iterate;
|
4551 | exports.generate = generate;
|
4552 |
|
4553 | Object.defineProperty(exports, '__esModule', { value: true });
|
4554 |
|
4555 | })));
|
4556 |
|