Skip to content

Commit d04ed18

Browse files
refactor: return an AsyncGenerator instead (#1)
* refactor: return an `AsyncGenerator` instead * chore: update test
1 parent 8cc1f85 commit d04ed18

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

__tests__/fdir.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,12 @@ test(`do not convert \\\\ to \\`, async (t) => {
497497

498498
test("interrupted iterator should stop yielding results", async (t) => {
499499
const api = new fdir().crawl("./src");
500-
const iterator = api.withIterator()[Symbol.asyncIterator]();
501-
const results: string[] = [];
500+
const iterator = api.withIterator();
501+
const results: (string | void)[] = [];
502502
let next = await iterator.next();
503503
do {
504504
results.push(next.value);
505-
iterator.return?.();
505+
iterator.return();
506506
} while (next.done !== false);
507507
t.expect(results.length).toBe(1);
508508
});

src/api/iterator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Options, IterableOutput } from "../types";
1+
import { Options, IterableOutput, OutputIterator } from "../types";
22
import { Walker } from "./walker";
33

44
class WalkerIterator<TOutput extends IterableOutput> {
@@ -48,7 +48,7 @@ class WalkerIterator<TOutput extends IterableOutput> {
4848
}
4949
};
5050

51-
async *[Symbol.asyncIterator]() {
51+
async *start(): OutputIterator<TOutput> {
5252
this.#walker.start();
5353

5454
try {
@@ -85,6 +85,6 @@ export function iterator<TOutput extends IterableOutput>(
8585
root: string,
8686
options: Options
8787
): AsyncIterable<TOutput[number]> {
88-
const iterator = new WalkerIterator<TOutput>(root, options);
89-
return iterator;
88+
const walker = new WalkerIterator<TOutput>(root, options);
89+
return walker.start();
9090
}

src/builder/api-builder.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { callback, promise } from "../api/async";
22
import { sync } from "../api/sync";
33
import { iterator } from "../api/iterator";
4-
import { Options, Output, ResultCallback, IterableOutput } from "../types";
4+
import {
5+
Options,
6+
Output,
7+
ResultCallback,
8+
IterableOutput,
9+
OutputIterator,
10+
} from "../types";
511

612
export class APIBuilder<TReturnType extends Output> {
713
constructor(
@@ -22,7 +28,7 @@ export class APIBuilder<TReturnType extends Output> {
2228
}
2329

2430
withIterator(): TReturnType extends IterableOutput
25-
? AsyncIterable<TReturnType[number]>
31+
? OutputIterator<TReturnType>
2632
: never {
2733
// TODO (43081j): get rid of this awful `never`
2834
return iterator(this.root, this.options) as never;

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export type PathsOutput = string[];
2727

2828
export type Output = OnlyCountsOutput | PathsOutput | GroupOutput;
2929
export type IterableOutput = PathsOutput | GroupOutput;
30+
export type OutputIterator<T extends IterableOutput> = AsyncGenerator<
31+
T[number],
32+
void,
33+
void
34+
>;
3035

3136
export type FSLike = {
3237
readdir: typeof nativeFs.readdir;

0 commit comments

Comments
 (0)