-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathasynciterablex.ts
More file actions
485 lines (436 loc) · 14.5 KB
/
asynciterablex.ts
File metadata and controls
485 lines (436 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import { OperatorAsyncFunction, UnaryFunction } from '../interfaces.js';
import { Observable } from '../observer.js';
import { bindCallback } from '../util/bindcallback.js';
import { identityAsync } from '../util/identity.js';
import {
isReadableNodeStream,
isWritableNodeStream,
isIterable,
isAsyncIterable,
isArrayLike,
isIterator,
isPromise,
isObservable,
} from '../util/isiterable.js';
import { toLength } from '../util/tolength.js';
import { AbortError, throwIfAborted } from '../aborterror.js';
/**
* This class serves as the base for all operations which support [Symbol.asyncIterator].
*/
export abstract class AsyncIterableX<T> implements AsyncIterable<T> {
abstract [Symbol.asyncIterator](signal?: AbortSignal): AsyncIterator<T>;
/** @nocollapse */
async forEach(
projection: (value: T, index: number, signal?: AbortSignal) => void | Promise<void>,
thisArg?: any,
signal?: AbortSignal
): Promise<void> {
const source = signal ? new WithAbortAsyncIterable(this, signal) : this;
let i = 0;
for await (const item of source) {
await projection.call(thisArg, item, i++, signal);
}
}
/** @nocollapse */
pipe<R>(...operations: UnaryFunction<AsyncIterable<T>, R>[]): R;
pipe<R>(...operations: OperatorAsyncFunction<T, R>[]): AsyncIterableX<R>;
pipe<R extends NodeJS.WritableStream>(writable: R, options?: { end?: boolean }): R;
pipe<R>(...args: any[]) {
let i = -1;
const n = args.length;
let acc: any = this;
while (++i < n) {
acc = args[i](AsyncIterableX.as(acc));
}
return acc;
}
/** @nocollapse */
static from<TSource, TResult = TSource>(
source: AsyncIterableInput<TSource>,
selector: (value: TSource, index: number) => TResult | Promise<TResult> = identityAsync,
thisArg?: any
): AsyncIterableX<TResult> {
const fn = bindCallback(selector, thisArg, 2);
if (isIterable(source) || isAsyncIterable(source)) {
return new FromAsyncIterable<TSource, TResult>(source, fn);
}
if (isPromise(source)) {
return new FromPromiseIterable<TSource, TResult>(source, fn);
}
if (isObservable(source)) {
return new FromObservableAsyncIterable<TSource, TResult>(source, fn);
}
if (isArrayLike(source)) {
return new FromArrayIterable<TSource, TResult>(source, fn);
}
if (isIterator(source)) {
return new FromAsyncIterable<TSource, TResult>({ [Symbol.asyncIterator]: () => source }, fn);
}
throw new TypeError('Input type not supported');
}
/**
* Converts an existing string into an async-iterable of characters.
*
* @param {string} source The string to convert to an async-iterable.
* @returns {AsyncIterableX<string>} An async-iterable stream of characters from the source.
*/
static as(source: string): AsyncIterableX<string>;
/**
* Converts the AsyncIterable-like input or single element into an AsyncIterable.
*
* @template T The type of elements in the async-iterable like sequence.
* @param {AsyncIterableInput<T>} source The async-iterable like input to convert to an async-iterable.
* @returns {AsyncIterableX<T>} An async-iterable stream from elements in the async-iterable like sequence.
*/
static as<T>(source: AsyncIterableInput<T> | T): AsyncIterableX<T>;
/**
* Converts the single element into an async-iterable sequence.
*
* @template T The type of the input to turn into an async-iterable sequence.
* @param {T} source The single element to turn into an async-iterable sequence.
* @returns {AsyncIterableX<T>} An async-iterable sequence which contains the single element.
*/
static as<T>(source: T): AsyncIterableX<T>;
/**
* Converts the input into an async-iterable sequence.
*
* @param {*} source The source to convert to an async-iterable sequence.
* @returns {AsyncIterableX<*>} An async-iterable containing the input.
*/
/** @nocollapse */
static as(source: any): AsyncIterableX<any> {
if (source instanceof AsyncIterableX) {
return source;
}
if (typeof source === 'string') {
return new FromArrayIterable([source], identityAsync);
}
if (isIterable(source) || isAsyncIterable(source)) {
return new FromAsyncIterable(source, identityAsync);
}
if (isPromise(source)) {
return new FromPromiseIterable(source, identityAsync);
}
if (isObservable(source)) {
return new FromObservableAsyncIterable(source, identityAsync);
}
if (isArrayLike(source)) {
return new FromArrayIterable(source, identityAsync);
}
return new FromArrayIterable([source], identityAsync);
}
}
(<any>AsyncIterableX.prototype)[Symbol.toStringTag] = 'AsyncIterableX';
Object.defineProperty(AsyncIterableX, Symbol.hasInstance, {
writable: true,
configurable: true,
value(inst: any) {
return !!(inst && inst[Symbol.toStringTag] === 'AsyncIterableX');
},
});
const ARRAY_VALUE = 'value';
const ARRAY_ERROR = 'error';
interface AsyncSinkItem<T> {
type: string;
value?: T;
error?: any;
}
interface AsyncResolver<T> {
resolve: (value: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
}
/** @ignore */
/** @ignore */
export class AsyncSink<TSource> implements AsyncIterableIterator<TSource> {
private _ended: boolean;
private _values: AsyncSinkItem<TSource>[];
private _resolvers: AsyncResolver<IteratorResult<TSource>>[];
constructor() {
this._ended = false;
this._values = [];
this._resolvers = [];
}
[Symbol.asyncIterator]() {
return this;
}
write(value: TSource) {
this._push({ type: ARRAY_VALUE, value });
}
error(error: any) {
this._push({ type: ARRAY_ERROR, error });
}
private _push(item: AsyncSinkItem<TSource>) {
if (this._ended) {
throw new Error('AsyncSink already ended');
}
if (this._resolvers.length > 0) {
const { resolve, reject } = this._resolvers.shift()!;
if (item.type === ARRAY_ERROR) {
reject(item.error!);
} else {
resolve({ done: false, value: item.value! });
}
} else {
this._values.push(item);
}
}
next() {
if (this._values.length > 0) {
const { type, value, error } = this._values.shift()!;
if (type === ARRAY_ERROR) {
return Promise.reject(error);
} else {
return Promise.resolve({ done: false, value } as IteratorResult<TSource>);
}
}
if (this._ended) {
return Promise.resolve({ done: true } as IteratorResult<TSource>);
}
return new Promise<IteratorResult<TSource>>((resolve, reject) => {
this._resolvers.push({ resolve, reject });
});
}
end() {
while (this._resolvers.length > 0) {
this._resolvers.shift()!.resolve({ done: true } as IteratorResult<TSource>);
}
this._ended = true;
}
}
/** @ignore */
export class FromArrayIterable<TSource, TResult = TSource> extends AsyncIterableX<TResult> {
private _source: ArrayLike<TSource>;
private _selector: (value: TSource, index: number) => TResult | Promise<TResult>;
constructor(
source: ArrayLike<TSource>,
selector: (value: TSource, index: number) => TResult | Promise<TResult>
) {
super();
this._source = source;
this._selector = selector;
}
async *[Symbol.asyncIterator]() {
let i = 0;
const length = toLength((<ArrayLike<TSource>>this._source).length);
while (i < length) {
yield await this._selector(this._source[i], i++);
}
}
}
/** @ignore */
export class FromAsyncIterable<TSource, TResult = TSource> extends AsyncIterableX<TResult> {
private _source: Iterable<TSource | PromiseLike<TSource>> | AsyncIterable<TSource>;
private _selector: (value: TSource, index: number) => TResult | Promise<TResult>;
constructor(
source: Iterable<TSource | PromiseLike<TSource>> | AsyncIterable<TSource>,
selector: (value: TSource, index: number) => TResult | Promise<TResult>
) {
super();
this._source = source;
this._selector = selector;
}
async *[Symbol.asyncIterator](signal?: AbortSignal) {
let i = 0;
if (signal && this._source instanceof AsyncIterableX) {
for await (const item of new WithAbortAsyncIterable(this._source, signal)) {
yield await this._selector(item, i++);
}
} else {
throwIfAborted(signal);
for await (const item of this._source) {
throwIfAborted(signal);
const value = await this._selector(item, i++);
throwIfAborted(signal);
yield value;
}
}
}
}
/** @ignore */
export class FromPromiseIterable<TSource, TResult = TSource> extends AsyncIterableX<TResult> {
private _source: PromiseLike<TSource>;
private _selector: (value: TSource, index: number) => TResult | Promise<TResult>;
constructor(
source: PromiseLike<TSource>,
selector: (value: TSource, index: number) => TResult | Promise<TResult>
) {
super();
this._source = source;
this._selector = selector;
}
async *[Symbol.asyncIterator]() {
yield await this._selector(await this._source, 0);
}
}
/** @ignore */
export class FromObservableAsyncIterable<TSource, TResult = TSource> extends AsyncIterableX<
TResult
> {
private _observable: Observable<TSource>;
private _selector: (value: TSource, index: number) => TResult | Promise<TResult>;
constructor(
observable: Observable<TSource>,
selector: (value: TSource, index: number) => TResult | Promise<TResult>
) {
super();
this._observable = observable;
this._selector = selector;
}
async *[Symbol.asyncIterator](signal?: AbortSignal) {
throwIfAborted(signal);
const sink: AsyncSink<TSource> = new AsyncSink<TSource>();
const subscription = this._observable.subscribe({
next(value: TSource) {
sink.write(value);
},
error(err: any) {
sink.error(err);
},
complete() {
sink.end();
},
});
function onAbort() {
sink.error(new AbortError());
}
if (signal) {
signal.addEventListener('abort', onAbort);
}
let i = 0;
try {
for (let next; !(next = await sink.next()).done; ) {
throwIfAborted(signal);
yield await this._selector(next.value!, i++);
}
} finally {
if (signal) {
signal.removeEventListener('abort', onAbort);
}
subscription.unsubscribe();
}
}
}
class WithAbortAsyncIterable<TSource> implements AsyncIterable<TSource> {
private _source: AsyncIterable<TSource>;
private _signal: AbortSignal;
constructor(source: AsyncIterable<TSource>, signal: AbortSignal) {
this._source = source;
this._signal = signal;
}
[Symbol.asyncIterator](): AsyncIterator<TSource> {
// @ts-ignore
return this._source[Symbol.asyncIterator](this._signal);
}
}
/** @ignore */
export type AsyncIterableInput<TSource> =
| AsyncIterable<TSource>
| AsyncIterator<TSource>
| Iterable<TSource | PromiseLike<TSource>>
| ArrayLike<TSource>
| PromiseLike<TSource>
| Observable<TSource>;
type WritableOrOperatorAsyncFunction<T, R> =
| NodeJS.WritableStream
| NodeJS.ReadWriteStream
| OperatorAsyncFunction<T, R>;
declare module '../asynciterable/asynciterablex' {
interface AsyncIterableX<T> {
pipe(): AsyncIterableX<T>;
pipe<A>(op1: OperatorAsyncFunction<T, A>): AsyncIterableX<A>;
pipe<A, B>(
op1: OperatorAsyncFunction<T, A>,
op2: OperatorAsyncFunction<A, B>
): AsyncIterableX<B>;
pipe<A, B, C>(
op1: OperatorAsyncFunction<T, A>,
op2: OperatorAsyncFunction<A, B>,
op3: OperatorAsyncFunction<B, C>
): AsyncIterableX<C>;
pipe<A, B, C, D>(
op1: OperatorAsyncFunction<T, A>,
op2: OperatorAsyncFunction<A, B>,
op3: OperatorAsyncFunction<B, C>,
op4: OperatorAsyncFunction<C, D>
): AsyncIterableX<D>;
pipe<A, B, C, D, E>(
op1: OperatorAsyncFunction<T, A>,
op2: OperatorAsyncFunction<A, B>,
op3: OperatorAsyncFunction<B, C>,
op4: OperatorAsyncFunction<C, D>,
op5: OperatorAsyncFunction<D, E>
): AsyncIterableX<E>;
pipe<A, B, C, D, E, F>(
op1: OperatorAsyncFunction<T, A>,
op2: OperatorAsyncFunction<A, B>,
op3: OperatorAsyncFunction<B, C>,
op4: OperatorAsyncFunction<C, D>,
op5: OperatorAsyncFunction<D, E>,
op6: OperatorAsyncFunction<E, F>
): AsyncIterableX<F>;
pipe<A, B, C, D, E, F, G>(
op1: OperatorAsyncFunction<T, A>,
op2: OperatorAsyncFunction<A, B>,
op3: OperatorAsyncFunction<B, C>,
op4: OperatorAsyncFunction<C, D>,
op5: OperatorAsyncFunction<D, E>,
op6: OperatorAsyncFunction<E, F>,
op7: OperatorAsyncFunction<F, G>
): AsyncIterableX<G>;
pipe<A, B, C, D, E, F, G, H>(
op1: OperatorAsyncFunction<T, A>,
op2: OperatorAsyncFunction<A, B>,
op3: OperatorAsyncFunction<B, C>,
op4: OperatorAsyncFunction<C, D>,
op5: OperatorAsyncFunction<D, E>,
op6: OperatorAsyncFunction<E, F>,
op7: OperatorAsyncFunction<F, G>,
op8: OperatorAsyncFunction<G, H>
): AsyncIterableX<H>;
pipe<A, B, C, D, E, F, G, H, I>(
op1: OperatorAsyncFunction<T, A>,
op2: OperatorAsyncFunction<A, B>,
op3: OperatorAsyncFunction<B, C>,
op4: OperatorAsyncFunction<C, D>,
op5: OperatorAsyncFunction<D, E>,
op6: OperatorAsyncFunction<E, F>,
op7: OperatorAsyncFunction<F, G>,
op8: OperatorAsyncFunction<G, H>,
op9: OperatorAsyncFunction<H, I>
): AsyncIterableX<I>;
pipe(...operations: OperatorAsyncFunction<any, any>[]): AsyncIterableX<any>;
pipe<A extends NodeJS.WritableStream>(op1: A, options?: { end?: boolean }): A;
}
}
try {
((isBrowser) => {
if (isBrowser) {
return;
}
AsyncIterableX.prototype['pipe'] = nodePipe;
const readableOpts = (x: any, opts = x._writableState || { objectMode: true }) => opts;
function nodePipe<T>(this: AsyncIterableX<T>, ...args: any[]) {
let i = -1;
let end: boolean;
const n = args.length;
let prev: any = this;
let next: WritableOrOperatorAsyncFunction<T, any>;
while (++i < n) {
next = args[i];
if (typeof next === 'function') {
prev = next(AsyncIterableX.as(prev));
} else if (isWritableNodeStream(next)) {
({ end = true } = args[i + 1] || {});
// prettier-ignore
return isReadableNodeStream(prev) ? prev.pipe(next, { end }) :
AsyncIterableX.as(prev).toNodeStream(readableOpts(next)).pipe(next, { end });
}
}
return prev;
}
})(typeof window === 'object' && typeof document === 'object' && document.nodeType === 9);
} catch (e) {
/* */
}
export const as = AsyncIterableX.as;
export const from = AsyncIterableX.from;