UNPKG

xstream

Version:

An extremely intuitive, small, and fast functional reactive stream library for JavaScript

96 lines 2.6 kB
"use strict"; var core_1 = require('../core'); var ConcatProducer = (function () { function ConcatProducer(streams) { this.streams = streams; this.type = 'concat'; this.out = null; this.i = 0; } ConcatProducer.prototype._start = function (out) { this.out = out; this.streams[this.i]._add(this); }; ConcatProducer.prototype._stop = function () { var streams = this.streams; if (this.i < streams.length) { streams[this.i]._remove(this); } this.i = 0; this.out = null; }; ConcatProducer.prototype._n = function (t) { var u = this.out; if (!u) return; u._n(t); }; ConcatProducer.prototype._e = function (err) { var u = this.out; if (!u) return; u._e(err); }; ConcatProducer.prototype._c = function () { var u = this.out; if (!u) return; var streams = this.streams; streams[this.i]._remove(this); if (++this.i < streams.length) { streams[this.i]._add(this); } else { u._c(); } }; return ConcatProducer; }()); /** * Puts one stream after the other. *concat* is a factory that takes multiple * streams as arguments, and starts the `n+1`-th stream only when the `n`-th * stream has completed. It concatenates those streams together. * * Marble diagram: * * ```text * --1--2---3---4-| * ...............--a-b-c--d-| * concat * --1--2---3---4---a-b-c--d-| * ``` * * Example: * * ```js * import concat from 'xstream/extra/concat' * * const streamA = xs.of('a', 'b', 'c') * const streamB = xs.of(10, 20, 30) * const streamC = xs.of('X', 'Y', 'Z') * * const outputStream = concat(streamA, streamB, streamC) * * outputStream.addListener({ * next: (x) => console.log(x), * error: (err) => console.error(err), * complete: () => console.log('concat completed'), * }) * ``` * * @factory true * @param {Stream} stream1 A stream to concatenate together with other streams. * @param {Stream} stream2 A stream to concatenate together with other streams. Two * or more streams may be given as arguments. * @return {Stream} */ function concat() { var streams = []; for (var _i = 0; _i < arguments.length; _i++) { streams[_i - 0] = arguments[_i]; } return new core_1.Stream(new ConcatProducer(streams)); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = concat; //# sourceMappingURL=concat.js.map