1 | import { AsyncIterableX } from './asynciterablex';
|
2 | export class ScanAsyncIterable extends AsyncIterableX {
|
3 | constructor(source, fn, seed) {
|
4 | super();
|
5 | this._source = source;
|
6 | this._fn = fn;
|
7 | this._hasSeed = seed.length === 1;
|
8 | this._seed = seed[0];
|
9 | }
|
10 | async *[Symbol.asyncIterator]() {
|
11 | let i = 0, hasValue = false, acc = this._seed;
|
12 | for await (let item of this._source) {
|
13 | if (hasValue || (hasValue = this._hasSeed)) {
|
14 | acc = await this._fn(acc, item, i++);
|
15 | yield acc;
|
16 | }
|
17 | else {
|
18 | acc = item;
|
19 | hasValue = true;
|
20 | i++;
|
21 | }
|
22 | }
|
23 | if (i === 1 && !this._hasSeed) {
|
24 | yield acc;
|
25 | }
|
26 | }
|
27 | }
|
28 | export function scan(source, accumulator, ...seed) {
|
29 | return new ScanAsyncIterable(source, accumulator, seed);
|
30 | }
|
31 |
|
32 |
|