Skip to content

Commit 0261be7

Browse files
committed
chore: self review and code review
1 parent 4932c4e commit 0261be7

File tree

11 files changed

+263
-362
lines changed

11 files changed

+263
-362
lines changed

src/generators.mjs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,15 @@ const createGenerator = input => {
3737
*
3838
* @param {GeneratorOptions} options The options for the generator runtime
3939
*/
40-
const runGenerators = async ({
41-
generators,
42-
threads,
43-
chunkSize,
44-
...extra
45-
}) => {
40+
const runGenerators = async options => {
41+
const { generators, threads } = options;
42+
4643
// WorkerPool for running full generators in worker threads
4744
const generatorPool = new WorkerPool('./generator-worker.mjs', threads);
4845

4946
// WorkerPool for chunk-level parallelization within generators
5047
const chunkPool = new WorkerPool('./chunk-worker.mjs', threads);
5148

52-
// Options including threading config
53-
const threadingOptions = { threads, chunkSize };
54-
5549
// Note that this method is blocking, and will only execute one generator per-time
5650
// but it ensures all dependencies are resolved, and that multiple bottom-level generators
5751
// can reuse the already parsed content from the top-level/dependency generators
@@ -61,34 +55,21 @@ const createGenerator = input => {
6155
// If the generator dependency has not yet been resolved, we resolve
6256
// the dependency first before running the current generator
6357
if (dependsOn && dependsOn in cachedGenerators === false) {
64-
await runGenerators({
65-
...extra,
66-
...threadingOptions,
67-
generators: [dependsOn],
68-
});
58+
await runGenerators({ ...options, generators: [dependsOn] });
6959
}
7060

7161
// Ensures that the dependency output gets resolved before we run the current
7262
// generator with its dependency output as the input
7363
const input = await cachedGenerators[dependsOn];
7464

7565
// Create a ParallelWorker for this generator to use for item-level parallelization
76-
const worker = createParallelWorker(generatorName, chunkPool, {
77-
...extra,
78-
...threadingOptions,
79-
});
80-
81-
// Generator options with worker instance
82-
const generatorOptions = { ...extra, ...threadingOptions, worker };
83-
84-
// Worker options for the worker thread
85-
const workerOptions = { ...extra, ...threadingOptions };
66+
const worker = createParallelWorker(generatorName, chunkPool, options);
8667

8768
// Adds the current generator execution Promise to the cache
8869
cachedGenerators[generatorName] =
8970
threads < 2
90-
? generate(input, generatorOptions) // Run in main thread
91-
: generatorPool.run({ generatorName, input, options: workerOptions }); // Offload to worker thread
71+
? generate(input, { ...options, worker })
72+
: generatorPool.run({ generatorName, input, options });
9273
}
9374

9475
// Returns the value of the last generator of the current pipeline

src/generators/ast-js/index.mjs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ export default {
2424

2525
/**
2626
* Process a chunk of JavaScript files in a worker thread.
27-
* Called by chunk-worker.mjs for parallel processing.
28-
*
29-
* @param {unknown} _ - Unused (we use options.input instead)
30-
* @param {number[]} itemIndices - Indices of source files to process
27+
* @param {unknown} _
28+
* @param {number[]} itemIndices
3129
* @param {Partial<GeneratorOptions>} options
3230
*/
3331
async processChunk(_, itemIndices, { input }) {
3432
const { loadFiles } = createJsLoader();
35-
3633
const sourceFiles = loadFiles(input ?? []);
3734

3835
const { parseJsSource } = createJsParser();
@@ -57,8 +54,6 @@ export default {
5754
const sourceFiles = loadFiles(input ?? []);
5855

5956
// Parse the Javascript sources into ASTs in parallel using worker threads
60-
// Note: We pass sourceFiles as items but _ (empty) as fullInput since
61-
// processChunk reloads files from options.input
6257
return worker.map(sourceFiles, _, { input });
6358
},
6459
};

src/generators/jsx-ast/index.mjs

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1+
import { OVERRIDDEN_POSITIONS } from './constants.mjs';
12
import { buildSideBarProps } from './utils/buildBarProps.mjs';
23
import buildContent from './utils/buildContent.mjs';
3-
import {
4-
buildDocPages,
5-
getSortedHeadNodes,
6-
groupNodesByModule,
7-
} from '../../utils/generators.mjs';
4+
import { groupNodesByModule } from '../../utils/generators.mjs';
85
import { getRemarkRecma } from '../../utils/remark.mjs';
96

107
/**
@@ -13,6 +10,34 @@ import { getRemarkRecma } from '../../utils/remark.mjs';
1310
* @typedef {Array<ApiDocMetadataEntry>} Input
1411
* @type {GeneratorMetadata<Input, string>}
1512
*/
13+
14+
/**
15+
* Sorts entries by OVERRIDDEN_POSITIONS and then heading name.
16+
* @param {Array<ApiDocMetadataEntry>} entries
17+
*/
18+
const getSortedHeadNodes = entries => {
19+
return entries
20+
.filter(node => node.heading.depth === 1)
21+
.sort((a, b) => {
22+
const ai = OVERRIDDEN_POSITIONS.indexOf(a.api);
23+
const bi = OVERRIDDEN_POSITIONS.indexOf(b.api);
24+
25+
if (ai !== -1 && bi !== -1) {
26+
return ai - bi;
27+
}
28+
29+
if (ai !== -1) {
30+
return -1;
31+
}
32+
33+
if (bi !== -1) {
34+
return 1;
35+
}
36+
37+
return a.heading.data.name.localeCompare(b.heading.data.name);
38+
});
39+
};
40+
1641
export default {
1742
name: 'jsx-ast',
1843
version: '1.0.0',
@@ -26,36 +51,45 @@ export default {
2651
* @param {Partial<GeneratorOptions>} options
2752
*/
2853
async processChunk(fullInput, itemIndices, { index, releases, version }) {
29-
const processor = getRemarkRecma();
54+
const remarkRecma = getRemarkRecma();
3055
const groupedModules = groupNodesByModule(fullInput);
3156
const headNodes = getSortedHeadNodes(fullInput);
32-
const docPages = buildDocPages(headNodes, index);
3357

34-
return Promise.all(
35-
itemIndices.map(async idx => {
36-
const entry = headNodes[idx];
58+
const docPages = index
59+
? index.map(({ section, api }) => [section, `${api}.html`])
60+
: headNodes.map(node => [node.heading.data.name, `${node.api}.html`]);
3761

38-
const sideBarProps = buildSideBarProps(
39-
entry,
40-
releases,
41-
version,
42-
docPages
43-
);
62+
const results = [];
4463

45-
return buildContent(
64+
for (const idx of itemIndices) {
65+
const entry = headNodes[idx];
66+
67+
const sideBarProps = buildSideBarProps(
68+
entry,
69+
releases,
70+
version,
71+
docPages
72+
);
73+
74+
results.push(
75+
await buildContent(
4676
groupedModules.get(entry.api),
4777
entry,
4878
sideBarProps,
49-
processor
50-
);
51-
})
52-
);
79+
remarkRecma
80+
)
81+
);
82+
}
83+
84+
return results;
5385
},
5486

5587
/**
5688
* Generates a JSX AST
89+
*
5790
* @param {Input} entries
5891
* @param {Partial<GeneratorOptions>} options
92+
* @returns {Promise<Array<string>>} Array of generated content
5993
*/
6094
async generate(entries, { index, releases, version, worker }) {
6195
const headNodes = getSortedHeadNodes(entries);

0 commit comments

Comments
 (0)