watch: enable passthrough ipc in watch mode · nodejs/node@e895f7c · GitHub
Skip to content

Commit e895f7c

Browse files
znewshamtargos
authored andcommitted
watch: enable passthrough ipc in watch mode
PR-URL: #50890 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 37098eb commit e895f7c

3 files changed

Lines changed: 131 additions & 11 deletions

File tree

lib/internal/main/watch_mode.js

Lines changed: 21 additions & 11 deletions

lib/internal/watch_mode/files_watcher.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
const {
44
ArrayIsArray,
55
ArrayPrototypeForEach,
6+
Boolean,
67
SafeMap,
78
SafeSet,
9+
SafeWeakMap,
810
StringPrototypeStartsWith,
911
} = primordials;
1012

@@ -31,6 +33,8 @@ class FilesWatcher extends EventEmitter {
3133
#debounce;
3234
#mode;
3335
#signal;
36+
#passthroughIPC = false;
37+
#ipcHandlers = new SafeWeakMap();
3438

3539
constructor({ debounce = 200, mode = 'filter', signal } = kEmptyObject) {
3640
super({ __proto__: null, captureRejections: true });
@@ -40,6 +44,7 @@ class FilesWatcher extends EventEmitter {
4044
this.#debounce = debounce;
4145
this.#mode = mode;
4246
this.#signal = signal;
47+
this.#passthroughIPC = Boolean(process.send);
4348

4449
if (signal) {
4550
addAbortListener(signal, () => this.clear());
@@ -128,7 +133,31 @@ class FilesWatcher extends EventEmitter {
128133
this.#ownerDependencies.set(owner, dependencies);
129134
}
130135
}
136+
137+
138+
#setupIPC(child) {
139+
const handlers = {
140+
__proto__: null,
141+
parentToChild: (message) => child.send(message),
142+
childToParent: (message) => process.send(message),
143+
};
144+
this.#ipcHandlers.set(child, handlers);
145+
process.on('message', handlers.parentToChild);
146+
child.on('message', handlers.childToParent);
147+
}
148+
149+
destroyIPC(child) {
150+
const handlers = this.#ipcHandlers.get(child);
151+
if (this.#passthroughIPC && handlers !== undefined) {
152+
process.off('message', handlers.parentToChild);
153+
child.off('message', handlers.childToParent);
154+
}
155+
}
156+
131157
watchChildProcessModules(child, key = null) {
158+
if (this.#passthroughIPC) {
159+
this.#setupIPC(child);
160+
}
132161
if (this.#mode !== 'filter') {
133162
return;
134163
}

test/sequential/test-watch-mode.mjs

Lines changed: 81 additions & 0 deletions

0 commit comments

Comments
 (0)