Skip to content

Commit 32f9772

Browse files
authored
refactor: move results into the configuration, as discardResults
From the review, which asked for it in the configuration. runGenerators reads discardResults from it, next to threads and chunkSize, and the CLI's generate and preview commands set it. The configuration docs list it. The failure test of a dependency read as a stream runs with and without results, so the drain path also checks that no rejection goes unhandled, and the test of discarding results keeps only what it checks of its own. The cache's comments say a requested target is read once more when the run ends, rather than by the final collection, and that a drained result's chunks go as they're produced. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ee779b6 commit 32f9772

8 files changed

Lines changed: 37 additions & 40 deletions

File tree

‎.changeset/drain-results.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
'@doc-kit/cli': patch
44
---
55

6-
`runGenerators(configuration, { results: false })` keeps none of the requested generators' output: each still runs to its end, and once nothing else reads its output, its chunks go as they are recorded. The CLI's `generate` and `preview` commands use it, as they don't read the results.
6+
`runGenerators` with `discardResults: true` in its configuration keeps none of the requested generators' output: each still runs to its end, and once nothing else reads its output, its chunks go as they are produced. The CLI's `generate` and `preview` commands use it, as they don't read the results.

‎docs/configuration.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ Top-level, alongside `target` and `global`:
137137
CPU count. Each worker's heap is limited to 512MB; to raise the limit, run
138138
Node.js with `--max-old-space-size` (directly or through `NODE_OPTIONS`).
139139
- `chunkSize` {number} Items processed per worker thread. **Default:** `10`.
140+
- `discardResults` {boolean} Keeps none of the requested generators' results,
141+
so each can be let go as soon as it is produced. `doc-kit generate` and
142+
`doc-kit preview` set it. **Default:** `false`.
140143

141144
## Generator options
142145

‎packages/cli/bin/commands/generate.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ export default generate.action(
6464
const config = await setConfig(opts);
6565
assertRunnableOptions(config);
6666

67-
await runGenerators(config, { results: false });
67+
await runGenerators({ ...config, discardResults: true });
6868
})
6969
);

‎packages/cli/bin/commands/preview.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default preview.action(
6262
const config = await setConfig(opts);
6363
assertRunnableOptions(config);
6464

65-
await runGenerators(config, { results: false });
65+
await runGenerators({ ...config, discardResults: true });
6666

6767
// With the default coming from the configuration, this is always set
6868
output = config.global.output;

‎packages/core/src/__tests__/generators.test.mjs‎

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,23 @@ describe('createGenerator orchestration', () => {
312312
}
313313
);
314314

315-
it('rejects with the failure of a dependency read as a stream', async () => {
316-
const { runGenerators } = createGenerator();
315+
it('rejects with the failure of a dependency read as a stream, with or without results', async () => {
317316
const unhandled = [];
318317
const onUnhandled = error => unhandled.push(error);
319318

320319
process.on('unhandledRejection', onUnhandled);
321320

322321
try {
323-
await assert.rejects(
324-
runGenerators({ target: ['failing-reader'], threads: 2 }),
325-
/source failed/
326-
);
322+
for (const discardResults of [false, true]) {
323+
await assert.rejects(
324+
createGenerator().runGenerators({
325+
target: ['failing-reader'],
326+
threads: 2,
327+
discardResults,
328+
}),
329+
/source failed/
330+
);
331+
}
327332

328333
// A rejection left unhandled is reported once the microtasks have run
329334
await new Promise(resolve => setImmediate(resolve));
@@ -348,26 +353,13 @@ describe('createGenerator orchestration', () => {
348353
assert.deepStrictEqual(two, [{ array: false }]);
349354
});
350355

351-
it('runs every generator but keeps no results with results: false', async () => {
352-
const { runGenerators } = createGenerator();
353-
354-
const results = await runGenerators(
355-
{ target: ['gen-c', 'gen-c-all'], threads: 1 },
356-
{ results: false }
357-
);
356+
it('returns no results with discardResults', async () => {
357+
const results = await createGenerator().runGenerators({
358+
target: ['gen-c', 'gen-c-all'],
359+
threads: 1,
360+
discardResults: true,
361+
});
358362

359363
assert.equal(results, undefined);
360-
assert.equal(runs['gen-c'], 1);
361-
assert.equal(runs['gen-c-all'], 1);
362-
});
363-
364-
it('rejects with a failure with results: false', async () => {
365-
await assert.rejects(
366-
createGenerator().runGenerators(
367-
{ target: ['failing-reader'], threads: 2 },
368-
{ results: false }
369-
),
370-
/source failed/
371-
);
372364
});
373365
});

‎packages/core/src/caching.mjs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ export const createCache = () => {
141141
/**
142142
* Computes and records consumer counts across the dependency closure of the
143143
* requested generators. A generator is consumed once per dependent
144-
* generator, plus once more when it is a requested target (read by the
145-
* final collection). This drives eviction without coupling the cache to the
144+
* generator, plus once more when it is a requested target (read when the run
145+
* ends). This drives eviction without coupling the cache to the
146146
* generator registry: callers supply how to resolve a dependency.
147147
*
148148
* @param {string[]} targets - Requested generator cache keys
@@ -179,7 +179,7 @@ export const createCache = () => {
179179
}
180180
}
181181

182-
// Each requested target is consumed once more by the final collection
182+
// Each requested target is read once more when the run ends
183183
for (const name of targets) {
184184
remaining[name] = (remaining[name] ?? 0) + 1;
185185
}
@@ -232,7 +232,7 @@ export const createCache = () => {
232232

233233
const { ended } = teeOf(name, result);
234234

235-
// Once nothing else reads it, its chunks go as they're recorded
235+
// Once nothing else reads it, its chunks go as they're produced
236236
release(name);
237237

238238
await ended;

‎packages/core/src/generators.mjs‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,12 @@ const createGenerator = () => {
112112
* Runs all requested generators with their dependencies.
113113
*
114114
* @param {import('./utils/configuration/types').Configuration} configuration - Runtime options
115-
* @param {{ results?: boolean }} [options] - With `results: false`, none of
116-
* the requested generators' results is kept, so each can be let go as soon
117-
* as it is produced
118115
* @returns {Promise<unknown[] | undefined>} Results of all requested
119-
* generators, or `undefined` with `results: false`
116+
* generators, or `undefined` with `discardResults`, which lets each go as
117+
* soon as it is produced
120118
*/
121-
const runGenerators = async (configuration, { results = true } = {}) => {
122-
const { target, threads } = configuration;
119+
const runGenerators = async configuration => {
120+
const { target, threads, discardResults } = configuration;
123121

124122
// Resolve shorthand names and load the full dependency closure up front,
125123
// so scheduling below is fully synchronous.
@@ -153,12 +151,12 @@ const createGenerator = () => {
153151

154152
// Start all reads in parallel (don't await sequentially). Reading through
155153
// the shared path lets the final read also trigger eviction.
156-
const read = results ? cache.consume : cache.drain;
154+
const read = discardResults ? cache.drain : cache.consume;
157155
const collected = await Promise.all(targets.map(read));
158156

159157
await pool.destroy();
160158

161-
return results ? collected : undefined;
159+
return discardResults ? undefined : collected;
162160
};
163161

164162
return { runGenerators };

‎packages/core/src/utils/configuration/types.d.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export type Configuration = {
1616

1717
// Number of items to process per worker thread
1818
chunkSize: number;
19+
20+
// Keeps none of the requested generators' results, for runs that only need
21+
// what the generators write
22+
discardResults?: boolean;
1923
} & {
2024
[K in keyof AllGenerators]: GlobalConfiguration &
2125
AllGenerators[K]['defaultConfiguration'];

0 commit comments

Comments
 (0)