UNPKG

1.96 kBJavaScriptView Raw
1import { as as asAsyncIterable } from './as';
2import { _initialize as _initializeFrom } from './from';
3import { bindCallback } from '../util/bindcallback';
4import { isReadableNodeStream, isWritableNodeStream } from '../util/isiterable';
5/**
6 * This class serves as the base for all operations which support [Symbol.asyncIterator].
7 */
8export class AsyncIterableX {
9 async forEach(projection, thisArg) {
10 const fn = bindCallback(projection, thisArg, 2);
11 let i = 0;
12 for await (const item of this) {
13 await fn(item, i++);
14 }
15 }
16 pipe(...args) {
17 let i = -1;
18 const n = args.length;
19 let acc = this;
20 while (++i < n) {
21 acc = args[i](asAsyncIterable(acc));
22 }
23 return acc;
24 }
25}
26_initializeFrom(AsyncIterableX);
27try {
28 (isBrowser => {
29 if (isBrowser) {
30 return;
31 }
32 AsyncIterableX.prototype['pipe'] = nodePipe;
33 const readableOpts = (x, opts = x._writableState || { objectMode: true }) => opts;
34 function nodePipe(...args) {
35 let i = -1;
36 let end;
37 const n = args.length;
38 let prev = this;
39 let next;
40 while (++i < n) {
41 next = args[i];
42 if (typeof next === 'function') {
43 prev = next(asAsyncIterable(prev));
44 }
45 else if (isWritableNodeStream(next)) {
46 ({ end = true } = args[i + 1] || {});
47 // prettier-ignore
48 return isReadableNodeStream(prev) ? prev.pipe(next, { end }) :
49 asAsyncIterable(prev).toNodeStream(readableOpts(next)).pipe(next, { end });
50 }
51 }
52 return prev;
53 }
54 })(typeof window === 'object' && typeof document === 'object' && document.nodeType === 9);
55}
56catch (e) {
57 /* */
58}
59
60//# sourceMappingURL=asynciterablex.mjs.map