reference.js
This file
Path
lib/ipc/reference.js
48 lines · 1.8 KiB · JavaScript
Last changed by
Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
History of this fileTake it
Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 1–2why, one step back1// 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.Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
lines 3–6why, one step back3// This ensures the subprocess is kept alive while `getOneMessage()` and `getEachMessage()` are ongoing.4// This is not a problem with `sendMessage()` since Node.js handles that method automatically.5// We do not use `anyProcess.channel.ref()` since this would prevent the automatic `.channel.refCounted()` Node.js is doing.6// We keep a reference to `anyProcess.channel` since it might be `null` while `getOneMessage()` or `getEachMessage()` is still processing debounced messages.Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 7why, one step back7// See https://github.com/nodejs/node/blob/2aaeaa863c35befa2ebaa98fb7737ec84df4d8e9/lib/internal/child_process.js#L547Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
lines 8–14why, one step back8export const addReference = (channel, reference) => {9 if (reference) {10 addReferenceCount(channel);11 }12};1314const addReferenceCount = channel => {Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 15why, one step back15 channel.refCounted();Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 16–17why, one step back16};17Add `reference` option to `getOneMessage()` and `getEachMessage()` (#1107)ehmickybefore adoptionff02af6
lines 18–24why, one step back18export const removeReference = (channel, reference) => {19 if (reference) {20 removeReferenceCount(channel);21 }22};2324const removeReferenceCount = channel => {Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 25why, one step back25 channel.unrefCounted();Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 26–30why, one step back26};2728// To proxy events, we setup some global listeners on the `message` and `disconnect` events.29// Those should not keep the subprocess alive, so we remove the automatic counting that Node.js is doing.30// See https://github.com/nodejs/node/blob/1b965270a9c273d4cf70e8808e9d28b9ada7844f/lib/child_process.js#L180Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 31why, one step back31export const undoAddedReferences = (channel, isSubprocess) => {Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
lines 32–33why, one step back32 if (!isSubprocess) {33 return;Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
line 34why, one step back34 }Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
lines 35–37why, one step back3536 removeReferenceCount(channel);37 removeReferenceCount(channel);Refactor reference counting with IPC (#1089)ehmickybefore adoption18e607c
lines 38–41why, one step back38};3940// Reverse it during `disconnect`41export const redoAddedReferences = (channel, isSubprocess) => {Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
lines 42–43why, one step back42 if (!isSubprocess) {43 return;Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 44why, one step back44 }Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
lines 45–47why, one step back4546 addReferenceCount(channel);47 addReferenceCount(channel);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 48why, one step back48};