UNPKG

pnpm

Version:

Fast, disk space efficient package manager

2,097 lines (1,697 loc) • 116 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (factory((global.most = {}))); }(this, (function (exports) { 'use strict'; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function Stream (source) { this.source = source; } Stream.prototype.run = function (sink, scheduler) { return this.source.run(sink, scheduler) }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ // Non-mutating array operations // cons :: a -> [a] -> [a] // a with x prepended function cons (x, a) { var l = a.length; var b = new Array(l + 1); b[0] = x; for (var i = 0; i < l; ++i) { b[i + 1] = a[i]; } return b } // append :: a -> [a] -> [a] // a with x appended function append (x, a) { var l = a.length; var b = new Array(l + 1); for (var i = 0; i < l; ++i) { b[i] = a[i]; } b[l] = x; return b } // drop :: Int -> [a] -> [a] // drop first n elements function drop (n, a) { // eslint-disable-line complexity if (n < 0) { throw new TypeError('n must be >= 0') } var l = a.length; if (n === 0 || l === 0) { return a } if (n >= l) { return [] } return unsafeDrop(n, a, l - n) } // unsafeDrop :: Int -> [a] -> Int -> [a] // Internal helper for drop function unsafeDrop (n, a, l) { var b = new Array(l); for (var i = 0; i < l; ++i) { b[i] = a[n + i]; } return b } // tail :: [a] -> [a] // drop head element function tail (a) { return drop(1, a) } // copy :: [a] -> [a] // duplicate a (shallow duplication) function copy (a) { var l = a.length; var b = new Array(l); for (var i = 0; i < l; ++i) { b[i] = a[i]; } return b } // map :: (a -> b) -> [a] -> [b] // transform each element with f function map (f, a) { var l = a.length; var b = new Array(l); for (var i = 0; i < l; ++i) { b[i] = f(a[i]); } return b } // reduce :: (a -> b -> a) -> a -> [b] -> a // accumulate via left-fold function reduce (f, z, a) { var r = z; for (var i = 0, l = a.length; i < l; ++i) { r = f(r, a[i], i); } return r } // replace :: a -> Int -> [a] // replace element at index function replace (x, i, a) { // eslint-disable-line complexity if (i < 0) { throw new TypeError('i must be >= 0') } var l = a.length; var b = new Array(l); for (var j = 0; j < l; ++j) { b[j] = i === j ? x : a[j]; } return b } // remove :: Int -> [a] -> [a] // remove element at index function remove (i, a) { // eslint-disable-line complexity if (i < 0) { throw new TypeError('i must be >= 0') } var l = a.length; if (l === 0 || i >= l) { // exit early if index beyond end of array return a } if (l === 1) { // exit early if index in bounds and length === 1 return [] } return unsafeRemove(i, a, l - 1) } // unsafeRemove :: Int -> [a] -> Int -> [a] // Internal helper to remove element at index function unsafeRemove (i, a, l) { var b = new Array(l); var j; for (j = 0; j < i; ++j) { b[j] = a[j]; } for (j = i; j < l; ++j) { b[j] = a[j + 1]; } return b } // removeAll :: (a -> boolean) -> [a] -> [a] // remove all elements matching a predicate function removeAll (f, a) { var l = a.length; var b = new Array(l); var j = 0; for (var x = (void 0), i = 0; i < l; ++i) { x = a[i]; if (!f(x)) { b[j] = x; ++j; } } b.length = j; return b } // findIndex :: a -> [a] -> Int // find index of x in a, from the left function findIndex (x, a) { for (var i = 0, l = a.length; i < l; ++i) { if (x === a[i]) { return i } } return -1 } // isArrayLike :: * -> boolean // Return true iff x is array-like function isArrayLike (x) { return x != null && typeof x.length === 'number' && typeof x !== 'function' } /** @license MIT License (c) copyright 2010-2016 original author or authors */ // id :: a -> a var id = function (x) { return x; }; // compose :: (b -> c) -> (a -> b) -> (a -> c) var compose = function (f, g) { return function (x) { return f(g(x)); }; }; // apply :: (a -> b) -> a -> b var apply = function (f, x) { return f(x); }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Create a new Disposable which will dispose its underlying resource. * @param {function} dispose function * @param {*?} data any data to be passed to disposer function * @constructor */ function Disposable (dispose, data) { this._dispose = dispose; this._data = data; } Disposable.prototype.dispose = function () { return this._dispose(this._data) }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function SettableDisposable () { this.disposable = void 0; this.disposed = false; this._resolve = void 0; var self = this; this.result = new Promise(function (resolve) { self._resolve = resolve; }); } SettableDisposable.prototype.setDisposable = function (disposable) { if (this.disposable !== void 0) { throw new Error('setDisposable called more than once') } this.disposable = disposable; if (this.disposed) { this._resolve(disposable.dispose()); } }; SettableDisposable.prototype.dispose = function () { if (this.disposed) { return this.result } this.disposed = true; if (this.disposable !== void 0) { this.result = this.disposable.dispose(); } return this.result }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function isPromise (p) { return p !== null && typeof p === 'object' && typeof p.then === 'function' } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ var map$1 = map; var identity = id; /** * Call disposable.dispose. If it returns a promise, catch promise * error and forward it through the provided sink. * @param {number} t time * @param {{dispose: function}} disposable * @param {{error: function}} sink * @return {*} result of disposable.dispose */ function tryDispose (t, disposable, sink) { var result = disposeSafely(disposable); return isPromise(result) ? result.catch(function (e) { sink.error(t, e); }) : result } /** * Create a new Disposable which will dispose its underlying resource * at most once. * @param {function} dispose function * @param {*?} data any data to be passed to disposer function * @return {Disposable} */ function create (dispose, data) { return once(new Disposable(dispose, data)) } /** * Create a noop disposable. Can be used to satisfy a Disposable * requirement when no actual resource needs to be disposed. * @return {Disposable|exports|module.exports} */ function empty$1 () { return new Disposable(identity, void 0) } /** * Create a disposable that will dispose all input disposables in parallel. * @param {Array<Disposable>} disposables * @return {Disposable} */ function all (disposables) { return create(disposeAll, disposables) } function disposeAll (disposables) { return Promise.all(map$1(disposeSafely, disposables)) } function disposeSafely (disposable) { try { return disposable.dispose() } catch (e) { return Promise.reject(e) } } /** * Create a disposable from a promise for another disposable * @param {Promise<Disposable>} disposablePromise * @return {Disposable} */ /** * Create a disposable proxy that allows its underlying disposable to * be set later. * @return {SettableDisposable} */ function settable () { return new SettableDisposable() } /** * Wrap an existing disposable (which may not already have been once()d) * so that it will only dispose its underlying resource at most once. * @param {{ dispose: function() }} disposable * @return {Disposable} wrapped disposable */ function once (disposable) { return new Disposable(disposeMemoized, memoized(disposable)) } function disposeMemoized (memoized) { if (!memoized.disposed) { memoized.disposed = true; memoized.value = disposeSafely(memoized.disposable); memoized.disposable = void 0; } return memoized.value } function memoized (disposable) { return { disposed: false, disposable: disposable, value: void 0 } } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function fatalError (e) { setTimeout(function () { throw e }, 0); } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function PropagateTask (run, value, sink) { this._run = run; this.value = value; this.sink = sink; this.active = true; } PropagateTask.event = function (value, sink) { return new PropagateTask(emit, value, sink) }; PropagateTask.end = function (value, sink) { return new PropagateTask(end, value, sink) }; PropagateTask.error = function (value, sink) { return new PropagateTask(error, value, sink) }; PropagateTask.prototype.dispose = function () { this.active = false; }; PropagateTask.prototype.run = function (t) { if (!this.active) { return } this._run(t, this.value, this.sink); }; PropagateTask.prototype.error = function (t, e) { if (!this.active) { return fatalError(e) } this.sink.error(t, e); }; function error (t, e, sink) { sink.error(t, e); } function emit (t, x, sink) { sink.event(t, x); } function end (t, x, sink) { sink.end(t, x); } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Stream containing only x * @param {*} x * @returns {Stream} */ function of (x) { return new Stream(new Just(x)) } function Just (x) { this.value = x; } Just.prototype.run = function (sink, scheduler) { return scheduler.asap(new PropagateTask(runJust, this.value, sink)) }; function runJust (t, x, sink) { sink.event(t, x); sink.end(t, void 0); } /** * Stream containing no events and ends immediately * @returns {Stream} */ function empty () { return EMPTY } function EmptySource () {} EmptySource.prototype.run = function (sink, scheduler) { var task = PropagateTask.end(void 0, sink); scheduler.asap(task); return create(disposeEmpty, task) }; function disposeEmpty (task) { return task.dispose() } var EMPTY = new Stream(new EmptySource()); /** * Stream containing no events and never ends * @returns {Stream} */ function never () { return NEVER } function NeverSource () {} NeverSource.prototype.run = function () { return empty$1() }; var NEVER = new Stream(new NeverSource()); /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function fromArray (a) { return new Stream(new ArraySource(a)) } function ArraySource (a) { this.array = a; } ArraySource.prototype.run = function (sink, scheduler) { return scheduler.asap(new PropagateTask(runProducer, this.array, sink)) }; function runProducer (t, array, sink) { for (var i = 0, l = array.length; i < l && this.active; ++i) { sink.event(t, array[i]); } this.active && sink.end(t); } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /* global Set, Symbol */ var iteratorSymbol; // Firefox ships a partial implementation using the name @@iterator. // https://bugzilla.mozilla.org/show_bug.cgi?id=907077#c14 if (typeof Set === 'function' && typeof new Set()['@@iterator'] === 'function') { iteratorSymbol = '@@iterator'; } else { iteratorSymbol = typeof Symbol === 'function' ? Symbol.iterator : '_es6shim_iterator_'; } function isIterable (o) { return typeof o[iteratorSymbol] === 'function' } function getIterator (o) { return o[iteratorSymbol]() } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function fromIterable (iterable) { return new Stream(new IterableSource(iterable)) } function IterableSource (iterable) { this.iterable = iterable; } IterableSource.prototype.run = function (sink, scheduler) { return scheduler.asap(new PropagateTask(runProducer$1, getIterator(this.iterable), sink)) }; function runProducer$1 (t, iterator, sink) { var r = iterator.next(); while (!r.done && this.active) { sink.event(t, r.value); r = iterator.next(); } sink.end(t, r.value); } function symbolObservablePonyfill(root) { var result; var Symbol = root.Symbol; if (typeof Symbol === 'function') { if (Symbol.observable) { result = Symbol.observable; } else { result = Symbol('observable'); Symbol.observable = result; } } else { result = '@@observable'; } return result; } /* global window */ var root; if (typeof self !== 'undefined') { root = self; } else if (typeof window !== 'undefined') { root = window; } else if (typeof global !== 'undefined') { root = global; } else if (typeof module !== 'undefined') { root = module; } else { root = Function('return this')(); } var result = symbolObservablePonyfill(root); /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function getObservable (o) { // eslint-disable-line complexity var obs = null; if (o) { // Access foreign method only once var method = o[result]; if (typeof method === 'function') { obs = method.call(o); if (!(obs && typeof obs.subscribe === 'function')) { throw new TypeError('invalid observable ' + obs) } } } return obs } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function tryEvent (t, x, sink) { try { sink.event(t, x); } catch (e) { sink.error(t, e); } } function tryEnd (t, x, sink) { try { sink.end(t, x); } catch (e) { sink.error(t, e); } } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function fromObservable (observable) { return new Stream(new ObservableSource(observable)) } function ObservableSource (observable) { this.observable = observable; } ObservableSource.prototype.run = function (sink, scheduler) { var sub = this.observable.subscribe(new SubscriberSink(sink, scheduler)); if (typeof sub === 'function') { return create(sub) } else if (sub && typeof sub.unsubscribe === 'function') { return create(unsubscribe, sub) } throw new TypeError('Observable returned invalid subscription ' + String(sub)) }; function SubscriberSink (sink, scheduler) { this.sink = sink; this.scheduler = scheduler; } SubscriberSink.prototype.next = function (x) { tryEvent(this.scheduler.now(), x, this.sink); }; SubscriberSink.prototype.complete = function (x) { tryEnd(this.scheduler.now(), x, this.sink); }; SubscriberSink.prototype.error = function (e) { this.sink.error(this.scheduler.now(), e); }; function unsubscribe (subscription) { return subscription.unsubscribe() } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function from (a) { // eslint-disable-line complexity if (a instanceof Stream) { return a } var observable = getObservable(a); if (observable != null) { return fromObservable(observable) } if (Array.isArray(a) || isArrayLike(a)) { return fromArray(a) } if (isIterable(a)) { return fromIterable(a) } throw new TypeError('from(x) must be observable, iterable, or array-like: ' + a) } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Create a stream that emits the current time periodically * @param {Number} period periodicity of events in millis * @param {*} deprecatedValue @deprecated value to emit each period * @returns {Stream} new stream that emits the current time every period */ function periodic (period, deprecatedValue) { return new Stream(new Periodic(period, deprecatedValue)) } function Periodic (period, value) { this.period = period; this.value = value; } Periodic.prototype.run = function (sink, scheduler) { return scheduler.periodic(this.period, PropagateTask.event(this.value, sink)) }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function ScheduledTask (delay, period, task, scheduler) { this.time = delay; this.period = period; this.task = task; this.scheduler = scheduler; this.active = true; } ScheduledTask.prototype.run = function () { return this.task.run(this.time) }; ScheduledTask.prototype.error = function (e) { return this.task.error(this.time, e) }; ScheduledTask.prototype.dispose = function () { this.scheduler.cancel(this); return this.task.dispose() }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function defer (task) { return Promise.resolve(task).then(runTask) } function runTask (task) { try { return task.run() } catch (e) { return task.error(e) } } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function Scheduler (timer, timeline) { this.timer = timer; this.timeline = timeline; this._timer = null; this._nextArrival = Infinity; var self = this; this._runReadyTasksBound = function () { self._runReadyTasks(self.now()); }; } Scheduler.prototype.now = function () { return this.timer.now() }; Scheduler.prototype.asap = function (task) { return this.schedule(0, -1, task) }; Scheduler.prototype.delay = function (delay, task) { return this.schedule(delay, -1, task) }; Scheduler.prototype.periodic = function (period, task) { return this.schedule(0, period, task) }; Scheduler.prototype.schedule = function (delay, period, task) { var now = this.now(); var st = new ScheduledTask(now + Math.max(0, delay), period, task, this); this.timeline.add(st); this._scheduleNextRun(now); return st }; Scheduler.prototype.cancel = function (task) { task.active = false; if (this.timeline.remove(task)) { this._reschedule(); } }; Scheduler.prototype.cancelAll = function (f) { this.timeline.removeAll(f); this._reschedule(); }; Scheduler.prototype._reschedule = function () { if (this.timeline.isEmpty()) { this._unschedule(); } else { this._scheduleNextRun(this.now()); } }; Scheduler.prototype._unschedule = function () { this.timer.clearTimer(this._timer); this._timer = null; }; Scheduler.prototype._scheduleNextRun = function (now) { // eslint-disable-line complexity if (this.timeline.isEmpty()) { return } var nextArrival = this.timeline.nextArrival(); if (this._timer === null) { this._scheduleNextArrival(nextArrival, now); } else if (nextArrival < this._nextArrival) { this._unschedule(); this._scheduleNextArrival(nextArrival, now); } }; Scheduler.prototype._scheduleNextArrival = function (nextArrival, now) { this._nextArrival = nextArrival; var delay = Math.max(0, nextArrival - now); this._timer = this.timer.setTimer(this._runReadyTasksBound, delay); }; Scheduler.prototype._runReadyTasks = function (now) { this._timer = null; this.timeline.runTasks(now, runTask); this._scheduleNextRun(this.now()); }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /* global setTimeout, clearTimeout */ function ClockTimer () {} ClockTimer.prototype.now = Date.now; ClockTimer.prototype.setTimer = function (f, dt) { return dt <= 0 ? runAsap(f) : setTimeout(f, dt) }; ClockTimer.prototype.clearTimer = function (t) { return t instanceof Asap ? t.cancel() : clearTimeout(t) }; function Asap (f) { this.f = f; this.active = true; } Asap.prototype.run = function () { return this.active && this.f() }; Asap.prototype.error = function (e) { throw e }; Asap.prototype.cancel = function () { this.active = false; }; function runAsap (f) { var task = new Asap(f); defer(task); return task } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function Timeline () { this.tasks = []; } Timeline.prototype.nextArrival = function () { return this.isEmpty() ? Infinity : this.tasks[0].time }; Timeline.prototype.isEmpty = function () { return this.tasks.length === 0 }; Timeline.prototype.add = function (st) { insertByTime(st, this.tasks); }; Timeline.prototype.remove = function (st) { var i = binarySearch(st.time, this.tasks); if (i >= 0 && i < this.tasks.length) { var at = findIndex(st, this.tasks[i].events); if (at >= 0) { this.tasks[i].events.splice(at, 1); return true } } return false }; Timeline.prototype.removeAll = function (f) { var this$1 = this; for (var i = 0, l = this.tasks.length; i < l; ++i) { removeAllFrom(f, this$1.tasks[i]); } }; Timeline.prototype.runTasks = function (t, runTask) { var this$1 = this; var tasks = this.tasks; var l = tasks.length; var i = 0; while (i < l && tasks[i].time <= t) { ++i; } this.tasks = tasks.slice(i); // Run all ready tasks for (var j = 0; j < i; ++j) { this$1.tasks = runTasks(runTask, tasks[j], this$1.tasks); } }; function runTasks (runTask, timeslot, tasks) { // eslint-disable-line complexity var events = timeslot.events; for (var i = 0; i < events.length; ++i) { var task = events[i]; if (task.active) { runTask(task); // Reschedule periodic repeating tasks // Check active again, since a task may have canceled itself if (task.period >= 0 && task.active) { task.time = task.time + task.period; insertByTime(task, tasks); } } } return tasks } function insertByTime (task, timeslots) { // eslint-disable-line complexity var l = timeslots.length; if (l === 0) { timeslots.push(newTimeslot(task.time, [task])); return } var i = binarySearch(task.time, timeslots); if (i >= l) { timeslots.push(newTimeslot(task.time, [task])); } else if (task.time === timeslots[i].time) { timeslots[i].events.push(task); } else { timeslots.splice(i, 0, newTimeslot(task.time, [task])); } } function removeAllFrom (f, timeslot) { timeslot.events = removeAll(f, timeslot.events); } function binarySearch (t, sortedArray) { // eslint-disable-line complexity var lo = 0; var hi = sortedArray.length; var mid, y; while (lo < hi) { mid = Math.floor((lo + hi) / 2); y = sortedArray[mid]; if (t === y.time) { return mid } else if (t < y.time) { hi = mid; } else { lo = mid + 1; } } return hi } function newTimeslot (t, events) { return { time: t, events: events } } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ var defaultScheduler = new Scheduler(new ClockTimer(), new Timeline()); /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function subscribe (subscriber, stream) { if (Object(subscriber) !== subscriber) { throw new TypeError('subscriber must be an object') } var disposable = settable(); var observer = new SubscribeObserver(fatalError, subscriber, disposable); disposable.setDisposable(stream.source.run(observer, defaultScheduler)); return new Subscription(disposable) } function SubscribeObserver (fatalError$$1, subscriber, disposable) { this.fatalError = fatalError$$1; this.subscriber = subscriber; this.disposable = disposable; } SubscribeObserver.prototype.event = function (t, x) { if (!this.disposable.disposed && typeof this.subscriber.next === 'function') { this.subscriber.next(x); } }; SubscribeObserver.prototype.end = function (t, x) { if (!this.disposable.disposed) { var s = this.subscriber; var fatalError$$1 = this.fatalError; Promise.resolve(this.disposable.dispose()).then(function () { if (typeof s.complete === 'function') { s.complete(x); } }).catch(function (e) { throwError(e, s, fatalError$$1); }); } }; SubscribeObserver.prototype.error = function (t, e) { var s = this.subscriber; var fatalError$$1 = this.fatalError; Promise.resolve(this.disposable.dispose()).then(function () { throwError(e, s, fatalError$$1); }); }; function Subscription (disposable) { this.disposable = disposable; } Subscription.prototype.unsubscribe = function () { this.disposable.dispose(); }; function throwError (e1, subscriber, throwError) { if (typeof subscriber.error === 'function') { try { subscriber.error(e1); } catch (e2) { throwError(e2); } } else { throwError(e1); } } /** @license MIT License (c) copyright 2010-2017 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function thru (f, stream) { return f(stream) } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function EventTargetSource (event, source, capture) { this.event = event; this.source = source; this.capture = capture; } EventTargetSource.prototype.run = function (sink, scheduler) { function addEvent (e) { tryEvent(scheduler.now(), e, sink); } this.source.addEventListener(this.event, addEvent, this.capture); return create(disposeEventTarget, { target: this, addEvent: addEvent }) }; function disposeEventTarget (info) { var target = info.target; target.source.removeEventListener(target.event, info.addEvent, target.capture); } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function DeferredSink (sink) { this.sink = sink; this.events = []; this.active = true; } DeferredSink.prototype.event = function (t, x) { if (!this.active) { return } if (this.events.length === 0) { defer(new PropagateAllTask(this.sink, t, this.events)); } this.events.push({ time: t, value: x }); }; DeferredSink.prototype.end = function (t, x) { if (!this.active) { return } this._end(new EndTask(t, x, this.sink)); }; DeferredSink.prototype.error = function (t, e) { this._end(new ErrorTask(t, e, this.sink)); }; DeferredSink.prototype._end = function (task) { this.active = false; defer(task); }; function PropagateAllTask (sink, time, events) { this.sink = sink; this.events = events; this.time = time; } PropagateAllTask.prototype.run = function () { var this$1 = this; var events = this.events; var sink = this.sink; var event; for (var i = 0, l = events.length; i < l; ++i) { event = events[i]; this$1.time = event.time; sink.event(event.time, event.value); } events.length = 0; }; PropagateAllTask.prototype.error = function (e) { this.sink.error(this.time, e); }; function EndTask (t, x, sink) { this.time = t; this.value = x; this.sink = sink; } EndTask.prototype.run = function () { this.sink.end(this.time, this.value); }; EndTask.prototype.error = function (e) { this.sink.error(this.time, e); }; function ErrorTask (t, e, sink) { this.time = t; this.value = e; this.sink = sink; } ErrorTask.prototype.run = function () { this.sink.error(this.time, this.value); }; ErrorTask.prototype.error = function (e) { throw e }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function EventEmitterSource (event, source) { this.event = event; this.source = source; } EventEmitterSource.prototype.run = function (sink, scheduler) { // NOTE: Because EventEmitter allows events in the same call stack as // a listener is added, use a DeferredSink to buffer events // until the stack clears, then propagate. This maintains most.js's // invariant that no event will be delivered in the same call stack // as an observer begins observing. var dsink = new DeferredSink(sink); function addEventVariadic (a) { var arguments$1 = arguments; var l = arguments.length; if (l > 1) { var arr = new Array(l); for (var i = 0; i < l; ++i) { arr[i] = arguments$1[i]; } tryEvent(scheduler.now(), arr, dsink); } else { tryEvent(scheduler.now(), a, dsink); } } this.source.addListener(this.event, addEventVariadic); return create(disposeEventEmitter, { target: this, addEvent: addEventVariadic }) }; function disposeEventEmitter (info) { var target = info.target; target.source.removeListener(target.event, info.addEvent); } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Create a stream from an EventTarget, such as a DOM Node, or EventEmitter. * @param {String} event event type name, e.g. 'click' * @param {EventTarget|EventEmitter} source EventTarget or EventEmitter * @param {*?} capture for DOM events, whether to use * capturing--passed as 3rd parameter to addEventListener. * @returns {Stream} stream containing all events of the specified type * from the source. */ function fromEvent (event, source, capture) { // eslint-disable-line complexity var s; if (typeof source.addEventListener === 'function' && typeof source.removeEventListener === 'function') { if (arguments.length < 3) { capture = false; } s = new EventTargetSource(event, source, capture); } else if (typeof source.addListener === 'function' && typeof source.removeListener === 'function') { s = new EventEmitterSource(event, source); } else { throw new Error('source must support addEventListener/removeEventListener or addListener/removeListener') } return new Stream(s) } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function withDefaultScheduler (source) { return withScheduler(source, defaultScheduler) } function withScheduler (source, scheduler) { return new Promise(function (resolve, reject) { runSource(source, scheduler, resolve, reject); }) } function runSource (source, scheduler, resolve, reject) { var disposable = settable(); var observer = new Drain(resolve, reject, disposable); disposable.setDisposable(source.run(observer, scheduler)); } function Drain (end, error, disposable) { this._end = end; this._error = error; this._disposable = disposable; this.active = true; } Drain.prototype.event = function (t, x) {}; Drain.prototype.end = function (t, x) { if (!this.active) { return } this.active = false; disposeThen(this._end, this._error, this._disposable, x); }; Drain.prototype.error = function (t, e) { this.active = false; disposeThen(this._error, this._error, this._disposable, e); }; function disposeThen (end, error, disposable, x) { Promise.resolve(disposable.dispose()).then(function () { end(x); }, error); } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * A sink mixin that simply forwards event, end, and error to * another sink. * @param sink * @constructor */ function Pipe (sink) { this.sink = sink; } Pipe.prototype.event = function (t, x) { return this.sink.event(t, x) }; Pipe.prototype.end = function (t, x) { return this.sink.end(t, x) }; Pipe.prototype.error = function (t, e) { return this.sink.error(t, e) }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function Filter (p, source) { this.p = p; this.source = source; } /** * Create a filtered source, fusing adjacent filter.filter if possible * @param {function(x:*):boolean} p filtering predicate * @param {{run:function}} source source to filter * @returns {Filter} filtered source */ Filter.create = function createFilter (p, source) { if (source instanceof Filter) { return new Filter(and(source.p, p), source.source) } return new Filter(p, source) }; Filter.prototype.run = function (sink, scheduler) { return this.source.run(new FilterSink(this.p, sink), scheduler) }; function FilterSink (p, sink) { this.p = p; this.sink = sink; } FilterSink.prototype.end = Pipe.prototype.end; FilterSink.prototype.error = Pipe.prototype.error; FilterSink.prototype.event = function (t, x) { var p = this.p; p(x) && this.sink.event(t, x); }; function and (p, q) { return function (x) { return p(x) && q(x) } } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function FilterMap (p, f, source) { this.p = p; this.f = f; this.source = source; } FilterMap.prototype.run = function (sink, scheduler) { return this.source.run(new FilterMapSink(this.p, this.f, sink), scheduler) }; function FilterMapSink (p, f, sink) { this.p = p; this.f = f; this.sink = sink; } FilterMapSink.prototype.event = function (t, x) { var f = this.f; var p = this.p; p(x) && this.sink.event(t, f(x)); }; FilterMapSink.prototype.end = Pipe.prototype.end; FilterMapSink.prototype.error = Pipe.prototype.error; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function Map (f, source) { this.f = f; this.source = source; } /** * Create a mapped source, fusing adjacent map.map, filter.map, * and filter.map.map if possible * @param {function(*):*} f mapping function * @param {{run:function}} source source to map * @returns {Map|FilterMap} mapped source, possibly fused */ Map.create = function createMap (f, source) { if (source instanceof Map) { return new Map(compose(f, source.f), source.source) } if (source instanceof Filter) { return new FilterMap(source.p, f, source.source) } return new Map(f, source) }; Map.prototype.run = function (sink, scheduler) { // eslint-disable-line no-extend-native return this.source.run(new MapSink(this.f, sink), scheduler) }; function MapSink (f, sink) { this.f = f; this.sink = sink; } MapSink.prototype.end = Pipe.prototype.end; MapSink.prototype.error = Pipe.prototype.error; MapSink.prototype.event = function (t, x) { var f = this.f; this.sink.event(t, f(x)); }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Transform each value in the stream by applying f to each * @param {function(*):*} f mapping function * @param {Stream} stream stream to map * @returns {Stream} stream containing items transformed by f */ function map$2 (f, stream) { return new Stream(Map.create(f, stream.source)) } /** * Replace each value in the stream with x * @param {*} x * @param {Stream} stream * @returns {Stream} stream containing items replaced with x */ function constant (x, stream) { return map$2(function () { return x }, stream) } /** * Perform a side effect for each item in the stream * @param {function(x:*):*} f side effect to execute for each item. The * return value will be discarded. * @param {Stream} stream stream to tap * @returns {Stream} new stream containing the same items as this stream */ function tap (f, stream) { return new Stream(new Tap(f, stream.source)) } function Tap (f, source) { this.source = source; this.f = f; } Tap.prototype.run = function (sink, scheduler) { return this.source.run(new TapSink(this.f, sink), scheduler) }; function TapSink (f, sink) { this.sink = sink; this.f = f; } TapSink.prototype.end = Pipe.prototype.end; TapSink.prototype.error = Pipe.prototype.error; TapSink.prototype.event = function (t, x) { var f = this.f; f(x); this.sink.event(t, x); }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Observe all the event values in the stream in time order. The * provided function `f` will be called for each event value * @param {function(x:T):*} f function to call with each event value * @param {Stream<T>} stream stream to observe * @return {Promise} promise that fulfills after the stream ends without * an error, or rejects if the stream ends with an error. */ function observe (f, stream) { return drain(tap(f, stream)) } /** * "Run" a stream by creating demand and consuming all events * @param {Stream<T>} stream stream to drain * @return {Promise} promise that fulfills after the stream ends without * an error, or rejects if the stream ends with an error. */ function drain (stream) { return withDefaultScheduler(stream.source) } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Generalized feedback loop. Call a stepper function for each event. The stepper * will be called with 2 params: the current seed and the an event value. It must * return a new { seed, value } pair. The `seed` will be fed back into the next * invocation of stepper, and the `value` will be propagated as the event value. * @param {function(seed:*, value:*):{seed:*, value:*}} stepper loop step function * @param {*} seed initial seed value passed to first stepper call * @param {Stream} stream event stream * @returns {Stream} new stream whose values are the `value` field of the objects * returned by the stepper */ function loop (stepper, seed, stream) { return new Stream(new Loop(stepper, seed, stream.source)) } function Loop (stepper, seed, source) { this.step = stepper; this.seed = seed; this.source = source; } Loop.prototype.run = function (sink, scheduler) { return this.source.run(new LoopSink(this.step, this.seed, sink), scheduler) }; function LoopSink (stepper, seed, sink) { this.step = stepper; this.seed = seed; this.sink = sink; } LoopSink.prototype.error = Pipe.prototype.error; LoopSink.prototype.event = function (t, x) { var result = this.step(this.seed, x); this.seed = result.seed; this.sink.event(t, result.value); }; LoopSink.prototype.end = function (t) { this.sink.end(t, this.seed); }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Create a stream containing successive reduce results of applying f to * the previous reduce result and the current stream item. * @param {function(result:*, x:*):*} f reducer function * @param {*} initial initial value * @param {Stream} stream stream to scan * @returns {Stream} new stream containing successive reduce results */ function scan (f, initial, stream) { return new Stream(new Scan(f, initial, stream.source)) } function Scan (f, z, source) { this.source = source; this.f = f; this.value = z; } Scan.prototype.run = function (sink, scheduler) { var d1 = scheduler.asap(PropagateTask.event(this.value, sink)); var d2 = this.source.run(new ScanSink(this.f, this.value, sink), scheduler); return all([d1, d2]) }; function ScanSink (f, z, sink) { this.f = f; this.value = z; this.sink = sink; } ScanSink.prototype.event = function (t, x) { var f = this.f; this.value = f(this.value, x); this.sink.event(t, this.value); }; ScanSink.prototype.error = Pipe.prototype.error; ScanSink.prototype.end = Pipe.prototype.end; /** * Reduce a stream to produce a single result. Note that reducing an infinite * stream will return a Promise that never fulfills, but that may reject if an error * occurs. * @param {function(result:*, x:*):*} f reducer function * @param {*} initial initial value * @param {Stream} stream to reduce * @returns {Promise} promise for the file result of the reduce */ function reduce$1 (f, initial, stream) { return withDefaultScheduler(new Reduce(f, initial, stream.source)) } function Reduce (f, z, source) { this.source = source; this.f = f; this.value = z; } Reduce.prototype.run = function (sink, scheduler) { return this.source.run(new ReduceSink(this.f, this.value, sink), scheduler) }; function ReduceSink (f, z, sink) { this.f = f; this.value = z; this.sink = sink; } ReduceSink.prototype.event = function (t, x) { var f = this.f; this.value = f(this.value, x); this.sink.event(t, this.value); }; ReduceSink.prototype.error = Pipe.prototype.error; ReduceSink.prototype.end = function (t) { this.sink.end(t, this.value); }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Compute a stream by unfolding tuples of future values from a seed value * Event times may be controlled by returning a Promise from f * @param {function(seed:*):{value:*, seed:*, done:boolean}|Promise<{value:*, seed:*, done:boolean}>} f unfolding function accepts * a seed and returns a new tuple with a value, new seed, and boolean done flag. * If tuple.done is true, the stream will end. * @param {*} seed seed value * @returns {Stream} stream containing all value of all tuples produced by the * unfolding function. */ function unfold (f, seed) { return new Stream(new UnfoldSource(f, seed)) } function UnfoldSource (f, seed) { this.f = f; this.value = seed; } UnfoldSource.prototype.run = function (sink, scheduler) { return new Unfold(this.f, this.value, sink, scheduler) }; function Unfold (f, x, sink, scheduler) { this.f = f; this.sink = sink; this.scheduler = scheduler; this.active = true; var self = this; function err (e) { self.sink.error(self.scheduler.now(), e); } function start (unfold) { return stepUnfold(unfold, x) } Promise.resolve(this).then(start).catch(err); } Unfold.prototype.dispose = function () { this.active = false; }; function stepUnfold (unfold, x) { var f = unfold.f; return Promise.resolve(f(x)).then(function (tuple) { return continueUnfold(unfold, tuple) }) } function continueUnfold (unfold, tuple) { if (tuple.done) { unfold.sink.end(unfold.scheduler.now(), tuple.value); return tuple.value } unfold.sink.event(unfold.scheduler.now(), tuple.value); if (!unfold.active) { return tuple.value } return stepUnfold(unfold, tuple.seed) } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Compute a stream by iteratively calling f to produce values * Event times may be controlled by returning a Promise from f * @param {function(x:*):*|Promise<*>} f * @param {*} x initial value * @returns {Stream} */ function iterate (f, x) { return new Stream(new IterateSource(f, x)) } function IterateSource (f, x) { this.f = f; this.value = x; } IterateSource.prototype.run = function (sink, scheduler) { return new Iterate(this.f, this.value, sink, scheduler) }; function Iterate (f, initial, sink, scheduler) { this.f = f; this.sink = sink; this.scheduler = scheduler; this.active = true; var x = initial; var self = this; function err (e) { self.sink.error(self.scheduler.now(), e); } function start (iterate) { return stepIterate(iterate, x) } Promise.resolve(this).then(start).catch(err); } Iterate.prototype.dispose = function () { this.active = false; }; function stepIterate (iterate, x) { iterate.sink.event(iterate.scheduler.now(), x); if (!iterate.active) { return x } var f = iterate.f; return Promise.resolve(f(x)).then(function (y) { return continueIterate(iterate, y) }) } function continueIterate (iterate, x) { return !iterate.active ? iterate.value : stepIterate(iterate, x) } /** @license MIT License (c) copyright 2010-2014 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * Compute a stream using an *async* generator, which yields promises * to control event times. * @param f * @returns {Stream} */ function generate (f /* ...args */) { return new Stream(new GenerateSource(f, tail(arguments))) } function GenerateSource (f, args) { this.f = f; this.args = args; } GenerateSource.prototype.run = function (sink, scheduler) { return new Generate(this.f.apply(void 0, this.args), sink, scheduler) }; function Generate (iterator, sink, scheduler) { this.iterator = iterator; this.sink = sink; this.scheduler = scheduler; this.active = true; var self = this; function err (e) { self.sink.error(self.scheduler.now(), e); } Promise.resolve(this).then(next).catch(err); } function next (generate, x) { return generate.active ? handle(generate, generate.iterator.next(x)) : x } function handle (generate, result) { if (result.done) { return generate.sink.end(generate.scheduler.now(), result.value) } return Promise.resolve(result.value).then(function (x) { return emit$1(generate, x) }, function (e) { return error$1(generate, e) }) } function emit$1 (generate, x) { generate.sink.event(generate.scheduler.now(), x); return next(generate, x) } function error$1 (generate, e) { return handle(generate, generate.iterator.throw(e)) } Generate.prototype.dispose = function () { this.active = false; }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function continueWith (f, stream) { return new Stream(new ContinueWith(f, stream.source)) } function ContinueWith (f, source) { this.f = f; this.source = source; } ContinueWith.prototype.run = function (sink, scheduler) { return new ContinueWithSink(this.f, this.source, sink, scheduler) }; function ContinueWithSink (f, source, sink, scheduler) { this.f = f; this.sink = sink; this.scheduler = scheduler; this.active = true; this.disposable = once(source.run(this, scheduler)); } ContinueWithSink.prototype.error = Pipe.prototype.error; ContinueWithSink.prototype.event = function (t, x) { if (!this.active) { return } this.sink.event(t, x); }; ContinueWithSink.prototype.end = function (t, x) { if (!this.active) { return } tryDispose(t, this.disposable, this.sink); this._startNext(t, x, this.sink); }; ContinueWithSink.prototype._startNext = function (t, x, sink) { try { this.disposable = this._continue(this.f, x, sink); } catch (e) { sink.error(t, e); } }; ContinueWithSink.prototype._continue = function (f, x, sink) { return f(x).source.run(sink, this.scheduler) }; ContinueWithSink.prototype.dispose = function () { this.active = false; return this.disposable.dispose() }; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /** * @param {*} x value to prepend * @param {Stream} stream * @returns {Stream} new stream with x prepended */ function cons$1 (x, stream) { return concat(of(x), stream) } /** * @param {Stream} left * @param {Stream} right * @returns {Stream} new stream containing all events in left followed by all * events in right. This *timeshifts* right to the end of left. */ function concat (left, right) { return continueWith(function () { return right }, left) } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function IndexSink (i, sink) { this.sink = sink; this.index = i; this.active = true; this.value = void 0; } IndexSink.prototype.event = function (t, x) { if (!this.active) { return } this.value = x; this.sink.event(t, this); }; IndexSink.prototype.end = function (t, x) { if (!this.active) { return } this.active = false; this.sink.end(t, { index: this.index, value: x }); }; IndexSink.prototype.error = Pipe.prototype.error; /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ function invoke (f, args) { /* eslint complexity: [2,7] */ switch (args.length) { case 0: return f() case 1: return f(args[0]) case 2: return f(args[0], args[1]) case 3: return f(args[0], args[1], args[2]) case 4: return f(args[0], args[1], args[2], args[3]) case 5: return f(args[0], args[1], args[2], args[3], args[4]) default: return f.apply(void 0, args) } } /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ var map$3 = map; var tail$1 = tail; /** * Combine latest events from all input streams * @param {function(...events):*} f function to combine most recent events * @returns {Stream} stream containing the result of applying f to the most recent * event of each input stream, whenever a new event arrives on any stream. */ function combine (f /* ...streams */) { return combineArray(f, tail$1(arguments)) } /** * Combine latest events from all input streams * @param {function(...events):*} f function to combine most recent events * @p