UNPKG

2.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/*-----------------------------------------------------------------------------
4| Copyright (c) 2014-2017, PhosphorJS Contributors
5|
6| Distributed under the terms of the BSD 3-Clause License.
7|
8| The full license is in the file LICENSE, distributed with this software.
9|----------------------------------------------------------------------------*/
10var iter_1 = require("./iter");
11/**
12 * Chain together several iterables.
13 *
14 * @param objects - The iterable or array-like objects of interest.
15 *
16 * @returns An iterator which yields the values of the iterables
17 * in the order in which they are supplied.
18 *
19 * #### Example
20 * ```typescript
21 * import { chain, toArray } from '@phosphor/algorithm';
22 *
23 * let data1 = [1, 2, 3];
24 * let data2 = [4, 5, 6];
25 *
26 * let stream = chain(data1, data2);
27 *
28 * toArray(stream); // [1, 2, 3, 4, 5, 6]
29 * ```
30 */
31function chain() {
32 var objects = [];
33 for (var _i = 0; _i < arguments.length; _i++) {
34 objects[_i] = arguments[_i];
35 }
36 return new ChainIterator(iter_1.iter(objects.map(iter_1.iter)));
37}
38exports.chain = chain;
39/**
40 * An iterator which chains together several iterators.
41 */
42var ChainIterator = /** @class */ (function () {
43 /**
44 * Construct a new chain iterator.
45 *
46 * @param source - The iterator of iterators of interest.
47 */
48 function ChainIterator(source) {
49 this._cloned = false;
50 this._source = source;
51 this._active = undefined;
52 }
53 /**
54 * Get an iterator over the object's values.
55 *
56 * @returns An iterator which yields the object's values.
57 */
58 ChainIterator.prototype.iter = function () {
59 return this;
60 };
61 /**
62 * Create an independent clone of the iterator.
63 *
64 * @returns A new independent clone of the iterator.
65 */
66 ChainIterator.prototype.clone = function () {
67 var result = new ChainIterator(this._source.clone());
68 result._active = this._active && this._active.clone();
69 result._cloned = true;
70 this._cloned = true;
71 return result;
72 };
73 /**
74 * Get the next value from the iterator.
75 *
76 * @returns The next value from the iterator, or `undefined`.
77 */
78 ChainIterator.prototype.next = function () {
79 if (this._active === undefined) {
80 var active = this._source.next();
81 if (active === undefined) {
82 return undefined;
83 }
84 this._active = this._cloned ? active.clone() : active;
85 }
86 var value = this._active.next();
87 if (value !== undefined) {
88 return value;
89 }
90 this._active = undefined;
91 return this.next();
92 };
93 return ChainIterator;
94}());
95exports.ChainIterator = ChainIterator;