outgoing.js
This file
Path
lib/ipc/outgoing.js
53 lines · 2.4 KiB · JavaScript
Last changed by
Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
History of this fileTake it
Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 1why, one step back1import {createDeferred} from '../utils/deferred.js';Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 2why, one step back2import {getFdSpecificValue} from '../arguments/specific.js';Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427
line 3why, one step back3import {validateStrictDeadlock} from './strict.js';Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 4–7why, one step back45// When `sendMessage()` is ongoing, any `message` being received waits before being emitted.6// This allows calling one or multiple `await sendMessage()` followed by `await getOneMessage()`/`await getEachMessage()`.7// Without running into a race condition when the other process sends a response too fast, before the current process set up a listener.Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427
line 8why, one step back8export const startSendMessage = (anyProcess, wrappedMessage, strict) => {Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 9–14why, one step back9 if (!OUTGOING_MESSAGES.has(anyProcess)) {10 OUTGOING_MESSAGES.set(anyProcess, new Set());11 }1213 const outgoingMessages = OUTGOING_MESSAGES.get(anyProcess);14 const onMessageSent = createDeferred();Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427
lines 15–18why, one step back15 const id = strict ? wrappedMessage.id : undefined;16 const outgoingMessage = {onMessageSent, id};17 outgoingMessages.add(outgoingMessage);18 return {outgoingMessages, outgoingMessage};Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 19–20why, one step back19};20Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427
lines 21–23why, one step back21export const endSendMessage = ({outgoingMessages, outgoingMessage}) => {22 outgoingMessages.delete(outgoingMessage);23 outgoingMessage.onMessageSent.resolve();Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 24–25why, one step back24};25Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 26why, one step back26// Await while `sendMessage()` is ongoing, unless there is already a `message` listenerFix deadlocks with IPC (#1104)ehmickybefore adoption465e427
line 27why, one step back27export const waitForOutgoingMessages = async (anyProcess, ipcEmitter, wrappedMessage) => {Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 28why, one step back28 while (!hasMessageListeners(anyProcess, ipcEmitter) && OUTGOING_MESSAGES.get(anyProcess)?.size > 0) {Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427
lines 29–30why, one step back29 const outgoingMessages = [...OUTGOING_MESSAGES.get(anyProcess)];30 validateStrictDeadlock(outgoingMessages, wrappedMessage);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 31why, one step back31 // eslint-disable-next-line no-await-in-loopFix deadlocks with IPC (#1104)ehmickybefore adoption465e427
line 32why, one step back32 await Promise.all(outgoingMessages.map(({onMessageSent}) => onMessageSent));Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 33–36why, one step back33 }34};3536const OUTGOING_MESSAGES = new WeakMap();Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 37why, one step back37const IPC_SUBPROCESS_OPTIONS = new WeakMap();Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 38why, one step back38Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 39–42why, one step back39export const setIpcSubprocessOptions = (subprocess, options) => {40 IPC_SUBPROCESS_OPTIONS.set(subprocess, options);41};42Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 43–47why, one step back43// Whether any `message` listener is setup44export const hasMessageListeners = (anyProcess, ipcEmitter) => ipcEmitter.listenerCount('message') > getMinListenerCount(anyProcess);4546// When `buffer` is `false`, we set up a `message` listener that should be ignored.47// That listener is only meant to intercept `strict` acknowledgement responses.Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 48–49why, one step back48const getMinListenerCount = anyProcess => getOptions(anyProcess) !== undefined49 && !getFdSpecificValue(getOptions(anyProcess).buffer, 'ipc')Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
lines 50–51why, one step back50 ? 151 : 0;Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 52–53why, one step back5253const getOptions = anyProcess => IPC_SUBPROCESS_OPTIONS.get(anyProcess);