'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.PropagateTask = exports.defaultScheduler = exports.multicast = exports.throwError = exports.flatMapError = exports.recoverWith = exports.await = exports.awaitPromises = exports.fromPromise = exports.debounce = exports.throttle = exports.timestamp = exports.delay = exports.during = exports.since = exports.skipUntil = exports.until = exports.takeUntil = exports.skipAfter = exports.skipWhile = exports.takeWhile = exports.slice = exports.skip = exports.take = exports.distinctBy = exports.skipRepeatsWith = exports.distinct = exports.skipRepeats = exports.filter = exports.switch = exports.switchLatest = exports.zipArray = exports.zip = exports.sampleWith = exports.sampleArray = exports.sample = exports.combineArray = exports.combine = exports.mergeArray = exports.merge = exports.mergeConcurrently = exports.concatMap = exports.flatMapEnd = exports.continueWith = exports.join = exports.chain = exports.flatMap = exports.transduce = exports.ap = exports.tap = exports.constant = exports.map = exports.startWith = exports.concat = exports.generate = exports.iterate = exports.unfold = exports.reduce = exports.scan = exports.loop = exports.drain = exports.forEach = exports.observe = exports.fromEvent = exports.periodic = exports.from = exports.never = exports.empty = exports.just = exports.of = exports.Stream = undefined; var _fromEvent = require('./source/fromEvent'); Object.defineProperty(exports, 'fromEvent', { enumerable: true, get: function () { return _fromEvent.fromEvent; } }); var _unfold = require('./source/unfold'); Object.defineProperty(exports, 'unfold', { enumerable: true, get: function () { return _unfold.unfold; } }); var _iterate = require('./source/iterate'); Object.defineProperty(exports, 'iterate', { enumerable: true, get: function () { return _iterate.iterate; } }); var _generate = require('./source/generate'); Object.defineProperty(exports, 'generate', { enumerable: true, get: function () { return _generate.generate; } }); var _Stream = require('./Stream'); var _Stream2 = _interopRequireDefault(_Stream); var _prelude = require('@most/prelude'); var base = _interopRequireWildcard(_prelude); var _core = require('./source/core'); var _from = require('./source/from'); var _periodic = require('./source/periodic'); var _symbolObservable = require('symbol-observable'); var _symbolObservable2 = _interopRequireDefault(_symbolObservable); var _subscribe = require('./observable/subscribe'); var _thru = require('./combinator/thru'); var _observe = require('./combinator/observe'); var _loop = require('./combinator/loop'); var _accumulate = require('./combinator/accumulate'); var _build = require('./combinator/build'); var _transform = require('./combinator/transform'); var _applicative = require('./combinator/applicative'); var _transduce = require('./combinator/transduce'); var _flatMap = require('./combinator/flatMap'); var _continueWith = require('./combinator/continueWith'); var _concatMap = require('./combinator/concatMap'); var _mergeConcurrently = require('./combinator/mergeConcurrently'); var _merge = require('./combinator/merge'); var _combine = require('./combinator/combine'); var _sample = require('./combinator/sample'); var _zip = require('./combinator/zip'); var _switch = require('./combinator/switch'); var _filter = require('./combinator/filter'); var _slice = require('./combinator/slice'); var _timeslice = require('./combinator/timeslice'); var _delay = require('./combinator/delay'); var _timestamp = require('./combinator/timestamp'); var _limit = require('./combinator/limit'); var _promises = require('./combinator/promises'); var _errors = require('./combinator/errors'); var _multicast = require('@most/multicast'); var _multicast2 = _interopRequireDefault(_multicast); var _defaultScheduler = require('./scheduler/defaultScheduler'); var _defaultScheduler2 = _interopRequireDefault(_defaultScheduler); var _PropagateTask = require('./scheduler/PropagateTask'); var _PropagateTask2 = _interopRequireDefault(_PropagateTask); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Core stream type * @type {Stream} */ /** @license MIT License (c) copyright 2010-2016 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ /* eslint import/first: 0 */ exports.Stream = _Stream2.default; // Add of and empty to constructor for fantasy-land compat _Stream2.default.of = _core.of; _Stream2.default.empty = _core.empty; // Add from to constructor for ES Observable compat _Stream2.default.from = _from.from; exports.of = _core.of; exports.just = _core.of; exports.empty = _core.empty; exports.never = _core.never; exports.from = _from.from; exports.periodic = _periodic.periodic; // ----------------------------------------------------------------------- // Draft ES Observable proposal interop // https://github.com/zenparsing/es-observable _Stream2.default.prototype.subscribe = function (subscriber) { return (0, _subscribe.subscribe)(subscriber, this); }; _Stream2.default.prototype[_symbolObservable2.default] = function () { return this; }; // ----------------------------------------------------------------------- // Fluent adapter /** * Adapt a functional stream transform to fluent style. * It applies f to the this stream object * @param {function(s: Stream): Stream} f function that * receives the stream itself and must return a new stream * @return {Stream} */ _Stream2.default.prototype.thru = function (f) { return (0, _thru.thru)(f, this); }; // ----------------------------------------------------------------------- // Adapting other sources /** * Create a stream of events from the supplied EventTarget or EventEmitter * @param {String} event event name * @param {EventTarget|EventEmitter} source EventTarget or EventEmitter. The source * must support either addEventListener/removeEventListener (w3c EventTarget: * http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget), * or addListener/removeListener (node EventEmitter: http://nodejs.org/api/events.html) * @returns {Stream} stream of events of the specified type from the source */ // ----------------------------------------------------------------------- // Observing exports.observe = _observe.observe; exports.forEach = _observe.observe; exports.drain = _observe.drain; /** * Process all the events in the stream * @returns {Promise} promise that fulfills when the stream ends, or rejects * if the stream fails with an unhandled error. */ _Stream2.default.prototype.observe = _Stream2.default.prototype.forEach = function (f) { return (0, _observe.observe)(f, this); }; /** * Consume all events in the stream, without providing a function to process each. * This causes a stream to become active and begin emitting events, and is useful * in cases where all processing has been setup upstream via other combinators, and * there is no need to process the terminal events. * @returns {Promise} promise that fulfills when the stream ends, or rejects * if the stream fails with an unhandled error. */ _Stream2.default.prototype.drain = function () { return (0, _observe.drain)(this); }; // ------------------------------------------------------- exports.loop = _loop.loop; /** * 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 * @returns {Stream} new stream whose values are the `value` field of the objects * returned by the stepper */ _Stream2.default.prototype.loop = function (stepper, seed) { return (0, _loop.loop)(stepper, seed, this); }; // ------------------------------------------------------- exports.scan = _accumulate.scan; exports.reduce = _accumulate.reduce; /** * 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 * @returns {Stream} new stream containing successive reduce results */ _Stream2.default.prototype.scan = function (f, initial) { return (0, _accumulate.scan)(f, initial, this); }; /** * Reduce the 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 optional initial value * @returns {Promise} promise for the file result of the reduce */ _Stream2.default.prototype.reduce = function (f, initial) { return (0, _accumulate.reduce)(f, initial, this); }; // ----------------------------------------------------------------------- // Building and extending exports.concat = _build.concat; exports.startWith = _build.cons; /** * @param {Stream} tail * @returns {Stream} new stream containing all items in this followed by * all items in tail */ _Stream2.default.prototype.concat = function (tail) { return (0, _build.concat)(this, tail); }; /** * @param {*} x value to prepend * @returns {Stream} a new stream with x prepended */ _Stream2.default.prototype.startWith = function (x) { return (0, _build.cons)(x, this); }; // ----------------------------------------------------------------------- // Transforming exports.map = _transform.map; exports.constant = _transform.constant; exports.tap = _transform.tap; exports.ap = _applicative.ap; /** * Transform each value in the stream by applying f to each * @param {function(*):*} f mapping function * @returns {Stream} stream containing items transformed by f */ _Stream2.default.prototype.map = function (f) { return (0, _transform.map)(f, this); }; /** * Assume this stream contains functions, and apply each function to each item * in the provided stream. This generates, in effect, a cross product. * @param {Stream} xs stream of items to which * @returns {Stream} stream containing the cross product of items */ _Stream2.default.prototype.ap = function (xs) { return (0, _applicative.ap)(this, xs); }; /** * Replace each value in the stream with x * @param {*} x * @returns {Stream} stream containing items replaced with x */ _Stream2.default.prototype.constant = function (x) { return (0, _transform.constant)(x, this); }; /** * 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. * @returns {Stream} new stream containing the same items as this stream */ _Stream2.default.prototype.tap = function (f) { return (0, _transform.tap)(f, this); }; // ----------------------------------------------------------------------- // Transducer support exports.transduce = _transduce.transduce; /** * Transform this stream by passing its events through a transducer. * @param {function} transducer transducer function * @return {Stream} stream of events transformed by the transducer */ _Stream2.default.prototype.transduce = function (transducer) { return (0, _transduce.transduce)(transducer, this); }; // ----------------------------------------------------------------------- // FlatMapping // @deprecated flatMap, use chain instead exports.flatMap = _flatMap.flatMap; exports.chain = _flatMap.flatMap; exports.join = _flatMap.join; /** * Map each value in the stream to a new stream, and merge it into the * returned outer stream. Event arrival times are preserved. * @param {function(x:*):Stream} f chaining function, must return a Stream * @returns {Stream} new stream containing all events from each stream returned by f */ _Stream2.default.prototype.chain = function (f) { return (0, _flatMap.flatMap)(f, this); }; // @deprecated use chain instead _Stream2.default.prototype.flatMap = _Stream2.default.prototype.chain; /** * Monadic join. Flatten a Stream> to Stream by merging inner * streams to the outer. Event arrival times are preserved. * @returns {Stream} new stream containing all events of all inner streams */ _Stream2.default.prototype.join = function () { return (0, _flatMap.join)(this); }; // @deprecated flatMapEnd, use continueWith instead exports.continueWith = _continueWith.continueWith; exports.flatMapEnd = _continueWith.continueWith; /** * Map the end event to a new stream, and begin emitting its values. * @param {function(x:*):Stream} f function that receives the end event value, * and *must* return a new Stream to continue with. * @returns {Stream} new stream that emits all events from the original stream, * followed by all events from the stream returned by f. */ _Stream2.default.prototype.continueWith = function (f) { return (0, _continueWith.continueWith)(f, this); }; // @deprecated use continueWith instead _Stream2.default.prototype.flatMapEnd = _Stream2.default.prototype.continueWith; exports.concatMap = _concatMap.concatMap; _Stream2.default.prototype.concatMap = function (f) { return (0, _concatMap.concatMap)(f, this); }; // ----------------------------------------------------------------------- // Concurrent merging exports.mergeConcurrently = _mergeConcurrently.mergeConcurrently; /** * Flatten a Stream> to Stream by merging inner * streams to the outer, limiting the number of inner streams that may * be active concurrently. * @param {number} concurrency at most this many inner streams will be * allowed to be active concurrently. * @return {Stream} new stream containing all events of all inner * streams, with limited concurrency. */ _Stream2.default.prototype.mergeConcurrently = function (concurrency) { return (0, _mergeConcurrently.mergeConcurrently)(concurrency, this); }; // ----------------------------------------------------------------------- // Merging exports.merge = _merge.merge; exports.mergeArray = _merge.mergeArray; /** * Merge this stream and all the provided streams * @returns {Stream} stream containing items from this stream and s in time * order. If two events are simultaneous they will be merged in * arbitrary order. */ _Stream2.default.prototype.merge = function () /* ...streams */{ return (0, _merge.mergeArray)(base.cons(this, arguments)); }; // ----------------------------------------------------------------------- // Combining exports.combine = _combine.combine; exports.combineArray = _combine.combineArray; /** * 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. */ _Stream2.default.prototype.combine = function (f /*, ...streams */) { return (0, _combine.combineArray)(f, base.replace(this, 0, arguments)); }; // ----------------------------------------------------------------------- // Sampling exports.sample = _sample.sample; exports.sampleArray = _sample.sampleArray; exports.sampleWith = _sample.sampleWith; /** * When an event arrives on sampler, emit the latest event value from stream. * @param {Stream} sampler stream of events at whose arrival time * signal's latest value will be propagated * @returns {Stream} sampled stream of values */ _Stream2.default.prototype.sampleWith = function (sampler) { return (0, _sample.sampleWith)(sampler, this); }; /** * When an event arrives on this stream, emit the result of calling f with the latest * values of all streams being sampled * @param {function(...values):*} f function to apply to each set of sampled values * @returns {Stream} stream of sampled and transformed values */ _Stream2.default.prototype.sample = function (f /* ...streams */) { return (0, _sample.sampleArray)(f, this, base.tail(arguments)); }; // ----------------------------------------------------------------------- // Zipping exports.zip = _zip.zip; exports.zipArray = _zip.zipArray; /** * Pair-wise combine items with those in s. Given 2 streams: * [1,2,3] zipWith f [4,5,6] -> [f(1,4),f(2,5),f(3,6)] * Note: zip causes fast streams to buffer and wait for slow streams. * @param {function(a:Stream, b:Stream, ...):*} f function to combine items * @returns {Stream} new stream containing pairs */ _Stream2.default.prototype.zip = function (f /*, ...streams */) { return (0, _zip.zipArray)(f, base.replace(this, 0, arguments)); }; // ----------------------------------------------------------------------- // Switching // @deprecated switch, use switchLatest instead exports.switchLatest = _switch.switchLatest; exports.switch = _switch.switchLatest; /** * Given a stream of streams, return a new stream that adopts the behavior * of the most recent inner stream. * @returns {Stream} switching stream */ _Stream2.default.prototype.switchLatest = function () { return (0, _switch.switchLatest)(this); }; // @deprecated use switchLatest instead _Stream2.default.prototype.switch = _Stream2.default.prototype.switchLatest; // ----------------------------------------------------------------------- // Filtering // @deprecated distinct, use skipRepeats instead // @deprecated distinctBy, use skipRepeatsWith instead exports.filter = _filter.filter; exports.skipRepeats = _filter.skipRepeats; exports.distinct = _filter.skipRepeats; exports.skipRepeatsWith = _filter.skipRepeatsWith; exports.distinctBy = _filter.skipRepeatsWith; /** * Retain only items matching a predicate * stream: -12345678- * filter(x => x % 2 === 0, stream): --2-4-6-8- * @param {function(x:*):boolean} p filtering predicate called for each item * @returns {Stream} stream containing only items for which predicate returns truthy */ _Stream2.default.prototype.filter = function (p) { return (0, _filter.filter)(p, this); }; /** * Skip repeated events, using === to compare items * stream: -abbcd- * distinct(stream): -ab-cd- * @returns {Stream} stream with no repeated events */ _Stream2.default.prototype.skipRepeats = function () { return (0, _filter.skipRepeats)(this); }; /** * Skip repeated events, using supplied equals function to compare items * @param {function(a:*, b:*):boolean} equals function to compare items * @returns {Stream} stream with no repeated events */ _Stream2.default.prototype.skipRepeatsWith = function (equals) { return (0, _filter.skipRepeatsWith)(equals, this); }; // ----------------------------------------------------------------------- // Slicing exports.take = _slice.take; exports.skip = _slice.skip; exports.slice = _slice.slice; exports.takeWhile = _slice.takeWhile; exports.skipWhile = _slice.skipWhile; exports.skipAfter = _slice.skipAfter; /** * stream: -abcd- * take(2, stream): -ab| * @param {Number} n take up to this many events * @returns {Stream} stream containing at most the first n items from this stream */ _Stream2.default.prototype.take = function (n) { return (0, _slice.take)(n, this); }; /** * stream: -abcd-> * skip(2, stream): ---cd-> * @param {Number} n skip this many events * @returns {Stream} stream not containing the first n events */ _Stream2.default.prototype.skip = function (n) { return (0, _slice.skip)(n, this); }; /** * Slice a stream by event index. Equivalent to, but more efficient than * stream.take(end).skip(start); * NOTE: Negative start and end are not supported * @param {Number} start skip all events before the start index * @param {Number} end allow all events from the start index to the end index * @returns {Stream} stream containing items where start <= index < end */ _Stream2.default.prototype.slice = function (start, end) { return (0, _slice.slice)(start, end, this); }; /** * stream: -123451234-> * takeWhile(x => x < 5, stream): -1234| * @param {function(x:*):boolean} p predicate * @returns {Stream} stream containing items up to, but not including, the * first item for which p returns falsy. */ _Stream2.default.prototype.takeWhile = function (p) { return (0, _slice.takeWhile)(p, this); }; /** * stream: -123451234-> * skipWhile(x => x < 5, stream): -----51234-> * @param {function(x:*):boolean} p predicate * @returns {Stream} stream containing items following *and including* the * first item for which p returns falsy. */ _Stream2.default.prototype.skipWhile = function (p) { return (0, _slice.skipWhile)(p, this); }; /** * stream: -123456789-> * skipAfter(x => x === 5, stream):-12345| * @param {function(x:*):boolean} p predicate * @returns {Stream} stream containing items up to, *and including*, the * first item for which p returns truthy. */ _Stream2.default.prototype.skipAfter = function (p) { return (0, _slice.skipAfter)(p, this); }; // ----------------------------------------------------------------------- // Time slicing // @deprecated takeUntil, use until instead // @deprecated skipUntil, use since instead exports.takeUntil = _timeslice.takeUntil; exports.until = _timeslice.takeUntil; exports.skipUntil = _timeslice.skipUntil; exports.since = _timeslice.skipUntil; exports.during = _timeslice.during; /** * stream: -a-b-c-d-e-f-g-> * signal: -------x * takeUntil(signal, stream): -a-b-c-| * @param {Stream} signal retain only events in stream before the first * event in signal * @returns {Stream} new stream containing only events that occur before * the first event in signal. */ _Stream2.default.prototype.until = function (signal) { return (0, _timeslice.takeUntil)(signal, this); }; // @deprecated use until instead _Stream2.default.prototype.takeUntil = _Stream2.default.prototype.until; /** * stream: -a-b-c-d-e-f-g-> * signal: -------x * takeUntil(signal, stream): -------d-e-f-g-> * @param {Stream} signal retain only events in stream at or after the first * event in signal * @returns {Stream} new stream containing only events that occur after * the first event in signal. */ _Stream2.default.prototype.since = function (signal) { return (0, _timeslice.skipUntil)(signal, this); }; // @deprecated use since instead _Stream2.default.prototype.skipUntil = _Stream2.default.prototype.since; /** * stream: -a-b-c-d-e-f-g-> * timeWindow: -----s * s: -----t * stream.during(timeWindow): -----c-d-e-| * @param {Stream} timeWindow a stream whose first event (s) represents * the window start time. That event (s) is itself a stream whose first event (t) * represents the window end time * @returns {Stream} new stream containing only events within the provided timespan */ _Stream2.default.prototype.during = function (timeWindow) { return (0, _timeslice.during)(timeWindow, this); }; // ----------------------------------------------------------------------- // Delaying exports.delay = _delay.delay; /** * @param {Number} delayTime milliseconds to delay each item * @returns {Stream} new stream containing the same items, but delayed by ms */ _Stream2.default.prototype.delay = function (delayTime) { return (0, _delay.delay)(delayTime, this); }; // ----------------------------------------------------------------------- // Getting event timestamp exports.timestamp = _timestamp.timestamp; /** * Expose event timestamps into the stream. Turns a Stream into * Stream<{time:t, value:X}> * @returns {Stream<{time:number, value:*}>} */ _Stream2.default.prototype.timestamp = function () { return (0, _timestamp.timestamp)(this); }; // ----------------------------------------------------------------------- // Rate limiting exports.throttle = _limit.throttle; exports.debounce = _limit.debounce; /** * Limit the rate of events * stream: abcd----abcd---- * throttle(2, stream): a-c-----a-c----- * @param {Number} period time to suppress events * @returns {Stream} new stream that skips events for throttle period */ _Stream2.default.prototype.throttle = function (period) { return (0, _limit.throttle)(period, this); }; /** * Wait for a burst of events to subside and emit only the last event in the burst * stream: abcd----abcd---- * debounce(2, stream): -----d-------d-- * @param {Number} period events occurring more frequently than this * on the provided scheduler will be suppressed * @returns {Stream} new debounced stream */ _Stream2.default.prototype.debounce = function (period) { return (0, _limit.debounce)(period, this); }; // ----------------------------------------------------------------------- // Awaiting Promises // @deprecated await, use awaitPromises instead exports.fromPromise = _promises.fromPromise; exports.awaitPromises = _promises.awaitPromises; exports.await = _promises.awaitPromises; /** * Await promises, turning a Stream> into Stream. Preserves * event order, but timeshifts events based on promise resolution time. * @returns {Stream} stream containing non-promise values */ _Stream2.default.prototype.awaitPromises = function () { return (0, _promises.awaitPromises)(this); }; // @deprecated use awaitPromises instead _Stream2.default.prototype.await = _Stream2.default.prototype.awaitPromises; // ----------------------------------------------------------------------- // Error handling // @deprecated flatMapError, use recoverWith instead exports.recoverWith = _errors.recoverWith; exports.flatMapError = _errors.flatMapError; exports.throwError = _errors.throwError; /** * If this stream encounters an error, recover and continue with items from stream * returned by f. * stream: -a-b-c-X- * f(X): d-e-f-g- * flatMapError(f, stream): -a-b-c-d-e-f-g- * @param {function(error:*):Stream} f function which returns a new stream * @returns {Stream} new stream which will recover from an error by calling f */ _Stream2.default.prototype.recoverWith = function (f) { return (0, _errors.flatMapError)(f, this); }; // @deprecated use recoverWith instead _Stream2.default.prototype.flatMapError = _Stream2.default.prototype.recoverWith; // ----------------------------------------------------------------------- // Multicasting exports.multicast = _multicast2.default; /** * Transform the stream into multicast stream. That means that many subscribers * to the stream will not cause multiple invocations of the internal machinery. * @returns {Stream} new stream which will multicast events to all observers. */ _Stream2.default.prototype.multicast = function () { return (0, _multicast2.default)(this); }; // export the instance of the defaultScheduler for third-party libraries exports.defaultScheduler = _defaultScheduler2.default; // export an implementation of Task used internally for third-party libraries exports.PropagateTask = _PropagateTask2.default;