@@ -16,11 +16,38 @@ Object.defineProperty(exports, '__esModule', { value: true });
17// drop :: Int -> [a] -> [a]
17// drop :: Int -> [a] -> [a]
18// drop first n elements
18// drop first n elements
19+function drop (n, a) { // eslint-disable-line complexity
21+ throw new TypeError('n must be >= 0')
25+ if (n === 0 || l === 0) {
33+ return unsafeDrop(n, a, l - n)
36+// unsafeDrop :: Int -> [a] -> Int -> [a]
37+// Internal helper for drop
38+function unsafeDrop (n, a, l) {
39+ var b = new Array(l);
40+ for (var i = 0; i < l; ++i) {
21// tail :: [a] -> [a]
46// tail :: [a] -> [a]
22// drop head element
47// drop head element
23−
48+function tail (a) {
25// copy :: [a] -> [a]
52// copy :: [a] -> [a]
26// duplicate a (shallow duplication)
53// duplicate a (shallow duplication)
@@ -39,7 +66,13 @@ function map (f, a) {
40// reduce :: (a -> b -> a) -> a -> [b] -> a
67// reduce :: (a -> b -> a) -> a -> [b] -> a
41// accumulate via left-fold
68// accumulate via left-fold
42−
69+function reduce (f, z, a) {
71+ for (var i = 0, l = a.length; i < l; ++i) {
44// replace :: a -> Int -> [a]
77// replace :: a -> Int -> [a]
45// replace element at index
78// replace element at index
@@ -124,10 +157,8 @@ function curry3 (f) {
124var Disposable = function Disposable (dispose, data) {
157var Disposable = function Disposable (dispose, data) {
125 this._dispose = dispose;
158 this._dispose = dispose;
126 this._data = data;
159 this._data = data;
129−Disposable.prototype.dispose = function dispose () {
161+ this.dispose = function () { return dispose(data); };
130− return this._dispose(this._data)
133var MemoizedDisposable = function MemoizedDisposable (disposable) {
164var MemoizedDisposable = function MemoizedDisposable (disposable) {
@@ -139,14 +170,14 @@ var MemoizedDisposable = function MemoizedDisposable (disposable) {
139MemoizedDisposable.prototype.dispose = function dispose () {
170MemoizedDisposable.prototype.dispose = function dispose () {
140 if (!this.disposed) {
171 if (!this.disposed) {
141 this.disposed = true;
172 this.disposed = true;
142− this.value = disposeSafely(this.disposable);
173+ this.value = this.disposable.dispose();
143 this.disposable = undefined;
174 this.disposable = undefined;
146 return this.value
177 return this.value
149−function disposeSafely$1 (disposable) {
180+function disposeSafely (disposable) {
150 disposable.dispose();
181 disposable.dispose();
@@ -154,7 +185,7 @@ var createDisposable = curry2(function (dispose, data) { return new MemoizedDisp
155var emptyDisposable = new Disposable(id, undefined);
186var emptyDisposable = new Disposable(id, undefined);
157−var disposeAll = function (disposables) { return createDisposable(map(disposeSafely$1, disposables), disposables); };
188+var disposeAll = function (disposables) { return createDisposable(curry2(map)(disposeSafely), disposables); };
159/** @license MIT License (c) copyright 2010-2016 original author or authors */
190/** @license MIT License (c) copyright 2010-2016 original author or authors */
160/** @author Brian Cavalier */
191/** @author Brian Cavalier */
@@ -202,21 +233,22 @@ function runAsap (f) {
205−function ScheduledTask (delay, period, task, scheduler) {
236+var ScheduledTask = function ScheduledTask (delay, period, task, scheduler) {
208 this.time = delay;
237 this.time = delay;
209 this.period = period;
238 this.period = period;
210 this.task = task;
239 this.task = task;
211 this.scheduler = scheduler;
240 this.scheduler = scheduler;
212 this.active = true;
241 this.active = true;
244+ScheduledTask.prototype.run = function run () {
214− this.run = function () { return task.run(this$1.time); };
245+ this.task.run(this.time);
215− this.dispose = function () {
248+ScheduledTask.prototype.dispose = function dispose () {
216− scheduler.cancel(this$1);
249+ this.scheduler.cancel(this);
217− task.dispose();
250+ this.task.dispose();
222function runTask$1 (task) {
254function runTask$1 (task) {
@@ -244,24 +276,32 @@ Scheduler.prototype.now = function now () {
246Scheduler.prototype.asap = function asap (task) {
278Scheduler.prototype.asap = function asap (task) {
247− return this.schedule(0, -1, task)
279+ return this.schedule(0)(-1)(task)
250−Scheduler.prototype.delay = function delay (delay$1, task) {
282+Scheduler.prototype.delay = function delay (delay$1) {
251− return this.schedule(delay$1, -1, task)
285+ return function (task) { return this$1.schedule(delay$1)(-1)(task); }
254−Scheduler.prototype.periodic = function periodic (period, task) {
288+Scheduler.prototype.periodic = function periodic (period) {
255− return this.schedule(0, period, task)
291+ return function (task) { return this$1.schedule(0)(period)(task); }
258−Scheduler.prototype.schedule = function schedule (delay, period, task) {
294+Scheduler.prototype.schedule = function schedule (delay) {
259− var now = this.now();
295+ var this$1 = this;
260− var st = new ScheduledTask(now + Math.max(0, delay), period, task, this);
297+ return function (period) { return function (task) {
298+ var now = this$1.now();
299+ var st = new ScheduledTask(now + Math.max(0, delay), period, task, this$1);
262− this.timeline.add(st);
301+ this$1.timeline.add(st);
263− this._scheduleNextRun(now);
302+ this$1._scheduleNextRun(now);
264− return st
303+ return st
267Scheduler.prototype.cancel = function cancel (task) {
307Scheduler.prototype.cancel = function cancel (task) {
@@ -478,48 +518,6 @@ var scheduleTasks = curry2(function (tasks, scheduler) {
481−var Stream = function Stream (source) {
482− this.source = source;
485−var tapEnd = curry2(function (f, stream) { return new Stream(new TapEnd(f, stream.source)); });
487−var tapEvent = curry2(function (f, stream) { return new Stream(new TapEvent(f, stream.source)); });
489−var TapEvent = function TapEvent (f, source) {
490− this.source = source;
493− this.run = function (sink) { return function (scheduler) { return source.run(new TapSink(f, sink))(scheduler); }; };
496−var TapSink = function TapSink (f, sink) {
497− this.event = function (t) { return function (x) {
502− this.end = function (t) {
507−var TapEnd = function TapEnd (f, source) {
508− this.source = source;
511− this.run = function (sink) { return function (scheduler) { return source.run(new TapEndSink(f, sink))(scheduler); }; };
514−var TapEndSink = function TapEndSink (f, sink) {
515− this.event = function (t) { return function (x) { return sink.event(t)(x); }; };
517− this.end = function (t) {
523var SettableDisposable = function SettableDisposable () {
521var SettableDisposable = function SettableDisposable () {
524 this.disposable = void 0;
522 this.disposable = void 0;
525 this.disposed = false;
523 this.disposed = false;
@@ -559,6 +557,8 @@ function runSource (source, scheduler) {
559 var observer = new RunEffectsSink(disposable);
557 var observer = new RunEffectsSink(disposable);
561 disposable.setDisposable(source.run(observer)(scheduler));
559 disposable.setDisposable(source.run(observer)(scheduler));
564var RunEffectsSink = function RunEffectsSink (disposable) {
564var RunEffectsSink = function RunEffectsSink (disposable) {
@@ -577,100 +577,526 @@ RunEffectsSink.prototype.end = function end (t) {
578var drain = function (stream) { return function () { return runEffects(stream, defaultScheduler); }; };
578var drain = function (stream) { return function () { return runEffects(stream, defaultScheduler); }; };
580−var combine =
580+var LinkedList = function LinkedList () {
581− curry3(function (f, stream1, stream2) { return new Stream(new Combine(f, stream1.source, stream2.source)); });
581+ this.head = null;
585+LinkedList.prototype.add = function add (x) {
586+ if (this.head !== null) {
594+LinkedList.prototype.remove = function remove (x) { // eslint-disable-linecomplexity
596+ if (x === this.head) {
597+ this.head = this.head.next;
599+ if (x.next !== null) {
600+ x.next.prev = x.prev;
603+ if (x.prev !== null) {
604+ x.prev.next = x.next;
609+LinkedList.prototype.isEmpty = function isEmpty () {
610+ return this.length === 0
613+LinkedList.prototype.dispose = function dispose () {
614+ if (this.isEmpty()) { return }
620+ while (x !== null) {
626+var Stream = function Stream (source) {
627+ this.source = source;
630+/** @license MIT License (c) copyright 2010-2016 original author or authors */
631+/** @author Brian Cavalier */
632+/** @author John Hann */
583−var Combine = function Combine (f, source1, source2) {
634+var flatMap =
635+ curry2(function (stream, f) { return mergeMapConcurrently(f, Infinity, stream); });
637+var mergeConcurrently = curry2(function (concurrency, stream) { return mergeMapConcurrently(id, concurrency, stream); });
640+ mergeConcurrently(Infinity);
642+var mergeMapConcurrently = curry3(function (f, concurrency, stream) { return new Stream(new MergeConcurrently(f, concurrency, stream.source)); });
644+var MergeConcurrently = function MergeConcurrently (f, concurrency, source) {
584 this.f = f;
645 this.f = f;
585− this.source1 = source1;
646+ this.concurrency = concurrency;
586− this.source2 = source2;
647+ this.source = source;
589−Combine.prototype.run = function run (sink) {
650+MergeConcurrently.prototype.run = function run (sink) {
590 var this$1 = this;
651 var this$1 = this;
592− return function (scheduler) {
653+ return function (scheduler) { return new Outer(this$1.f, this$1.concurrency, this$1.source, sink, scheduler); }
593− var disposables = Array(2);
654+};
594− var sinks = Array(2);
596− var mergeSink = new CombineSink(disposables, sinks, sink, this$1.f);
656+var Outer = function Outer (f, concurrency, source, sink, scheduler) {
658+ this.concurrency = concurrency;
660+ this.scheduler = scheduler;
662+ this.current = new LinkedList();
663+ this.disposable = new MemoizedDisposable(source.run(this)(scheduler));
598− sinks[0] = new IndexSink(0, mergeSink);
667+Outer.prototype.event = function event (t) {
599− sinks[1] = new IndexSink(1, mergeSink);
668+ var this$1 = this;
600− disposables[0] = this$1.source1.run(sinks[0])(scheduler);
601− disposables[1] = this$1.source2.run(sinks[1])(scheduler);
603− return disposeAll(disposables)
670+ return function (x) { return this$1._addInner(t, x); }
673+Outer.prototype._addInner = function _addInner (t, x) {
674+ if (this.current.length < this.concurrency) {
675+ this._startInner(t, x);
677+ this.pending.push(x);
607−var IndexSink = function IndexSink () {};
681+Outer.prototype._startInner = function _startInner (t, x) {
683+ this._initInner(t, x);
689+Outer.prototype._initInner = function _initInner (t, x) {
690+ var innerSink = new Inner(t, this, this.sink);
691+ innerSink.disposable = mapAndRun(this.f, x, innerSink, this.scheduler);
692+ this.current.add(innerSink);
695+Outer.prototype.end = function end (t) {
696+ this.active = false;
697+ this.disposable.dispose();
609−IndexSink.prototype.cosntructor = function cosntructor (i, sink) {
701+Outer.prototype.dispose = function dispose () {
610− this.i = i;
702+ this.active = false;
703+ this.pending.length = 0;
704+ this.disposable.dispose();
705+ this.current.dispose();
708+Outer.prototype._endInner = function _endInner (t, inner) {
709+ this.current.remove(inner);
712+ if (this.pending.length === 0) {
715+ this._startInner(t, this.pending.shift());
719+Outer.prototype._checkEnd = function _checkEnd (t) {
720+ if (!this.active && this.current.isEmpty()) {
725+var mapAndRun = function (f, x, sink, scheduler) { return f(x).source.run(sink)(scheduler); };
727+var Inner = function Inner (time, outer, sink) {
728+ this.prev = this.next = null;
732+ this.disposable = void 0;
735+Inner.prototype.event = function event (t) {
738+ return function (x) { return this$1.sink.event(Math.max(t, this$1.time))(x); }
741+Inner.prototype.end = function end (t) {
742+ this.outer._endInner(Math.max(t, this.time), this);
745+Inner.prototype.dispose = function dispose () {
746+ return this.disposable.dispose()
749+var IndexSink = function IndexSink (i, sink) {
611 this.sink = sink;
750 this.sink = sink;
753+ this.value = void 0;
614IndexSink.prototype.event = function event (t) {
756IndexSink.prototype.event = function event (t) {
615 var this$1 = this;
757 var this$1 = this;
617 return function (x) {
759 return function (x) {
760+ if (!this$1.active) { return }
618− this$1.sink.event(t)({ index: this$1.i, value: x });
763+ this$1.sink.event(t)(this$1);
622IndexSink.prototype.end = function end (t) {
767IndexSink.prototype.end = function end (t) {
768+ if (!this.active) { return }
770+ this.active = false;
623− this.sink.end(t, this.i);
771+ this.sink.end(t, { index: this.index });
774+/** @license MIT License (c) copyright 2010-2016 original author or authors */
775+/** @author Brian Cavalier */
776+/** @author John Hann */
778+var combine2 = function (f) { return function (s1) { return function (s2) { return combineArray(f, [s1, s2]); }; }; };
779+var combine3 = function (f) { return function (s1) { return function (s2) { return function (s3) { return combineArray(f, [s1, s2, s3]); }; }; }; };
780+var combine4 = function (f) { return function (s1) { return function (s2) { return function (s3) { return function (s4) { return combineArray(f, [s1, s2, s3, s4]); }; }; }; }; };
781+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]); }; }; }; }; }; };
783+function combineArray (f, streams) {
784+ return new Stream(combineSources(f, streams))
787+function combineSources (f, streams) {
788+ return new Combine(f, map(getSource, streams))
791+function getSource (stream) {
792+ return stream.source
795+function Combine (f, sources) {
797+ this.sources = sources;
800+Combine.prototype.run = function (sink) {
803+ return function (scheduler) {
804+ var l = this$1.sources.length;
805+ var disposables = new Array(l);
806+ var sinks = new Array(l);
808+ var mergeSink = new CombineSink(disposables, sinks, sink, this$1.f);
810+ for (var indexSink, i = 0; i < l; ++i) {
811+ indexSink = sinks[i] = new IndexSink(i, mergeSink);
812+ disposables[i] = this$1.sources[i].run(indexSink)(scheduler);
815+ return disposeAll(disposables)
626−var CombineSink = function CombineSink (disposables, sinks, sink, f) {
819+function CombineSink (disposables, sinks, sink, f) {
627 this.disposables = disposables;
823 this.disposables = disposables;
628 this.sinks = sinks;
824 this.sinks = sinks;
630 this.f = f;
825 this.f = f;
827+ var l = sinks.length;
632− this.awaiting = 2;
828+ this.awaiting = l;
633− this.values = Array(2);
829+ this.values = new Array(l);
634− this.hasValue = [false, false];
830+ this.hasValue = new Array(l);
635− this.activeCount = 2;
638−CombineSink.prototype.event = function event (t) {
832+ for (var i = 0; i < l; ++i) {
639− var this$1 = this;
833+ this$1.hasValue[i] = false;
836+ this.activeCount = sinks.length;
641− return function (ref) {
839+CombineSink.prototype.event = function (t) {
642− var index = ref.index;
840+ var this$1 = this;
643− var value = ref.value;
645− var f = this$1.f;
842+ return function (indexedValue) {
843+ var i = indexedValue.index;
646− var awaiting = this$1._updateReady(index);
844+ var awaiting = this$1._updateReady(i);
647− this$1.values[index] = value;
846+ this$1.values[i] = indexedValue.value;
649 if (awaiting === 0) {
847 if (awaiting === 0) {
650− var result = f(this$1.values[0])(this$1.values[1]);
848+ var value = tail(this$1.values).reduce(function (f, x) { return f(x); }, this$1.f(this$1.values[0]));
651− this$1.sink.event(t)(result);
849+ this$1.sink.event(t)(value);
656−CombineSink.prototype._updateReady = function _updateReady (index) {
854+CombineSink.prototype._updateReady = function (index) {
657 if (this.awaiting > 0) {
855 if (this.awaiting > 0) {
658 if (!this.hasValue[index]) {
856 if (!this.hasValue[index]) {
659 this.hasValue[index] = true;
857 this.hasValue[index] = true;
660 this.awaiting -= 1;
858 this.awaiting -= 1;
664 return this.awaiting
861 return this.awaiting
667−CombineSink.prototype.end = function end (t, ref) {
864+CombineSink.prototype.end = function (t, indexedValue) {
668− var index = ref.index;
865+ this.disposables[indexedValue.index].dispose();
669− var value = ref.value;
866+
867+ if (--this.activeCount === 0) {
872+/** @license MIT License (c) copyright 2010-2016 original author or authors */
873+/** @author Brian Cavalier */
874+/** @author John Hann */
877+ * @param {Array} streams array of stream to merge
878+ * @returns {Stream} stream containing events from all input observables
879+ * in time order. If two events are simultaneous they will be merged in
882+function merge (streams) {
883+ return new Stream(mergeSources(streams))
887+ * This implements fusion/flattening for merge. It will
888+ * fuse adjacent merge operations. For example:
889+ * - a.merge(b).merge(c) effectively becomes merge(a, b, c)
890+ * - merge(a, merge(b, c)) effectively becomes merge(a, b, c)
891+ * It does this by concatenating the sources arrays of
892+ * any nested Merge sources, in effect "flattening" nested
893+ * merge operations into a single merge.
895+function mergeSources (streams) {
896+ return new Merge(reduce(appendSources, [], streams))
899+function appendSources (sources, stream) {
900+ var source = stream.source;
901+ return source instanceof Merge
902+ ? sources.concat(source.sources)
903+ : sources.concat(source)
906+function Merge (sources) {
907+ this.sources = sources;
910+Merge.prototype.run = function (sink, scheduler) {
913+ var l = this.sources.length;
671− this.disposables[index].dispose();
914+ var disposables = new Array(l);
915+ var sinks = new Array(l);
917+ var mergeSink = new MergeSink(disposables, sinks, sink);
919+ for (var indexSink, i = 0; i < l; ++i) {
920+ indexSink = sinks[i] = new IndexSink(i, mergeSink);
921+ disposables[i] = this$1.sources[i].run(indexSink, scheduler);
924+ return disposeAll(disposables)
927+function MergeSink (disposables, sinks, sink) {
929+ this.disposables = disposables;
930+ this.activeCount = sinks.length;
933+MergeSink.prototype.event = function (t, indexValue) {
934+ this.sink.event(t, indexValue.value);
937+MergeSink.prototype.end = function (t, indexedValue) {
938+ dispose.tryDispose(t, this.disposables[indexedValue.index], this.sink);
939+ if (--this.activeCount === 0) {
940+ this.sink.end(t, indexedValue.value);
944+var concat = function (s1) { return function (s2) { return continueWith(function () { return s2; })(s1); }; };
946+var continueWith = function (f) { return function (stream) { return new Stream(new ContinueWith(f, stream.source)); }; };
948+function ContinueWith (f, source) {
950+ this.source = source;
953+ContinueWith.prototype.run = function (sink) {
956+ return function (scheduler) {
957+ return new ContinueWithSink(this$1.f, this$1.source, sink, scheduler)
961+function ContinueWithSink (f, source, sink, scheduler) {
964+ this.scheduler = scheduler;
966+ this.disposable = new MemoizedDisposable(source.run(this)(scheduler));
969+ContinueWithSink.prototype.event = function (t) {
972+ return function (x) {
973+ if (!this$1.active) {
976+ this$1.sink.event(t)(x);
980+ContinueWithSink.prototype.end = function (t, x) {
985+ this.disposable.dispose();
986+ this._startNext(t, x, this.sink);
989+ContinueWithSink.prototype._startNext = function (t, x, sink) {
990+ this._continue(this.f, x, sink);
993+ContinueWithSink.prototype._continue = function (f, x, sink) {
994+ return f(x).source.run(sink)(this.scheduler)
997+ContinueWithSink.prototype.dispose = function () {
998+ this.active = false;
999+ return this.disposable.dispose()
1003+ * Given a stream of streams, return a new stream that adopts the behavior
1004+ * of the most recent inner stream.
1005+ * @param {Stream} stream of streams on which to switch
1006+ * @returns {Stream} switching stream
1008+function switchLatest (stream) {
1009+ return new Stream(new Switch(stream.source))
1012+function Switch (source) {
1013+ this.source = source;
1016+Switch.prototype.run = function (sink) {
1019+ return function (scheduler) {
1020+ var switchSink = new SwitchSink(sink, scheduler);
1021+ return disposeAll([switchSink, this$1.source.run(switchSink)(scheduler)])
1025+function SwitchSink (sink, scheduler) {
1027+ this.scheduler = scheduler;
1028+ this.current = null;
1029+ this.ended = false;
1032+SwitchSink.prototype.event = function (t) {
1035+ return function (stream) {
1036+ this$1._disposeCurrent(t); // TODO: capture the result of this dispose
1037+ this$1.current = new Segment(t, Infinity, this$1, this$1.sink);
1038+ this$1.current.disposable = stream.source.run(this$1.current)(this$1.scheduler);
1042+SwitchSink.prototype.end = function (t, x) {
1044+ this._checkEnd(t, x);
1047+SwitchSink.prototype.dispose = function () {
1048+ return this._disposeCurrent(this.scheduler.now())
1051+SwitchSink.prototype._disposeCurrent = function (t) {
1052+ if (this.current !== null) {
1053+ return this.current._dispose(t)
1057+SwitchSink.prototype._disposeInner = function (t, inner) {
1058+ inner._dispose(t); // TODO: capture the result of this dispose
1059+ if (inner === this.current) {
1060+ this.current = null;
1064+SwitchSink.prototype._checkEnd = function (t, x) {
1065+ if (this.ended && this.current === null) {
1066+ this.sink.end(t, x);
1070+SwitchSink.prototype._endInner = function (t, x, inner) {
1071+ this._disposeInner(t, inner);
1072+ this._checkEnd(t, x);
1075+function Segment (min, max, outer, sink) {
1078+ this.outer = outer;
1080+ this.disposable = emptyDisposable;
1083+Segment.prototype.event = function (t) {
1086+ return function (x) {
1087+ if (t < this$1.max) {
1088+ this$1.sink.event(Math.max(t, this$1.min))(x);
1093+Segment.prototype.end = function (t, x) {
1094+ this.outer._endInner(Math.max(t, this.min), x, this);
673− if (--this.activeCount === 0) { this.sink.end(t); }
1097+Segment.prototype._dispose = function (t) {
1099+ this.disposable.dispose();
@@ -682,7 +1108,16 @@ exports.defaultScheduler = defaultScheduler;
682exports.scheduleTasks = scheduleTasks;
1108exports.scheduleTasks = scheduleTasks;
683exports.endTask = endTask;
1109exports.endTask = endTask;
684exports.eventTask = eventTask;
1110exports.eventTask = eventTask;
685−exports.tapEnd = tapEnd;
686−exports.tapEvent = tapEvent;
687exports.drain = drain;
1111exports.drain = drain;
688−exports.combine = combine;
1112+exports.flatMap = flatMap;
1113+exports.mergeConcurrently = mergeConcurrently;
1114+exports.join = join;
1115+exports.mergeMapConcurrently = mergeMapConcurrently;
1116+exports.combine2 = combine2;
1117+exports.combine3 = combine3;
1118+exports.combine4 = combine4;
1119+exports.combine5 = combine5;
1120+exports.merge = merge;
1121+exports.concat = concat;
1122+exports.continueWith = continueWith;
1123+exports.switch = switchLatest;