Skip to content

Commit a13b6c9

Browse files
ovflowdavivkeller
andauthored
refactor: drop groupWholeModules, as metadata's chunks hold whole modules
From the review. A module is one file: parseApiDoc sets every entry's api once per file, and worker.stream never splits an item, so each of metadata's chunks already holds whole modules. section-pages reads its input as it comes with toBatches instead of regrouping it, and the helper and its tests go. Co-authored-by: Aviv Keller <me@aviv.sh> Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bec4a99 commit a13b6c9

6 files changed

Lines changed: 14 additions & 176 deletions

File tree

‎.changeset/stream-metadata.md‎

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

6-
`metadata` and `section-pages` stream their input: with more than one thread, they start on their dependency's first chunk, so parsing, metadata and section pages overlap. The generated files are unchanged. `groupWholeModules`, from `@doc-kit/core/utils/generators.mjs`, regroups streamed entries so that each batch holds whole modules, and `toBatches` gives an input that is whole or streamed as batches.
6+
`metadata` and `section-pages` stream their input: with more than one thread, they start on their dependency's first chunk, so parsing, metadata and section pages overlap. The generated files are unchanged. `toBatches`, from `@doc-kit/core/utils/generators.mjs`, gives an input that is whole or streamed as batches.

‎docs/creating-generators.md‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,8 @@ export default {
359359
- A dependency that does not stream arrives as a single chunk.
360360
- With one thread, `input` is still the collected array, so accept both.
361361
- `input` can be iterated only once.
362-
- `metadata`'s chunks hold whole files, but chunks need not hold whole modules:
363-
to get all of a module's entries at once, regroup them with
364-
`groupWholeModules` from `@doc-kit/core/utils/generators.mjs`.
362+
- `metadata`'s chunks hold whole files, and a file is one module, so a chunk
363+
holds all the entries of each module in it.
365364

366365
### Non-Streaming Generators
367366

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

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { describe, it } from 'node:test';
44
import {
55
getEntryDescription,
66
groupNodesByModule,
7-
groupWholeModules,
87
getVersionFromSemVer,
98
coerceSemVer,
109
getCompatibleVersions,
@@ -88,97 +87,6 @@ describe('groupNodesByModule', () => {
8887
});
8988
});
9089

91-
describe('groupWholeModules', () => {
92-
/**
93-
* Collects the batches regrouped from the given input, each entry as its
94-
* module and number.
95-
*
96-
* @param {Array<{ api: string }> | AsyncIterable<Array<{ api: string }>>} input
97-
* @returns {Promise<string[][]>}
98-
*/
99-
const collect = async input => {
100-
const output = [];
101-
102-
for await (const batch of groupWholeModules(input)) {
103-
output.push(batch.map(({ api, n }) => `${api}${n}`));
104-
}
105-
106-
return output;
107-
};
108-
109-
/**
110-
* Regroups the given batches, streamed in, and collects the batches it
111-
* yields.
112-
*
113-
* @param {Array<Array<{ api: string }>>} batches
114-
* @returns {Promise<string[][]>}
115-
*/
116-
const regroup = batches => {
117-
async function* input() {
118-
yield* batches;
119-
}
120-
121-
return collect(input());
122-
};
123-
124-
/**
125-
* An entry of a module, told apart from the module's others by its number.
126-
*
127-
* @param {string} api
128-
* @param {number} n
129-
* @returns {{ api: string, n: number }}
130-
*/
131-
const entry = (api, n) => ({ api, n });
132-
133-
it('passes the entries on in the same order, each batch ending once its last module is whole', async () => {
134-
const output = await regroup([
135-
[entry('a', 1), entry('a', 2), entry('b', 1)],
136-
[entry('c', 1)],
137-
]);
138-
139-
assert.deepEqual(output, [['a1', 'a2'], ['b1'], ['c1']]);
140-
});
141-
142-
it('holds back a module that continues in the next batch', async () => {
143-
const output = await regroup([
144-
[entry('a', 1), entry('b', 1)],
145-
[entry('b', 2), entry('b', 3)],
146-
[entry('b', 4), entry('c', 1)],
147-
]);
148-
149-
assert.deepEqual(output, [['a1'], ['b1', 'b2', 'b3', 'b4'], ['c1']]);
150-
});
151-
152-
it('skips empty batches', async () => {
153-
const output = await regroup([[], [entry('a', 1)], []]);
154-
155-
assert.deepEqual(output, [['a1']]);
156-
});
157-
158-
it('takes entries given as one array as a single batch', async () => {
159-
const output = await collect([entry('a', 1), entry('a', 2), entry('b', 1)]);
160-
161-
assert.deepEqual(output, [['a1', 'a2'], ['b1']]);
162-
});
163-
164-
it('rejects a module whose entries arrive apart', async () => {
165-
await assert.rejects(
166-
regroup([
167-
[entry('a', 1), entry('b', 1)],
168-
[entry('c', 1), entry('a', 2)],
169-
]),
170-
/"a" arrived apart/
171-
);
172-
});
173-
174-
it('rejects a module whose entries arrive apart in one batch too', async () => {
175-
await assert.rejects(
176-
regroup([[entry('a', 1), entry('b', 1), entry('a', 2)]]),
177-
/"a" arrived apart/
178-
);
179-
});
180-
});
181-
18290
describe('getVersionFromSemVer', () => {
18391
it('returns major.x for minor 0', () => {
18492
const version = { major: 18, minor: 0, patch: 0 };

‎packages/core/src/utils/generators.mjs‎

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -59,67 +59,6 @@ export const toBatches = input => {
5959
return [input];
6060
};
6161

62-
/**
63-
* Regroups streamed entries so that each batch holds whole modules: a module
64-
* whose entries continue in the next batch is held back and passed on with
65-
* it. A module's entries must arrive together: one that comes back after
66-
* another module's is an error, wherever the batches end, as its pages would
67-
* be built without it. Entries given as one array, as a generator that streams
68-
* its input gets them with one thread, are taken as a single batch.
69-
*
70-
* @template {{ api: string }} T
71-
* @param {Array<T> | AsyncIterable<Array<T>>} batches - The entries, in
72-
* batches as they are streamed, or in one array
73-
* @returns {AsyncGenerator<Array<T>>} The same entries, in batches of whole
74-
* modules
75-
*/
76-
export async function* groupWholeModules(batches) {
77-
// The modules whose entries have all arrived
78-
const finished = new Set();
79-
80-
// The module whose entries are arriving, and those of them held back
81-
let current;
82-
let held = [];
83-
84-
for await (const batch of toBatches(batches)) {
85-
for (const { api } of batch) {
86-
if (api !== current) {
87-
if (finished.has(api)) {
88-
throw new Error(
89-
`The entries of "${api}" arrived apart: a module's entries must ` +
90-
'arrive together, in one chunk or in consecutive ones'
91-
);
92-
}
93-
94-
if (current !== undefined) {
95-
finished.add(current);
96-
}
97-
98-
current = api;
99-
}
100-
}
101-
102-
const entries = held.length > 0 ? [...held, ...batch] : batch;
103-
104-
// The module the batch ends with may continue in the next one
105-
let end = entries.length;
106-
107-
while (end > 0 && entries[end - 1].api === current) {
108-
end--;
109-
}
110-
111-
held = entries.slice(end);
112-
113-
if (end > 0) {
114-
yield entries.slice(0, end);
115-
}
116-
}
117-
118-
if (held.length > 0) {
119-
yield held;
120-
}
121-
}
122-
12362
/**
12463
* Parses the SemVer string into a Node.js-alike version
12564
*

‎packages/react/src/section-pages/__tests__/generate.test.mjs‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -308,46 +308,37 @@ describe('section-pages generate', () => {
308308
});
309309

310310
it(
311-
'passes whole modules on before the next batch arrives',
312-
// The next batch waits for the test, so modules held back until it arrives
313-
// would never be passed on
311+
'passes each batch on before the next one arrives',
312+
// The next batch waits for the test, so a batch held back until it
313+
// arrives would never be passed on
314314
{ timeout: 10_000 },
315315
async () => {
316316
const gate = Promise.withResolvers();
317-
const fs = createModule();
317+
const first = [...createModule(), createEntry('tiny', 1, 'Tiny')];
318318

319319
async function* batches() {
320-
yield [...fs, createEntry('tiny', 1, 'Tiny')];
320+
yield first;
321321
await gate.promise;
322322
yield [createEntry('zlib', 1, 'Zlib')];
323323
}
324324

325325
const output = generate(batches());
326326
const { value } = await output.next();
327327

328-
// `fs` is whole, `tiny` may continue in the next batch
329-
assert.deepEqual(value, fs);
328+
assert.deepEqual(value, first);
330329

331330
gate.resolve();
332331

333332
const rest = await collect(output);
334333

335334
assert.deepEqual(
336335
rest.filter(e => !e.chunk).map(e => e.api),
337-
['tiny', 'zlib']
336+
['zlib']
338337
);
339338
assert.equal(
340339
rest.filter(e => e.chunk && e.heading.depth === 1).length,
341340
3
342341
);
343342
}
344343
);
345-
346-
it('builds a module split across batches as one', async () => {
347-
const whole = await run(createModule());
348-
const module = createModule();
349-
const split = await run(module.slice(0, 2), module.slice(2));
350-
351-
assert.deepEqual(split, whole);
352-
});
353344
});

‎packages/react/src/section-pages/generate.mjs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import getConfig from '@doc-kit/core/utils/configuration/index.mjs';
44
import {
55
groupNodesByModule,
6-
groupWholeModules,
6+
toBatches,
77
} from '@doc-kit/core/utils/generators.mjs';
88
import { visit } from 'unist-util-visit';
99

@@ -159,8 +159,9 @@ export async function* generate(input) {
159159

160160
const chunkPages = [];
161161

162-
// A module's chunk pages need all of its entries
163-
for await (const entries of groupWholeModules(input)) {
162+
// A module's chunk pages need all of its entries, which arrive together:
163+
// each of `metadata`'s chunks holds whole files, and a file is one module
164+
for await (const entries of toBatches(input)) {
164165
yield entries;
165166

166167
chunkPages.push(...buildChunkPages(entries, config));

0 commit comments

Comments
 (0)