@@ -40,40 +40,43 @@ const createGenerator = input => {
4040 const runGenerators = async options => {
4141 const { generators, threads } = options ;
4242
43- // WorkerPool for running full generators in worker threads
44- const generatorPool = new WorkerPool ( './generator-worker.mjs' , threads ) ;
45-
4643 // WorkerPool for chunk-level parallelization within generators
4744 const chunkPool = new WorkerPool ( './chunk-worker.mjs' , threads ) ;
4845
49- // Note that this method is blocking, and will only execute one generator per-time
50- // but it ensures all dependencies are resolved, and that multiple bottom-level generators
51- // can reuse the already parsed content from the top-level/dependency generators
46+ // Schedule all generators, allowing independent ones to run in parallel.
47+ // Each generator awaits its own dependency internally, so generators
48+ // with the same dependency (e.g. legacy-html and legacy-json both depend
49+ // on metadata) will run concurrently once metadata resolves.
5250 for ( const generatorName of generators ) {
51+ // Skip if already scheduled
52+ if ( generatorName in cachedGenerators ) {
53+ continue ;
54+ }
55+
5356 const { dependsOn, generate } = allGenerators [ generatorName ] ;
5457
55- // If the generator dependency has not yet been resolved, we resolve
56- // the dependency first before running the current generator
57- if ( dependsOn && dependsOn in cachedGenerators === false ) {
58+ // Ensure dependency is scheduled (but don't await its result yet)
59+ if ( dependsOn && ! ( dependsOn in cachedGenerators ) ) {
5860 await runGenerators ( { ...options , generators : [ dependsOn ] } ) ;
5961 }
6062
61- // Ensures that the dependency output gets resolved before we run the current
62- // generator with its dependency output as the input
63- const input = await cachedGenerators [ dependsOn ] ;
64-
65- // Create a ParallelWorker for this generator to use for item-level parallelization
63+ // Create a ParallelWorker for this generator
6664 const worker = createParallelWorker ( generatorName , chunkPool , options ) ;
6765
68- // Adds the current generator execution Promise to the cache
69- cachedGenerators [ generatorName ] =
70- threads < 2
71- ? generate ( input , { ...options , worker } )
72- : generatorPool . run ( { generatorName, input, options } ) ;
66+ /**
67+ * Schedule the generator - it awaits its dependency internally
68+ * his allows multiple generators with the same dependency to run in parallel
69+ */
70+ const scheduledGenerator = async ( ) => {
71+ const input = await cachedGenerators [ dependsOn ] ;
72+
73+ return generate ( input , { ...options , worker } ) ;
74+ } ;
75+
76+ cachedGenerators [ generatorName ] = scheduledGenerator ( ) ;
7377 }
7478
7579 // Returns the value of the last generator of the current pipeline
76- // Note that dependencies will be awaited (as shown on line 48)
7780 return cachedGenerators [ generators [ generators . length - 1 ] ] ;
7881 } ;
7982
0 commit comments