Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(child-process): Use Set to manage book-keeping instead of mutable…
… integer
  • Loading branch information
evocateur committed Oct 7, 2019
1 parent 892ebc2 commit 5dbea32
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions core/child-process/index.js
Expand Up @@ -6,9 +6,9 @@ const execa = require("execa");
const logTransformer = require("strong-log-transformer");

// bookkeeping for spawned processes
let children = 0;
const children = new Set();

// when streaming children are spawned, use this color for prefix
// when streaming processes are spawned, use this color for prefix
const colorWheel = ["cyan", "magenta", "blue", "yellow", "green", "red"];
const NUM_COLORS = colorWheel.length;

Expand Down Expand Up @@ -54,9 +54,9 @@ function spawnStreaming(command, args, opts, prefix) {
}

// Avoid "Possible EventEmitter memory leak detected" warning due to piped stdio
if (children > process.stdout.listenerCount("close")) {
process.stdout.setMaxListeners(children);
process.stderr.setMaxListeners(children);
if (children.size > process.stdout.listenerCount("close")) {
process.stdout.setMaxListeners(children.size);
process.stderr.setMaxListeners(children.size);
}

spawned.stdout.pipe(logTransformer(stdoutOpts)).pipe(process.stdout);
Expand All @@ -66,7 +66,7 @@ function spawnStreaming(command, args, opts, prefix) {
}

function getChildProcessCount() {
return children;
return children.size;
}

function getExitCode(result) {
Expand All @@ -86,11 +86,9 @@ function getExitCode(result) {
}

function spawnProcess(command, args, opts) {
children += 1;

const child = execa(command, args, opts);
const drain = (code, signal) => {
children -= 1;
children.delete(child);

// don't run repeatedly if this is the error event
if (signal === undefined) {
Expand All @@ -105,6 +103,8 @@ function spawnProcess(command, args, opts) {
child.pkg = opts.pkg;
}

children.add(child);

return child;
}

Expand Down

0 comments on commit 5dbea32

Please sign in to comment.