@@ -0,0 +1,1384 @@
1+'use strict';
2+
3+Object.defineProperty(exports, '__esModule', { value: true });
4+
5+/** @license MIT License (c) copyright 2010-2016 original author or authors */
6+
7+// Non-mutating array operations
8+
9+// cons :: a -> [a] -> [a]
10+// a with x prepended
11+
12+
13+// append :: a -> [a] -> [a]
14+// a with x appended
15+function append (x, a) {
16+ var l = a.length;
17+ var b = new Array(l + 1);
18+ for (var i = 0; i < l; ++i) {
19+ b[i] = a[i];
20+ }
21+
22+ b[l] = x;
23+ return b
24+}
25+
26+// drop :: Int -> [a] -> [a]
27+// drop first n elements
28+function drop (n, a) { // eslint-disable-line complexity
29+ if (n < 0) {
30+ throw new TypeError('n must be >= 0')
31+ }
32+
33+ var l = a.length;
34+ if (n === 0 || l === 0) {
35+ return a
36+ }
37+
38+ if (n >= l) {
39+ return []
40+ }
41+
42+ return unsafeDrop(n, a, l - n)
43+}
44+
45+// unsafeDrop :: Int -> [a] -> Int -> [a]
46+// Internal helper for drop
47+function unsafeDrop (n, a, l) {
48+ var b = new Array(l);
49+ for (var i = 0; i < l; ++i) {
50+ b[i] = a[n + i];
51+ }
52+ return b
53+}
54+
55+// tail :: [a] -> [a]
56+// drop head element
57+function tail (a) {
58+ return drop(1, a)
59+}
60+
61+// copy :: [a] -> [a]
62+// duplicate a (shallow duplication)
63+
64+
65+// map :: (a -> b) -> [a] -> [b]
66+// transform each element with f
67+function map (f, a) {
68+ var l = a.length;
69+ var b = new Array(l);
70+ for (var i = 0; i < l; ++i) {
71+ b[i] = f(a[i]);
72+ }
73+ return b
74+}
75+
76+// reduce :: (a -> b -> a) -> a -> [b] -> a
77+// accumulate via left-fold
78+function reduce (f, z, a) {
79+ var r = z;
80+ for (var i = 0, l = a.length; i < l; ++i) {
81+ r = f(r, a[i], i);
82+ }
83+ return r
84+}
85+
86+// replace :: a -> Int -> [a]
87+// replace element at index
88+
89+
90+// remove :: Int -> [a] -> [a]
91+// remove element at index
92+function remove (i, a) { // eslint-disable-line complexity
93+ if (i < 0) {
94+ throw new TypeError('i must be >= 0')
95+ }
96+
97+ var l = a.length;
98+ if (l === 0 || i >= l) { // exit early if index beyond end of array
99+ return a
100+ }
101+
102+ if (l === 1) { // exit early if index in bounds and length === 1
103+ return []
104+ }
105+
106+ return unsafeRemove(i, a, l - 1)
107+}
108+
109+// unsafeRemove :: Int -> [a] -> Int -> [a]
110+// Internal helper to remove element at index
111+function unsafeRemove (i, a, l) {
112+ var b = new Array(l);
113+ var j;
114+ for (j = 0; j < i; ++j) {
115+ b[j] = a[j];
116+ }
117+ for (j = i; j < l; ++j) {
118+ b[j] = a[j + 1];
119+ }
120+
121+ return b
122+}
123+
124+// removeAll :: (a -> boolean) -> [a] -> [a]
125+// remove all elements matching a predicate
126+function removeAll (f, a) {
127+ var l = a.length;
128+ var b = new Array(l);
129+ var j = 0;
130+ for (var x = (void 0), i = 0; i < l; ++i) {
131+ x = a[i];
132+ if (!f(x)) {
133+ b[j] = x;
134+ ++j;
135+ }
136+ }
137+
138+ b.length = j;
139+ return b
140+}
141+
142+// findIndex :: a -> [a] -> Int
143+// find index of x in a, from the left
144+function findIndex (x, a) {
145+ for (var i = 0, l = a.length; i < l; ++i) {
146+ if (x === a[i]) {
147+ return i
148+ }
149+ }
150+ return -1
151+}
152+
153+// isArrayLike :: * -> boolean
154+// Return true iff x is array-like
155+
156+/** @license MIT License (c) copyright 2010-2016 original author or authors */
157+
158+// id :: a -> a
159+var id = function (x) { return x; };
160+
161+// compose :: (b -> c) -> (a -> b) -> (a -> c)
162+
163+
164+// apply :: (a -> b) -> a -> b
165+
166+
167+// curry2 :: ((a, b) -> c) -> (a -> b -> c)
168+function curry2 (f) {
169+ function curried (a, b) {
170+ switch (arguments.length) {
171+ case 0: return curried
172+ case 1: return function (b) { return f(a, b); }
173+ default: return f(a, b)
174+ }
175+ }
176+ return curried
177+}
178+
179+// curry3 :: ((a, b, c) -> d) -> (a -> b -> c -> d)
180+function curry3 (f) {
181+ function curried (a, b, c) { // eslint-disable-line complexity
182+ switch (arguments.length) {
183+ case 0: return curried
184+ case 1: return curry2(function (b, c) { return f(a, b, c); })
185+ case 2: return function (c) { return f(a, b, c); }
186+ default:return f(a, b, c)
187+ }
188+ }
189+ return curried
190+}
191+
192+// curry4 :: ((a, b, c, d) -> e) -> (a -> b -> c -> d -> e)
193+
194+/** @license MIT License (c) copyright 2016 original author or authors */
195+
196+var Disposable = function Disposable (dispose, data) {
197+ this._dispose = dispose;
198+ this._data = data;
199+
200+ this.dispose = function () { return dispose(data); };
201+};
202+
203+var MemoizedDisposable = function MemoizedDisposable (disposable) {
204+ this.disposed = false;
205+ this.value = undefined;
206+ this.disposable = disposable;
207+};
208+
209+MemoizedDisposable.prototype.dispose = function dispose () {
210+ if (!this.disposed) {
211+ this.disposed = true;
212+ this.value = this.disposable.dispose();
213+ this.disposable = undefined;
214+ }
215+
216+ return this.value
217+};
218+
219+function disposeSafely (disposable) {
220+ disposable.dispose();
221+}
222+
223+var createDisposable = curry2(function (dispose, data) { return new MemoizedDisposable(new Disposable(dispose, data)); });
224+
225+var emptyDisposable = new Disposable(id, undefined);
226+
227+var disposeAll = function (disposables) { return createDisposable(curry2(map)(disposeSafely), disposables); };
228+
229+/** @license MIT License (c) copyright 2010-2016 original author or authors */
230+/** @author Brian Cavalier */
231+/** @author John Hann */
232+
233+/*global setTimeout, clearTimeout*/
234+
235+function defer (task) {
236+ return Promise.resolve(task).then(runTask)
237+}
238+
239+function runTask (task) {
240+ return task.run()
241+}
242+
243+var ClockTimer = function ClockTimer () {
244+ this.now = Date.now;
245+};
246+
247+ClockTimer.prototype.setTimer = function setTimer (f, dt) {
248+ return dt <= 0 ? runAsap(f) : setTimeout(f, dt)
249+};
250+
251+ClockTimer.prototype.clearTimer = function clearTimer (t) {
252+ return t instanceof Asap ? t.cancel() : clearTimeout(t)
253+};
254+
255+var Asap = function Asap (f) {
256+ this.f = f;
257+ this.active = true;
258+};
259+
260+Asap.prototype.run = function run () {
261+ return this.active && this.f()
262+};
263+
264+Asap.prototype.cancel = function cancel () {
265+ this.active = false;
266+};
267+
268+function runAsap (f) {
269+ var task = new Asap(f);
270+ defer(task);
271+ return task
272+}
273+
274+/* */
275+var ScheduledTask = function ScheduledTask (delay, period, task, scheduler) {
276+ this.time = delay;
277+ this.period = period;
278+ this.task = task;
279+ this.scheduler = scheduler;
280+ this.active = true;
281+};
282+
283+ScheduledTask.prototype.run = function run () {
284+ this.task.run(this.time);
285+};
286+
287+ScheduledTask.prototype.dispose = function dispose () {
288+ this.scheduler.cancel(this);
289+ this.task.dispose();
290+};
291+
292+/* */
293+function runTask$1 (task) {
294+ try {
295+ return task.run()
296+ } catch (e) {
297+ console.error(e);
298+ }
299+}
300+
301+var Scheduler = function Scheduler (timer, timeline) {
302+ var this$1 = this;
303+
304+ this.timer = timer;
305+ this.timeline = timeline;
306+
307+ this._timer = null;
308+ this._nextArrival = Infinity;
309+
310+ this._runReadyTasksBound = function () { return this$1._runReadyTasks(this$1.now()); };
311+};
312+
313+Scheduler.prototype.now = function now () {
314+ return this.timer.now()
315+};
316+
317+Scheduler.prototype.asap = function asap (task) {
318+ return this.schedule(0)(-1)(task)
319+};
320+
321+Scheduler.prototype.delay = function delay (delay$1) {
322+ var this$1 = this;
323+
324+ return function (task) { return this$1.schedule(delay$1)(-1)(task); }
325+};
326+
327+Scheduler.prototype.periodic = function periodic (period) {
328+ var this$1 = this;
329+
330+ return function (task) { return this$1.schedule(0)(period)(task); }
331+};
332+
333+Scheduler.prototype.schedule = function schedule (delay) {
334+ var this$1 = this;
335+
336+ return function (period) { return function (task) {
337+ var now = this$1.now();
338+ var st = new ScheduledTask(now + Math.max(0, delay), period, task, this$1);
339+
340+ this$1.timeline.add(st);
341+ this$1._scheduleNextRun(now);
342+ return st
343+ }; }
344+};
345+
346+Scheduler.prototype.cancel = function cancel (task) {
347+ task.active = false;
348+ if (this.timeline.remove(task)) {
349+ this._reschedule();
350+ }
351+};
352+
353+Scheduler.prototype.cancelAll = function cancelAll (f) {
354+ this.timeline.removeAll(f);
355+ this._reschedule();
356+};
357+
358+Scheduler.prototype._reschedule = function _reschedule () {
359+ if (this.timeline.isEmpty()) {
360+ this._unschedule();
361+ } else {
362+ this._scheduleNextRun(this.now());
363+ }
364+};
365+
366+Scheduler.prototype._unschedule = function _unschedule () {
367+ this.timer.clearTimer(this._timer);
368+ this._timer = null;
369+};
370+
371+Scheduler.prototype._scheduleNextRun = function _scheduleNextRun (now) { // eslint-disable-line complexity
372+ if (this.timeline.isEmpty()) {
373+ return
374+ }
375+
376+ var nextArrival = this.timeline.nextArrival();
377+
378+ if (this._timer === null) {
379+ this._scheduleNextArrival(nextArrival, now);
380+ } else if (nextArrival < this._nextArrival) {
381+ this._unschedule();
382+ this._scheduleNextArrival(nextArrival, now);
383+ }
384+};
385+
386+Scheduler.prototype._scheduleNextArrival = function _scheduleNextArrival (nextArrival, now) {
387+ this._nextArrival = nextArrival;
388+ var delay = Math.max(0, nextArrival - now);
389+ this._timer = this.timer.setTimer(this._runReadyTasksBound, delay);
390+};
391+
392+Scheduler.prototype._runReadyTasks = function _runReadyTasks (now) {
393+ this._timer = null;
394+ this.timeline.runTasks(now, runTask$1);
395+ this._scheduleNextRun(this.now());
396+};
397+
398+/** @license MIT License (c) copyright 2010-2016 original author or authors */
399+/** @author Brian Cavalier */
400+/** @author John Hann */
401+
402+var Timeline = function Timeline () {
403+ this.tasks = [];
404+};
405+
406+Timeline.prototype.nextArrival = function nextArrival () {
407+ return this.isEmpty() ? Infinity : this.tasks[0].time
408+};
409+
410+Timeline.prototype.isEmpty = function isEmpty () {
411+ return this.tasks.length === 0
412+};
413+
414+Timeline.prototype.add = function add (st) {
415+ insertByTime(st, this.tasks);
416+};
417+
418+Timeline.prototype.remove = function remove$$1 (st) {
419+ var i = binarySearch(st.time, this.tasks);
420+
421+ if (i >= 0 && i < this.tasks.length) {
422+ var at = findIndex(st, this.tasks[i].events);
423+ if (at >= 0) {
424+ this.tasks[i].events.splice(at, 1);
425+ return true
426+ }
427+ }
428+
429+ return false
430+};
431+
432+Timeline.prototype.removeAll = function removeAll$$1 (f) {
433+ var this$1 = this;
434+
435+ for (var i = 0; i < this.tasks.length; ++i) {
436+ removeAllFrom(f, this$1.tasks[i]);
437+ }
438+};
439+
440+Timeline.prototype.runTasks = function runTasks (t, runTask) {
441+ var this$1 = this;
442+
443+ var tasks = this.tasks;
444+ var l = tasks.length;
445+ var i = 0;
446+
447+ while (i < l && tasks[i].time <= t) {
448+ ++i;
449+ }
450+
451+ this.tasks = tasks.slice(i);
452+
453+ // Run all ready tasks
454+ for (var j = 0; j < i; ++j) {
455+ this$1.tasks = runReadyTasks(runTask, tasks[j].events, this$1.tasks);
456+ }
457+};
458+
459+function runReadyTasks (runTask, events, tasks) { // eslint-disable-line complexity
460+ for (var i = 0; i < events.length; ++i) {
461+ var task = events[i];
462+
463+ if (task.active) {
464+ runTask(task);
465+
466+ // Reschedule periodic repeating tasks
467+ // Check active again, since a task may have canceled itself
468+ if (task.period >= 0 && task.active) {
469+ task.time = task.time + task.period;
470+ insertByTime(task, tasks);
471+ }
472+ }
473+ }
474+
475+ return tasks
476+}
477+
478+function insertByTime (task, timeslots) { // eslint-disable-line complexity
479+ var l = timeslots.length;
480+
481+ if (l === 0) {
482+ timeslots.push(newTimeslot(task.time, [task]));
483+ return
484+ }
485+
486+ var i = binarySearch(task.time, timeslots);
487+
488+ if (i >= l) {
489+ timeslots.push(newTimeslot(task.time, [task]));
490+ } else if (task.time === timeslots[i].time) {
491+ timeslots[i].events.push(task);
492+ } else {
493+ timeslots.splice(i, 0, newTimeslot(task.time, [task]));
494+ }
495+}
496+
497+function removeAllFrom (f, timeslot) {
498+ timeslot.events = removeAll(f, timeslot.events);
499+}
500+
501+function binarySearch (t, sortedArray) { // eslint-disable-line complexity
502+ var lo = 0;
503+ var hi = sortedArray.length;
504+ var mid, y;
505+
506+ while (lo < hi) {
507+ mid = Math.floor((lo + hi) / 2);
508+ y = sortedArray[mid];
509+
510+ if (t === y.time) {
511+ return mid
512+ } else if (t < y.time) {
513+ hi = mid;
514+ } else {
515+ lo = mid + 1;
516+ }
517+ }
518+ return hi
519+}
520+
521+var newTimeslot = function (t, events) { return ({ time: t, events: events }); };
522+
523+var propagateTask =
524+ curry3(function (run, value, sink) { return new PropagateTask(run, value, sink); });
525+
526+var eventTask =
527+ curry2(function (value, sink) { return propagateTask(runEvent, value, sink); });
528+
529+var endTask = function (sink) { return propagateTask(runEnd, void 0, sink); };
530+
531+var PropagateTask = function PropagateTask (run, value, sink) {
532+ var active = true;
533+ this.sink = sink;
534+
535+ this.dispose = function () { active = false; };
536+ this.run = function (t) {
537+ if (!active) { return }
538+
539+ run(t, value, sink);
540+ };
541+};
542+
543+function runEvent (time, value, sink) {
544+ sink.event(time)(value);
545+}
546+
547+function runEnd (time, value, sink) {
548+ sink.end(time, value);
549+}
550+
551+var defaultScheduler = new Scheduler(new ClockTimer(), new Timeline());
552+
553+var scheduleTasks = curry2(function (tasks, scheduler) {
554+ var scheduledTasks = tasks.map(function (task) { return scheduler.asap(task); });
555+
556+ return {
557+ dispose: function () { return scheduledTasks.forEach(function (task) { return task.dispose(); }); }
558+ }
559+});
560+
561+var SettableDisposable = function SettableDisposable () {
562+ this.disposable = void 0;
563+ this.disposed = false;
564+ this.result = void 0;
565+};
566+
567+SettableDisposable.prototype.setDisposable = function setDisposable (disposable) {
568+ this.disposable = disposable;
569+
570+ if (this.disposed) {
571+ this.result = disposable.dispose();
572+ }
573+};
574+
575+SettableDisposable.prototype.dispose = function dispose () {
576+ if (this.disposed) {
577+ return this.result
578+ }
579+
580+ this.disposed = true;
581+
582+ if (this.disposable !== void 0) {
583+ this.result = this.disposable.dispose();
584+ }
585+
586+ return this.result
587+};
588+
589+function runEffects (stream, scheduler) {
590+ return runSourceEffects(stream.source, scheduler)
591+}
592+
593+var runSourceEffects = function (source, scheduler) { return runSource(source, scheduler); };
594+
595+function runSource (source, scheduler) {
596+ var disposable = new SettableDisposable();
597+ var observer = new RunEffectsSink(disposable);
598+
599+ disposable.setDisposable(source.run(observer)(scheduler));
600+
601+ return {}
602+}
603+
604+var RunEffectsSink = function RunEffectsSink (disposable) {
605+ this._disposable = disposable;
606+ this.active = true;
607+};
608+
609+RunEffectsSink.prototype.event = function event (t) {
610+ return function (x) {}
611+};
612+
613+RunEffectsSink.prototype.end = function end (t) {
614+ this.active = false;
615+ Promise.resolve(this._disposable).then(function (d) { return d.dispose(); });
616+};
617+
618+var drain = function (stream) { return function () { return runEffects(stream, defaultScheduler); }; };
619+
620+var LinkedList = function LinkedList () {
621+ this.head = null;
622+ this.length = 0;
623+};
624+
625+LinkedList.prototype.add = function add (x) {
626+ if (this.head !== null) {
627+ this.head.prev = x;
628+ x.next = this.head;
629+ }
630+ this.head = x;
631+ ++this.length;
632+};
633+
634+LinkedList.prototype.remove = function remove (x) { // eslint-disable-linecomplexity
635+ --this.length;
636+ if (x === this.head) {
637+ this.head = this.head.next;
638+ }
639+ if (x.next !== null) {
640+ x.next.prev = x.prev;
641+ x.next = null;
642+ }
643+ if (x.prev !== null) {
644+ x.prev.next = x.next;
645+ x.prev = null;
646+ }
647+};
648+
649+LinkedList.prototype.isEmpty = function isEmpty () {
650+ return this.length === 0
651+};
652+
653+LinkedList.prototype.dispose = function dispose () {
654+ if (this.isEmpty()) { return }
655+
656+ var x = this.head;
657+ this.head = null;
658+ this.length = 0;
659+
660+ while (x !== null) {
661+ x.dispose();
662+ x = x.next;
663+ }
664+};
665+
666+var Stream = function Stream (source) {
667+ this.source = source;
668+};
669+
670+/** @license MIT License (c) copyright 2010-2016 original author or authors */
671+/** @author Brian Cavalier */
672+/** @author John Hann */
673+
674+var flatMap =
675+ curry2(function (stream, f) { return mergeMapConcurrently(f, Infinity, stream); });
676+
677+var mergeConcurrently = curry2(function (concurrency, stream) { return mergeMapConcurrently(id, concurrency, stream); });
678+
679+var join =
680+ mergeConcurrently(Infinity);
681+
682+var mergeMapConcurrently = curry3(function (f, concurrency, stream) { return new Stream(new MergeConcurrently(f, concurrency, stream.source)); });
683+
684+var MergeConcurrently = function MergeConcurrently (f, concurrency, source) {
685+ this.f = f;
686+ this.concurrency = concurrency;
687+ this.source = source;
688+};
689+
690+MergeConcurrently.prototype.run = function run (sink) {
691+ var this$1 = this;
692+
693+ return function (scheduler) { return new Outer(this$1.f, this$1.concurrency, this$1.source, sink, scheduler); }
694+};
695+
696+var Outer = function Outer (f, concurrency, source, sink, scheduler) {
697+ this.f = f;
698+ this.concurrency = concurrency;
699+ this.sink = sink;
700+ this.scheduler = scheduler;
701+ this.pending = [];
702+ this.current = new LinkedList();
703+ this.disposable = new MemoizedDisposable(source.run(this)(scheduler));
704+ this.active = true;
705+};
706+
707+Outer.prototype.event = function event (t) {
708+ var this$1 = this;
709+
710+ return function (x) { return this$1._addInner(t, x); }
711+};
712+
713+Outer.prototype._addInner = function _addInner (t, x) {
714+ if (this.current.length < this.concurrency) {
715+ this._startInner(t, x);
716+ } else {
717+ this.pending.push(x);
718+ }
719+};
720+
721+Outer.prototype._startInner = function _startInner (t, x) {
722+ try {
723+ this._initInner(t, x);
724+ } catch (e) {
725+ this.error(t, e);
726+ }
727+};
728+
729+Outer.prototype._initInner = function _initInner (t, x) {
730+ var innerSink = new Inner(t, this, this.sink);
731+ innerSink.disposable = mapAndRun(this.f, x, innerSink, this.scheduler);
732+ this.current.add(innerSink);
733+};
734+
735+Outer.prototype.end = function end (t) {
736+ this.active = false;
737+ this.disposable.dispose();
738+ this._checkEnd(t);
739+};
740+
741+Outer.prototype.dispose = function dispose () {
742+ this.active = false;
743+ this.pending.length = 0;
744+ this.disposable.dispose();
745+ this.current.dispose();
746+};
747+
748+Outer.prototype._endInner = function _endInner (t, inner) {
749+ this.current.remove(inner);
750+ inner.dispose();
751+
752+ if (this.pending.length === 0) {
753+ this._checkEnd(t);
754+ } else {
755+ this._startInner(t, this.pending.shift());
756+ }
757+};
758+
759+Outer.prototype._checkEnd = function _checkEnd (t) {
760+ if (!this.active && this.current.isEmpty()) {
761+ this.sink.end(t);
762+ }
763+};
764+
765+var mapAndRun = function (f, x, sink, scheduler) { return f(x).source.run(sink)(scheduler); };
766+
767+var Inner = function Inner (time, outer, sink) {
768+ this.prev = this.next = null;
769+ this.time = time;
770+ this.outer = outer;
771+ this.sink = sink;
772+ this.disposable = void 0;
773+};
774+
775+Inner.prototype.event = function event (t) {
776+ var this$1 = this;
777+
778+ return function (x) { return this$1.sink.event(Math.max(t, this$1.time))(x); }
779+};
780+
781+Inner.prototype.end = function end (t) {
782+ this.outer._endInner(Math.max(t, this.time), this);
783+};
784+
785+Inner.prototype.dispose = function dispose () {
786+ return this.disposable.dispose()
787+};
788+
789+var IndexSink = function IndexSink (i, sink) {
790+ this.sink = sink;
791+ this.index = i;
792+ this.active = true;
793+ this.value = void 0;
794+};
795+
796+IndexSink.prototype.event = function event (t) {
797+ var this$1 = this;
798+
799+ return function (x) {
800+ if (!this$1.active) { return }
801+
802+ this$1.value = x;
803+ this$1.sink.event(t)(this$1);
804+ }
805+};
806+
807+IndexSink.prototype.end = function end (t) {
808+ if (!this.active) { return }
809+
810+ this.active = false;
811+ this.sink.end(t, { index: this.index });
812+};
813+
814+/** @license MIT License (c) copyright 2010-2016 original author or authors */
815+/** @author Brian Cavalier */
816+/** @author John Hann */
817+
818+var combine2 = function (f) { return function (s1) { return function (s2) { return combineArray(f, [s1, s2]); }; }; };
819+var combine3 = function (f) { return function (s1) { return function (s2) { return function (s3) { return combineArray(f, [s1, s2, s3]); }; }; }; };
820+var combine4 = function (f) { return function (s1) { return function (s2) { return function (s3) { return function (s4) { return combineArray(f, [s1, s2, s3, s4]); }; }; }; }; };
821+var combine5 = function (f) { return function (s1) { return function (s2) { return function (s3) { return function (s4) { return function (s5) { return combineArray(f, [s1, s2, s3, s4, s5]); }; }; }; }; }; };
822+
823+function combineArray (f, streams) {
824+ return new Stream(combineSources(f, streams))
825+}
826+
827+function combineSources (f, streams) {
828+ return new Combine(f, map(getSource, streams))
829+}
830+
831+function getSource (stream) {
832+ return stream.source
833+}
834+
835+function Combine (f, sources) {
836+ this.f = f;
837+ this.sources = sources;
838+}
839+
840+Combine.prototype.run = function (sink) {
841+ var this$1 = this;
842+
843+ return function (scheduler) {
844+ var l = this$1.sources.length;
845+ var disposables = new Array(l);
846+ var sinks = new Array(l);
847+
848+ var mergeSink = new CombineSink(disposables, sinks, sink, this$1.f);
849+
850+ for (var indexSink, i = 0; i < l; ++i) {
851+ indexSink = sinks[i] = new IndexSink(i, mergeSink);
852+ disposables[i] = this$1.sources[i].run(indexSink)(scheduler);
853+ }
854+
855+ return disposeAll(disposables)
856+ }
857+};
858+
859+function CombineSink (disposables, sinks, sink, f) {
860+ var this$1 = this;
861+
862+ this.sink = sink;
863+ this.disposables = disposables;
864+ this.sinks = sinks;
865+ this.f = f;
866+
867+ var l = sinks.length;
868+ this.awaiting = l;
869+ this.values = new Array(l);
870+ this.hasValue = new Array(l);
871+
872+ for (var i = 0; i < l; ++i) {
873+ this$1.hasValue[i] = false;
874+ }
875+
876+ this.activeCount = sinks.length;
877+}
878+
879+CombineSink.prototype.event = function (t) {
880+ var this$1 = this;
881+
882+ return function (indexedValue) {
883+ var i = indexedValue.index;
884+ var awaiting = this$1._updateReady(i);
885+
886+ this$1.values[i] = indexedValue.value;
887+ if (awaiting === 0) {
888+ var value = tail(this$1.values).reduce(function (f, x) { return f(x); }, this$1.f(this$1.values[0]));
889+ this$1.sink.event(t)(value);
890+ }
891+ }
892+};
893+
894+CombineSink.prototype._updateReady = function (index) {
895+ if (this.awaiting > 0) {
896+ if (!this.hasValue[index]) {
897+ this.hasValue[index] = true;
898+ this.awaiting -= 1;
899+ }
900+ }
901+ return this.awaiting
902+};
903+
904+CombineSink.prototype.end = function (t, indexedValue) {
905+ this.disposables[indexedValue.index].dispose();
906+
907+ if (--this.activeCount === 0) {
908+ this.sink.end(t);
909+ }
910+};
911+
912+/** @license MIT License (c) copyright 2010-2016 original author or authors */
913+/** @author Brian Cavalier */
914+/** @author John Hann */
915+
916+/**
917+ * @param {Array} streams array of stream to merge
918+ * @returns {Stream} stream containing events from all input observables
919+ * in time order. If two events are simultaneous they will be merged in
920+ * arbitrary order.
921+ */
922+function merge (streams) {
923+ return new Stream(mergeSources(streams))
924+}
925+
926+/**
927+ * This implements fusion/flattening for merge. It will
928+ * fuse adjacent merge operations. For example:
929+ * - a.merge(b).merge(c) effectively becomes merge(a, b, c)
930+ * - merge(a, merge(b, c)) effectively becomes merge(a, b, c)
931+ * It does this by concatenating the sources arrays of
932+ * any nested Merge sources, in effect "flattening" nested
933+ * merge operations into a single merge.
934+ */
935+function mergeSources (streams) {
936+ return new Merge(reduce(appendSources, [], streams))
937+}
938+
939+function appendSources (sources, stream) {
940+ var source = stream.source;
941+ return source instanceof Merge
942+ ? sources.concat(source.sources)
943+ : sources.concat(source)
944+}
945+
946+function Merge (sources) {
947+ this.sources = sources;
948+}
949+
950+Merge.prototype.run = function (sink, scheduler) {
951+ var this$1 = this;
952+
953+ var l = this.sources.length;
954+ var disposables = new Array(l);
955+ var sinks = new Array(l);
956+
957+ var mergeSink = new MergeSink(disposables, sinks, sink);
958+
959+ for (var indexSink, i = 0; i < l; ++i) {
960+ indexSink = sinks[i] = new IndexSink(i, mergeSink);
961+ disposables[i] = this$1.sources[i].run(indexSink, scheduler);
962+ }
963+
964+ return disposeAll(disposables)
965+};
966+
967+function MergeSink (disposables, sinks, sink) {
968+ this.sink = sink;
969+ this.disposables = disposables;
970+ this.activeCount = sinks.length;
971+}
972+
973+MergeSink.prototype.event = function (t, indexValue) {
974+ this.sink.event(t, indexValue.value);
975+};
976+
977+MergeSink.prototype.end = function (t, indexedValue) {
978+ dispose.tryDispose(t, this.disposables[indexedValue.index], this.sink);
979+ if (--this.activeCount === 0) {
980+ this.sink.end(t, indexedValue.value);
981+ }
982+};
983+
984+var concat = function (s1) { return function (s2) { return continueWith(function () { return s2; })(s1); }; };
985+
986+var continueWith = function (f) { return function (stream) { return new Stream(new ContinueWith(f, stream.source)); }; };
987+
988+function ContinueWith (f, source) {
989+ this.f = f;
990+ this.source = source;
991+}
992+
993+ContinueWith.prototype.run = function (sink) {
994+ var this$1 = this;
995+
996+ return function (scheduler) {
997+ return new ContinueWithSink(this$1.f, this$1.source, sink, scheduler)
998+ }
999+};
1000+
1001+function ContinueWithSink (f, source, sink, scheduler) {
1002+ this.f = f;
1003+ this.sink = sink;
1004+ this.scheduler = scheduler;
1005+ this.active = true;
1006+ this.disposable = new MemoizedDisposable(source.run(this)(scheduler));
1007+}
1008+
1009+ContinueWithSink.prototype.event = function (t) {
1010+ var this$1 = this;
1011+
1012+ return function (x) {
1013+ if (!this$1.active) {
1014+ return
1015+ }
1016+ this$1.sink.event(t)(x);
1017+ }
1018+};
1019+
1020+ContinueWithSink.prototype.end = function (t, x) {
1021+ if (!this.active) {
1022+ return
1023+ }
1024+
1025+ this.disposable.dispose();
1026+ this._startNext(t, x, this.sink);
1027+};
1028+
1029+ContinueWithSink.prototype._startNext = function (t, x, sink) {
1030+ this._continue(this.f, x, sink);
1031+};
1032+
1033+ContinueWithSink.prototype._continue = function (f, x, sink) {
1034+ return f(x).source.run(sink)(this.scheduler)
1035+};
1036+
1037+ContinueWithSink.prototype.dispose = function () {
1038+ this.active = false;
1039+ return this.disposable.dispose()
1040+};
1041+
1042+/**
1043+ * Given a stream of streams, return a new stream that adopts the behavior
1044+ * of the most recent inner stream.
1045+ * @param {Stream} stream of streams on which to switch
1046+ * @returns {Stream} switching stream
1047+ */
1048+function switchLatest (stream) {
1049+ return new Stream(new Switch(stream.source))
1050+}
1051+
1052+function Switch (source) {
1053+ this.source = source;
1054+}
1055+
1056+Switch.prototype.run = function (sink) {
1057+ var this$1 = this;
1058+
1059+ return function (scheduler) {
1060+ var switchSink = new SwitchSink(sink, scheduler);
1061+ return disposeAll([switchSink, this$1.source.run(switchSink)(scheduler)])
1062+ }
1063+};
1064+
1065+function SwitchSink (sink, scheduler) {
1066+ this.sink = sink;
1067+ this.scheduler = scheduler;
1068+ this.current = null;
1069+ this.ended = false;
1070+}
1071+
1072+SwitchSink.prototype.event = function (t) {
1073+ var this$1 = this;
1074+
1075+ return function (stream) {
1076+ this$1._disposeCurrent(t); // TODO: capture the result of this dispose
1077+ this$1.current = new Segment(t, Infinity, this$1, this$1.sink);
1078+ this$1.current.disposable = stream.source.run(this$1.current)(this$1.scheduler);
1079+ }
1080+};
1081+
1082+SwitchSink.prototype.end = function (t, x) {
1083+ this.ended = true;
1084+ this._checkEnd(t, x);
1085+};
1086+
1087+SwitchSink.prototype.dispose = function () {
1088+ return this._disposeCurrent(this.scheduler.now())
1089+};
1090+
1091+SwitchSink.prototype._disposeCurrent = function (t) {
1092+ if (this.current !== null) {
1093+ return this.current._dispose(t)
1094+ }
1095+};
1096+
1097+SwitchSink.prototype._disposeInner = function (t, inner) {
1098+ inner._dispose(t); // TODO: capture the result of this dispose
1099+ if (inner === this.current) {
1100+ this.current = null;
1101+ }
1102+};
1103+
1104+SwitchSink.prototype._checkEnd = function (t, x) {
1105+ if (this.ended && this.current === null) {
1106+ this.sink.end(t, x);
1107+ }
1108+};
1109+
1110+SwitchSink.prototype._endInner = function (t, x, inner) {
1111+ this._disposeInner(t, inner);
1112+ this._checkEnd(t, x);
1113+};
1114+
1115+function Segment (min, max, outer, sink) {
1116+ this.min = min;
1117+ this.max = max;
1118+ this.outer = outer;
1119+ this.sink = sink;
1120+ this.disposable = emptyDisposable;
1121+}
1122+
1123+Segment.prototype.event = function (t) {
1124+ var this$1 = this;
1125+
1126+ return function (x) {
1127+ if (t < this$1.max) {
1128+ this$1.sink.event(Math.max(t, this$1.min))(x);
1129+ }
1130+ }
1131+};
1132+
1133+Segment.prototype.end = function (t, x) {
1134+ this.outer._endInner(Math.max(t, this.min), x, this);
1135+};
1136+
1137+Segment.prototype._dispose = function (t) {
1138+ this.max = t;
1139+ this.disposable.dispose();
1140+};
1141+
1142+var skipRepeatsWith = function (equals) { return function (stream) { return new Stream(new SkipRepeats(equals, stream)); }; };
1143+
1144+var SkipRepeats = function SkipRepeats (equals, source) {
1145+ this.equals = equals;
1146+ this.source = source;
1147+};
1148+
1149+SkipRepeats.prototype.run = function run (sink) {
1150+ var this$1 = this;
1151+
1152+ return function (scheduler) { return this$1.source.run(new SkipRepeatsSink(this$1.equals, sink), scheduler); }
1153+};
1154+
1155+var SkipRepeatsSink = function SkipRepeatsSink (equals, sink) {
1156+ this.equals = equals;
1157+ this.value = void 0;
1158+ this.init = true;
1159+};
1160+
1161+SkipRepeatsSink.prototype.event = function event (t) {
1162+ var this$1 = this;
1163+
1164+ return function (x) {
1165+ if (this$1.init) {
1166+ this$1.init = false;
1167+ this$1.value = x;
1168+ this$1.sink.event(t, x);
1169+ } else if (!this$1.equals(this$1.value)(x)) {
1170+ this$1.value = x;
1171+ this$1.sink.event(t)(x);
1172+ }
1173+ }
1174+};
1175+
1176+SkipRepeatsSink.prototype.end = function end (t) {
1177+ this.sink.end(t);
1178+};
1179+
1180+var delay = function (delayTime) { return function (stream) { return delayTime <= 0 ? stream : new Stream(new Delay(delayTime, stream.source)); }; };
1181+
1182+var Delay = function Delay (dt, source) {
1183+ this.dt = dt;
1184+ this.source = source;
1185+};
1186+
1187+Delay.prototype.run = function run (sink) {
1188+ var this$1 = this;
1189+
1190+ return function (scheduler) {
1191+ var delaySink = new DelaySink(this$1.dt, sink, scheduler);
1192+ return disposeAll([delaySink, this$1.source.run(delaySink)(scheduler)])
1193+ }
1194+};
1195+
1196+var DelaySink = function DelaySink (dt, sink, scheduler) {
1197+ this.dt = dt;
1198+ this.sink = sink;
1199+ this.scheduler = scheduler;
1200+};
1201+
1202+DelaySink.prototype.dispose = function dispose () {
1203+ var this$1 = this;
1204+
1205+ this.scheduler.cancelAll(function (task) { return task.sink === this$1.sink; });
1206+};
1207+
1208+DelaySink.prototype.event = function event (t) {
1209+ var this$1 = this;
1210+
1211+ return function (x) { return this$1.scheduler.delay(this$1.dt)(eventTask(x)(this$1.sink)); }
1212+};
1213+
1214+DelaySink.prototype.end = function end (t) {
1215+ this.scheduler.delay(this.dt)(endTask(this.sink));
1216+};
1217+
1218+var dispose$1 = function (disposable) { return disposable.dispose(); };
1219+
1220+var emptyDisposable$1 = {
1221+ dispose: function dispose () {}
1222+};
1223+
1224+var MulticastDisposable = function MulticastDisposable (source, sink) {
1225+ this.source = source;
1226+ this.sink = sink;
1227+ this.disposed = false;
1228+};
1229+
1230+MulticastDisposable.prototype.dispose = function dispose () {
1231+ if (this.disposed) {
1232+ return
1233+ }
1234+ this.disposed = true;
1235+ var remaining = this.source.remove(this.sink);
1236+ return remaining === 0 && this.source._dispose()
1237+};
1238+
1239+var MulticastSource = function MulticastSource (source) {
1240+ this.source = source;
1241+ this.sinks = [];
1242+ this._disposable = emptyDisposable$1;
1243+};
1244+
1245+MulticastSource.prototype.run = function run (sink) {
1246+ var this$1 = this;
1247+
1248+ return function (scheduler) {
1249+ var n = this$1.add(sink, scheduler);
1250+ if (n === 1) {
1251+ this$1._disposable = this$1.source.run(this$1)(scheduler);
1252+ }
1253+ return new MulticastDisposable(this$1, sink)
1254+ }
1255+};
1256+
1257+MulticastSource.prototype._dispose = function _dispose () {
1258+ var disposable = this._disposable;
1259+ this._disposable = emptyDisposable$1;
1260+ return Promise.resolve(disposable).then(dispose$1)
1261+};
1262+
1263+MulticastSource.prototype.add = function add (sink) {
1264+ this.sinks = append(sink, this.sinks);
1265+ return this.sinks.length
1266+};
1267+
1268+MulticastSource.prototype.remove = function remove$1 (sink) {
1269+ var i = findIndex(sink, this.sinks);
1270+ // istanbul ignore next
1271+ if (i >= 0) {
1272+ this.sinks = remove(i, this.sinks);
1273+ }
1274+
1275+ return this.sinks.length
1276+};
1277+
1278+MulticastSource.prototype.event = function event (time) {
1279+ var this$1 = this;
1280+
1281+ return function (value) {
1282+ var s = this$1.sinks;
1283+ if (s.length === 1) {
1284+ return s[0].event(time)(value)
1285+ }
1286+ for (var i = 0; i < s.length; ++i) {
1287+ s[i].event(time)(value);
1288+ }
1289+ }
1290+};
1291+
1292+MulticastSource.prototype.end = function end (time) {
1293+ var s = this.sinks;
1294+ for (var i = 0; i < s.length; ++i) {
1295+ s[i].end(time);
1296+ }
1297+};
1298+
1299+var HoldSource = (function (MulticastSource$$1) {
1300+ function HoldSource (source, bufferSize) {
1301+ MulticastSource$$1.call(this, source);
1302+ this.bufferSize = bufferSize;
1303+ this.has = false;
1304+ this.buffer = [];
1305+ }
1306+
1307+ if ( MulticastSource$$1 ) HoldSource.__proto__ = MulticastSource$$1;
1308+ HoldSource.prototype = Object.create( MulticastSource$$1 && MulticastSource$$1.prototype );
1309+ HoldSource.prototype.constructor = HoldSource;
1310+
1311+ HoldSource.prototype.add = function add (sink, scheduler) {
1312+ if (this.has) {
1313+ pushEvents(this.buffer, sink, scheduler);
1314+ }
1315+
1316+ return MulticastSource$$1.prototype.add.call(this, sink, scheduler)
1317+ };
1318+
1319+ HoldSource.prototype.event = function event (time) {
1320+ var this$1 = this;
1321+
1322+ return function (value) {
1323+ this$1.has = true;
1324+ this$1.buffer = dropAndAppend(value, this$1.buffer, this$1.bufferSize);
1325+
1326+ return MulticastSource$$1.prototype.event.call(this$1, time)(value)
1327+ }
1328+ };
1329+
1330+ return HoldSource;
1331+}(MulticastSource));
1332+
1333+function pushEvents (buffer, sink, scheduler) {
1334+ var length = buffer.length;
1335+
1336+ for (var i = 0; i < length; ++i) {
1337+ sink.event(scheduler.now(), buffer[i]);
1338+ }
1339+}
1340+
1341+function dropAndAppend (value, buffer, bufferSize) {
1342+ if (buffer.length === bufferSize) {
1343+ return append(value, drop(1, buffer))
1344+ }
1345+
1346+ return append(value, buffer)
1347+}
1348+
1349+function multicast (stream) {
1350+ var source = stream.source;
1351+
1352+ return source instanceof MulticastSource
1353+ ? stream
1354+ : new Stream(new MulticastSource(source))
1355+}
1356+
1357+var hold = function (bufferSize) { return function (stream) { return new Stream(new HoldSource(stream.source, bufferSize)); }; };
1358+
1359+/* */
1360+
1361+exports.createDisposable = createDisposable;
1362+exports.emptyDisposable = emptyDisposable;
1363+exports.disposeAll = disposeAll;
1364+exports.defaultScheduler = defaultScheduler;
1365+exports.scheduleTasks = scheduleTasks;
1366+exports.endTask = endTask;
1367+exports.eventTask = eventTask;
1368+exports.drain = drain;
1369+exports.flatMap = flatMap;
1370+exports.mergeConcurrently = mergeConcurrently;
1371+exports.join = join;
1372+exports.mergeMapConcurrently = mergeMapConcurrently;
1373+exports.combine2 = combine2;
1374+exports.combine3 = combine3;
1375+exports.combine4 = combine4;
1376+exports.combine5 = combine5;
1377+exports.merge = merge;
1378+exports.concat = concat;
1379+exports.continueWith = continueWith;
1380+exports.switch = switchLatest;
1381+exports.skipRepeatsWith = skipRepeatsWith;
1382+exports.delay = delay;
1383+exports.multicast = multicast;
1384+exports.hold = hold;