Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5dbea32

Browse files
committed
fix(child-process): Use Set to manage book-keeping instead of mutable integer
1 parent 892ebc2 commit 5dbea32

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

core/child-process/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const execa = require("execa");
66
const logTransformer = require("strong-log-transformer");
77

88
// bookkeeping for spawned processes
9-
let children = 0;
9+
const children = new Set();
1010

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

@@ -54,9 +54,9 @@ function spawnStreaming(command, args, opts, prefix) {
5454
}
5555

5656
// Avoid "Possible EventEmitter memory leak detected" warning due to piped stdio
57-
if (children > process.stdout.listenerCount("close")) {
58-
process.stdout.setMaxListeners(children);
59-
process.stderr.setMaxListeners(children);
57+
if (children.size > process.stdout.listenerCount("close")) {
58+
process.stdout.setMaxListeners(children.size);
59+
process.stderr.setMaxListeners(children.size);
6060
}
6161

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

6868
function getChildProcessCount() {
69-
return children;
69+
return children.size;
7070
}
7171

7272
function getExitCode(result) {
@@ -86,11 +86,9 @@ function getExitCode(result) {
8686
}
8787

8888
function spawnProcess(command, args, opts) {
89-
children += 1;
90-
9189
const child = execa(command, args, opts);
9290
const drain = (code, signal) => {
93-
children -= 1;
91+
children.delete(child);
9492

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

106+
children.add(child);
107+
108108
return child;
109109
}
110110

0 commit comments

Comments
 (0)