Skip to content

Commit 2af5e97

Browse files
committed
fix: don't use yield* at all
1 parent 241648c commit 2af5e97

File tree

22 files changed

+88
-30
lines changed

22 files changed

+88
-30
lines changed

spec/asynciterable-operators/finalize-spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { hasNext, hasErr, noNext } from '../asynciterablehelpers.js';
22
import { range, throwError } from 'ix/asynciterable/index.js';
3-
import { finalize, tap, concatMap } from 'ix/asynciterable/operators/index.js';
3+
import { finalize, tap, flatMap } from 'ix/asynciterable/operators/index.js';
44

55
test('AsyncIterable#finalize defers behavior', async () => {
66
let done = false;
@@ -79,7 +79,7 @@ test('AsyncIterable#finalize calls with downstream error from flattening', async
7979
finalize(async () => {
8080
done = true;
8181
}),
82-
concatMap(async (x) => {
82+
flatMap(async (x) => {
8383
// srcValues.push(x);
8484
if (x === 1) {
8585
return throwError(err);

spec/asynciterable-operators/mergeall-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import { mergeAll } from 'ix/asynciterable/operators/index.js';
44

55
test('AsyncIterable#merge mergeAll behavior', async () => {
66
const res = of(of(1, 2, 3), of(4, 5)).pipe(mergeAll());
7-
expect(await toArray(res)).toEqual([1, 2, 4, 3, 5]);
7+
expect(await toArray(res)).toEqual([1, 4, 2, 5, 3]);
88
});

spec/asynciterable/concat-spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
import { take } from 'ix/asynciterable/operators.js';
12
import '../asynciterablehelpers.js';
2-
import { concat, of, sequenceEqual } from 'ix/asynciterable/index.js';
3+
import { concat, of, sequenceEqual, toArray } from 'ix/asynciterable/index.js';
34

45
test('AsyncIterable#concat behavior', async () => {
56
const res = concat(of(1, 2, 3), of(4, 5));
67
expect(await sequenceEqual(res, of(1, 2, 3, 4, 5))).toBeTruthy();
78
});
9+
10+
test("AsyncIterable#concat doesn't execute more than necessary", async () => {
11+
let i = 0;
12+
13+
async function* asyncGenerator() {
14+
i++;
15+
yield 1;
16+
}
17+
18+
const res = concat(asyncGenerator(), asyncGenerator()).pipe(take(1));
19+
const items = await toArray(res);
20+
21+
expect(items).toEqual([1]);
22+
// This second generator should not be started at all since the first one
23+
// provides enough values
24+
expect(i).toBe(1);
25+
});

src/asynciterable/catcherror.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export class CatchAllAsyncIterable<TSource> extends AsyncIterableX<TSource> {
2222
hasError = false;
2323

2424
try {
25-
yield* wrapWithAbort(source, signal);
25+
for await (const item of wrapWithAbort(source, signal)) {
26+
yield item;
27+
}
2628
} catch (e) {
2729
error = e;
2830
hasError = true;

src/asynciterable/concat.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export class ConcatAsyncIterable<TSource> extends AsyncIterableX<TSource> {
1515
throwIfAborted(signal);
1616

1717
for (const outer of this._source) {
18-
yield* wrapWithAbort(outer, signal);
18+
for await (const inner of wrapWithAbort(outer, signal)) {
19+
yield inner;
20+
}
1921
}
2022
}
2123
}

src/asynciterable/create.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class AnonymousAsyncIterable<T> extends AsyncIterableX<T> {
1414

1515
const it = await this._fn(signal);
1616

17-
yield* {
17+
for await (const item of {
1818
[Symbol.asyncIterator]: () => it,
19-
};
19+
}) {
20+
yield item;
21+
}
2022
}
2123
}
2224

src/asynciterable/defer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class DeferAsyncIterable<TSource> extends AsyncIterableX<TSource> {
1515
async *[Symbol.asyncIterator](signal?: AbortSignal) {
1616
throwIfAborted(signal);
1717

18-
yield* wrapWithAbort(await this._fn(signal), signal);
18+
for await (const item of wrapWithAbort(await this._fn(signal), signal)) {
19+
yield item;
20+
}
1921
}
2022
}
2123

src/asynciterable/fromdomstream.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ async function* _consumeReader<T>(
4141
): AsyncIterator<T, any, undefined> {
4242
let threw = false;
4343
try {
44-
yield* iterator;
44+
for await (const item of iterator) {
45+
yield item;
46+
}
4547
} catch (e) {
4648
if ((threw = true) && reader) {
4749
await reader['cancel'](e);

src/asynciterable/of.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export class OfAsyncIterable<TSource> extends AsyncIterableX<TSource> {
1313
async *[Symbol.asyncIterator](signal?: AbortSignal) {
1414
throwIfAborted(signal);
1515

16-
yield* this._args;
16+
for await (const item of this._args) {
17+
yield item;
18+
}
1719
}
1820
}
1921

src/asynciterable/onerrorresumenext.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ export class OnErrorResumeNextAsyncIterable<TSource> extends AsyncIterableX<TSou
1414
async *[Symbol.asyncIterator](signal?: AbortSignal) {
1515
throwIfAborted(signal);
1616

17-
for (const item of this._source) {
17+
for (const outer of this._source) {
1818
try {
19-
yield* wrapWithAbort(item, signal);
19+
for await (const inner of wrapWithAbort(outer, signal)) {
20+
yield inner;
21+
}
2022
} catch {
2123
// ignore
2224
}

0 commit comments

Comments
 (0)