Skip to content
Myr

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 file

Take it

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 1why, one step back
1import {createDeferred} from '../utils/deferred.js';

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

line 2why, one step back
2import {getFdSpecificValue} from '../arguments/specific.js';

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 3why, one step back
3import {validateStrictDeadlock} from './strict.js';

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 4–7why, one step back
4
5// 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 back
8export const startSendMessage = (anyProcess, wrappedMessage, strict) => {

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 9–14why, one step back
9 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 back
15 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 back
19};
20

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 21–23why, one step back
21export const endSendMessage = ({outgoingMessages, outgoingMessage}) => {
22 outgoingMessages.delete(outgoingMessage);23 outgoingMessage.onMessageSent.resolve();

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 24–25why, one step back
24};
25

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 26why, one step back
26// Await while `sendMessage()` is ongoing, unless there is already a `message` listener

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 27why, one step back
27export const waitForOutgoingMessages = async (anyProcess, ipcEmitter, wrappedMessage) => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 28why, one step back
28 while (!hasMessageListeners(anyProcess, ipcEmitter) && OUTGOING_MESSAGES.get(anyProcess)?.size > 0) {

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 29–30why, one step back
29 const outgoingMessages = [...OUTGOING_MESSAGES.get(anyProcess)];
30 validateStrictDeadlock(outgoingMessages, wrappedMessage);

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 31why, one step back
31 // eslint-disable-next-line no-await-in-loop

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 32why, one step back
32 await Promise.all(outgoingMessages.map(({onMessageSent}) => onMessageSent));

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 33–36why, one step back
33 }
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 back
37const IPC_SUBPROCESS_OPTIONS = new WeakMap();

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 38why, one step back
38

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

lines 39–42why, one step back
39export const setIpcSubprocessOptions = (subprocess, options) => {
40 IPC_SUBPROCESS_OPTIONS.set(subprocess, options);41};42

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 43–47why, one step back
43// Whether any `message` listener is setup
44export 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 back
48const getMinListenerCount = anyProcess => getOptions(anyProcess) !== undefined
49 && !getFdSpecificValue(getOptions(anyProcess).buffer, 'ipc')

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 50–51why, one step back
50 ? 1
51 : 0;

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

lines 52–53why, one step back
52
53const getOptions = anyProcess => IPC_SUBPROCESS_OPTIONS.get(anyProcess);