Skip to content

Commit 6c7b826

Browse files
authored
feat(worker): simplify input passing (#664)
1 parent 3c16b4c commit 6c7b826

File tree

10 files changed

+16
-25
lines changed

10 files changed

+16
-25
lines changed

docs/generators.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export async function* generate(input, worker) {
241241
};
242242

243243
// Stream chunks as they complete
244-
for await (const chunkResult of worker.stream(input, input, deps)) {
244+
for await (const chunkResult of worker.stream(input, deps)) {
245245
// Process chunk result if needed
246246
yield chunkResult;
247247
}
@@ -316,7 +316,7 @@ export async function processChunk(fullInput, itemIndices, deps) {
316316
*/
317317
export async function* generate(input, worker) {
318318
// Stream results as workers complete chunks
319-
for await (const chunkResult of worker.stream(input, input, {})) {
319+
for await (const chunkResult of worker.stream(input, {})) {
320320
// Yield immediately - downstream can start processing
321321
yield chunkResult;
322322
}

src/generators/ast-js/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function* generate(_, worker) {
5050

5151
// Parse the Javascript sources into ASTs in parallel using worker threads
5252
// source is both the items list and the fullInput since we use sliceInput
53-
for await (const chunkResult of worker.stream(files, files)) {
53+
for await (const chunkResult of worker.stream(files)) {
5454
yield chunkResult;
5555
}
5656
}

src/generators/ast/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function* generate(_, worker) {
5252
);
5353

5454
// Parse markdown files in parallel using worker threads
55-
for await (const chunkResult of worker.stream(files, files)) {
55+
for await (const chunkResult of worker.stream(files)) {
5656
yield chunkResult;
5757
}
5858
}

src/generators/jsx-ast/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function* generate(input, worker) {
6161
entries: groupedModules.get(head.api),
6262
}));
6363

64-
for await (const chunkResult of worker.stream(entries, entries, docPages)) {
64+
for await (const chunkResult of worker.stream(entries, docPages)) {
6565
yield chunkResult;
6666
}
6767
}

src/generators/legacy-html/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export async function* generate(input, worker) {
123123
}));
124124

125125
// Stream chunks as they complete - HTML files are written immediately
126-
for await (const chunkResult of worker.stream(entries, entries, navigation)) {
126+
for await (const chunkResult of worker.stream(entries, navigation)) {
127127
// Write files for this chunk in the generate method (main thread)
128128
if (config.output) {
129129
for (const template of chunkResult) {

src/generators/legacy-json/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function* generate(input, worker) {
4949
nodes: groupedModules.get(head.api),
5050
}));
5151

52-
for await (const chunkResult of worker.stream(entries, entries)) {
52+
for await (const chunkResult of worker.stream(entries)) {
5353
if (config.output) {
5454
for (const section of chunkResult) {
5555
const out = join(config.output, `${section.api}.json`);

src/generators/metadata/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function* generate(inputs, worker) {
3232

3333
// Stream chunks as they complete - allows dependent generators
3434
// to start collecting/preparing while we're still processing
35-
for await (const chunkResult of worker.stream(inputs, inputs, typeMap)) {
35+
for await (const chunkResult of worker.stream(inputs, typeMap)) {
3636
yield chunkResult.flat();
3737
}
3838
}

src/generators/types.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ declare global {
1818
* while upstream chunks are still being processed.
1919
*
2020
* @param items - Items to process (determines chunk distribution)
21-
* @param fullInput - Full input data for context rebuilding in workers
2221
* @param opts - Additional options to pass to workers
2322
* @yields Each chunk's results as they complete
2423
*/
2524
stream<T, R>(
2625
items: T[],
27-
fullInput: T[],
2826
opts?: Record<string, unknown>
2927
): AsyncGenerator<R[], void, unknown>;
3028
}

src/threading/__tests__/parallel.test.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('createParallelWorker', () => {
9090
];
9191

9292
const chunks = await collectChunks(
93-
worker.stream(mockInput, mockInput, { typeMap: {} })
93+
worker.stream(mockInput, { typeMap: {} })
9494
);
9595

9696
strictEqual(chunks.length, 4);
@@ -121,7 +121,7 @@ describe('createParallelWorker', () => {
121121
];
122122

123123
const chunks = await collectChunks(
124-
worker.stream(mockInput, mockInput, { typeMap: {} })
124+
worker.stream(mockInput, { typeMap: {} })
125125
);
126126

127127
strictEqual(chunks.length, 2);
@@ -144,7 +144,7 @@ describe('createParallelWorker', () => {
144144
];
145145

146146
const chunks = await collectChunks(
147-
worker.stream(mockInput, mockInput, { typeMap: {} })
147+
worker.stream(mockInput, { typeMap: {} })
148148
);
149149

150150
strictEqual(chunks.length, 1);
@@ -172,7 +172,7 @@ describe('createParallelWorker', () => {
172172
];
173173

174174
const chunks = await collectChunks(
175-
worker.stream(mockInput, mockInput, { typeMap: {} })
175+
worker.stream(mockInput, { typeMap: {} })
176176
);
177177

178178
strictEqual(chunks.length, 2);

src/threading/parallel.mjs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,12 @@ export default function createParallelWorker(
7676
/**
7777
* Processes items in parallel, yielding results as chunks complete.
7878
*
79-
* @template T, R
79+
* @template T
8080
* @param {T[]} items - Items to process
81-
* @param {T[]} fullInput - Full input for context
8281
* @param {object} extra - Extra options
8382
* @yields {R[]} Chunk results as they complete
8483
*/
85-
async *stream(items, fullInput, extra) {
84+
async *stream(items, extra) {
8685
if (items.length === 0) {
8786
return;
8887
}
@@ -101,21 +100,15 @@ export default function createParallelWorker(
101100
chunks.map(indices => {
102101
if (runInOneGo) {
103102
const promise = generator
104-
.processChunk(fullInput, indices, extra)
103+
.processChunk(items, indices, extra)
105104
.then(result => ({ promise, result }));
106105

107106
return promise;
108107
}
109108

110109
const promise = pool
111110
.run(
112-
createTask(
113-
fullInput,
114-
indices,
115-
extra,
116-
configuration,
117-
generatorName
118-
)
111+
createTask(items, indices, extra, configuration, generatorName)
119112
)
120113
.then(result => ({ promise, result }));
121114

0 commit comments

Comments
 (0)