|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -import publicGenerators from './generators/index.mjs'; |
4 |
| -import astJs from './generators/ast-js/index.mjs'; |
5 |
| -import oramaDb from './generators/orama-db/index.mjs'; |
6 |
| - |
7 |
| -const availableGenerators = { |
8 |
| - ...publicGenerators, |
9 |
| - // This one is a little special since we don't want it to run unless we need |
10 |
| - // it and we also don't want it to be publicly accessible through the CLI. |
11 |
| - 'ast-js': astJs, |
12 |
| - 'orama-db': oramaDb, |
13 |
| -}; |
| 3 | +import { allGenerators } from './generators/index.mjs'; |
| 4 | +import WorkerPool from './threading/index.mjs'; |
14 | 5 |
|
15 | 6 | /**
|
16 | 7 | * @typedef {{ ast: GeneratorMetadata<ApiDocMetadataEntry, ApiDocMetadataEntry>}} AstGenerator The AST "generator" is a facade for the AST tree and it isn't really a generator
|
@@ -43,30 +34,39 @@ const createGenerator = markdownInput => {
|
43 | 34 | */
|
44 | 35 | const cachedGenerators = { ast: Promise.resolve(markdownInput) };
|
45 | 36 |
|
| 37 | + const threadPool = new WorkerPool(); |
| 38 | + |
46 | 39 | /**
|
47 | 40 | * Runs the Generator engine with the provided top-level input and the given generator options
|
48 | 41 | *
|
49 | 42 | * @param {GeneratorOptions} options The options for the generator runtime
|
50 | 43 | */
|
51 |
| - const runGenerators = async ({ generators, ...extra }) => { |
| 44 | + const runGenerators = async ({ generators, threads, ...extra }) => { |
52 | 45 | // Note that this method is blocking, and will only execute one generator per-time
|
53 | 46 | // but it ensures all dependencies are resolved, and that multiple bottom-level generators
|
54 | 47 | // can reuse the already parsed content from the top-level/dependency generators
|
55 | 48 | for (const generatorName of generators) {
|
56 |
| - const { dependsOn, generate } = availableGenerators[generatorName]; |
| 49 | + const { dependsOn, generate } = allGenerators[generatorName]; |
57 | 50 |
|
58 | 51 | // If the generator dependency has not yet been resolved, we resolve
|
59 | 52 | // the dependency first before running the current generator
|
60 | 53 | if (dependsOn && dependsOn in cachedGenerators === false) {
|
61 |
| - await runGenerators({ ...extra, generators: [dependsOn] }); |
| 54 | + await runGenerators({ |
| 55 | + ...extra, |
| 56 | + threads, |
| 57 | + generators: [dependsOn], |
| 58 | + }); |
62 | 59 | }
|
63 | 60 |
|
64 | 61 | // Ensures that the dependency output gets resolved before we run the current
|
65 | 62 | // generator with its dependency output as the input
|
66 | 63 | const dependencyOutput = await cachedGenerators[dependsOn];
|
67 | 64 |
|
68 | 65 | // Adds the current generator execution Promise to the cache
|
69 |
| - cachedGenerators[generatorName] = generate(dependencyOutput, extra); |
| 66 | + cachedGenerators[generatorName] = |
| 67 | + threads < 2 |
| 68 | + ? generate(dependencyOutput, extra) // Run in main thread |
| 69 | + : threadPool.run(generatorName, dependencyOutput, threads, extra); // Offload to worker thread |
70 | 70 | }
|
71 | 71 |
|
72 | 72 | // Returns the value of the last generator of the current pipeline
|
|
0 commit comments