1 | import { as as asAsyncIterable } from './as';
|
2 | import { _initialize as _initializeFrom } from './from';
|
3 | import { isReadableNodeStream, isWritableNodeStream } from '../util/isiterable';
|
4 | class WithAbortAsyncIterable {
|
5 | constructor(source, signal) {
|
6 | this._source = source;
|
7 | this._signal = signal;
|
8 | }
|
9 | [Symbol.asyncIterator]() {
|
10 |
|
11 | return this._source[Symbol.asyncIterator](this._signal);
|
12 | }
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 | export class AsyncIterableX {
|
18 | async forEach(projection, thisArg, signal) {
|
19 | const source = signal ? new WithAbortAsyncIterable(this, signal) : this;
|
20 | let i = 0;
|
21 | for await (const item of source) {
|
22 | await projection.call(thisArg, item, i++, signal);
|
23 | }
|
24 | }
|
25 | pipe(...args) {
|
26 | let i = -1;
|
27 | const n = args.length;
|
28 | let acc = this;
|
29 | while (++i < n) {
|
30 | acc = args[i](asAsyncIterable(acc));
|
31 | }
|
32 | return acc;
|
33 | }
|
34 | }
|
35 | AsyncIterableX.prototype[Symbol.toStringTag] = 'AsyncIterableX';
|
36 | Object.defineProperty(AsyncIterableX, Symbol.hasInstance, {
|
37 | writable: true,
|
38 | configurable: true,
|
39 | value(inst) {
|
40 | return !!(inst && inst[Symbol.toStringTag] === 'AsyncIterableX');
|
41 | },
|
42 | });
|
43 | _initializeFrom(AsyncIterableX);
|
44 | try {
|
45 | ((isBrowser) => {
|
46 | if (isBrowser) {
|
47 | return;
|
48 | }
|
49 | AsyncIterableX.prototype['pipe'] = nodePipe;
|
50 | const readableOpts = (x, opts = x._writableState || { objectMode: true }) => opts;
|
51 | function nodePipe(...args) {
|
52 | let i = -1;
|
53 | let end;
|
54 | const n = args.length;
|
55 | let prev = this;
|
56 | let next;
|
57 | while (++i < n) {
|
58 | next = args[i];
|
59 | if (typeof next === 'function') {
|
60 | prev = next(asAsyncIterable(prev));
|
61 | }
|
62 | else if (isWritableNodeStream(next)) {
|
63 | ({ end = true } = args[i + 1] || {});
|
64 |
|
65 | return isReadableNodeStream(prev) ? prev.pipe(next, { end }) :
|
66 | asAsyncIterable(prev).toNodeStream(readableOpts(next)).pipe(next, { end });
|
67 | }
|
68 | }
|
69 | return prev;
|
70 | }
|
71 | })(typeof window === 'object' && typeof document === 'object' && document.nodeType === 9);
|
72 | }
|
73 | catch (e) {
|
74 |
|
75 | }
|
76 |
|
77 |
|