Skip to content
Myr

run-sync.js

This file

Path

lib/transform/run-sync.js

50 lines · 1.3 KiB · JavaScript

Last changed by

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

History of this file

Take it

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 1why, one step back
1// Duplicate the code from `run-async.js` but as synchronous functions

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 2why, one step back
2export const pushChunksSync = (getChunksSync, getChunksArguments, transformStream, done) => {

Rename files (#977)ehmickybefore adoption4b2e316

line 3why, one step back
3 try {

Upgrade xo (#980)ehmickybefore adoptionccfa412

line 4why, one step back
4 for (const chunk of getChunksSync(...getChunksArguments)) {

Rename files (#977)ehmickybefore adoption4b2e316

lines 5–50why, one step back
5 transformStream.push(chunk);
6 }78 done();9 } catch (error) {10 done(error);11 }12};1314// Run synchronous generators with `execaSync()`15export const runTransformSync = (generators, chunks) => [16 ...chunks.flatMap(chunk => [...transformChunkSync(chunk, generators, 0)]),17 ...finalChunksSync(generators),18];1920export const transformChunkSync = function * (chunk, generators, index) {21 if (index === generators.length) {22 yield chunk;23 return;24 }2526 const {transform = identityGenerator} = generators[index];27 for (const transformedChunk of transform(chunk)) {28 yield * transformChunkSync(transformedChunk, generators, index + 1);29 }30};3132export const finalChunksSync = function * (generators) {33 for (const [index, {final}] of Object.entries(generators)) {34 yield * generatorFinalChunksSync(final, Number(index), generators);35 }36};3738const generatorFinalChunksSync = function * (final, index, generators) {39 if (final === undefined) {40 return;41 }4243 for (const finalChunk of final()) {44 yield * transformChunkSync(finalChunk, generators, index + 1);45 }46};4748const identityGenerator = function * (chunk) {49 yield chunk;50};