Skip to content
Myr

reference.js

This file

Path

lib/ipc/reference.js

21 lines · 1.0 KiB · JavaScript

Last changed by

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

History of this file

Take it

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 1–21why, one step back
1// By default, Node.js keeps the subprocess alive while it has a `message` or `disconnect` listener.
2// We replicate the same logic for the events that we proxy.3// This ensures the subprocess is kept alive while `sendMessage()`, `getOneMessage()` and `getEachMessage()` are ongoing.4// See https://github.com/nodejs/node/blob/2aaeaa863c35befa2ebaa98fb7737ec84df4d8e9/lib/internal/child_process.js#L5475export const addReference = anyProcess => {6 anyProcess.channel?.refCounted();7};89export const removeReference = anyProcess => {10 anyProcess.channel?.unrefCounted();11};1213// To proxy events, we setup some global listeners on the `message` and `disconnect` events.14// Those should not keep the subprocess alive, so we remove the automatic counting that Node.js is doing.15// See https://github.com/nodejs/node/blob/1b965270a9c273d4cf70e8808e9d28b9ada7844f/lib/child_process.js#L18016export const undoAddedReferences = (anyProcess, isSubprocess) => {17 if (isSubprocess) {18 removeReference(anyProcess);19 removeReference(anyProcess);20 }21};