Skip to content
Myr

Improve IPC documentation (#1079)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 7a0a1c6 · Browse files · 1 file+39 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+39 added−5 removed

  • other:+39 added−5 removed

1 file+39 added−5 removed

modified:docs/ipc.md+39 added-5 removed
34 unmodified lines
35```3637## Listening to messages3839The methods described above read a single message. On the other hand, [`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage) and [`getEachMessage()`](api.md#geteachmessage) return an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). This should be preferred when listening to multiple messages.39The methods described above read a single message. On the other hand, [`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage) and [`getEachMessage()`](api.md#geteachmessage) return an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). This should be [preferred](#prefer-geteachmessage-over-multiple-getonemessage) when listening to multiple messages.4041[`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage) waits for the subprocess to end (even when using [`break`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break) or [`return`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)). It throws if the subprocess [fails](api.md#result). This means you do not need to `await` the subprocess' [promise](execution.md#result).4243```js
17 unmodified lines
6162// The subprocess exits when hitting `break`63for await (const message of getEachMessage()) {64 if (message === 10) {65 break65 break;66 }6768 console.log(message); // 0, 2, 4, 6, 869 await sendMessage(message + 1);
91 unmodified lines
161### Call `getOneMessage()`/`getEachMessage()` early162163If a process sends a message but the other process is not listening/receiving it, that message is silently ignored.164165This means if [`getOneMessage()`](api.md#getonemessagegetonemessageoptions) or [`getEachMessage()`](api.md#geteachmessage) is called too late (i.e. after the other process called [`sendMessage()`](api.md#sendmessagemessage)), it will miss the message. Also, it will keep waiting for the missed message, which might either make it hang forever, or throw when the other process exits.165This means if [`getOneMessage()`](api.md#getonemessagegetonemessageoptions) or [`getEachMessage()`](api.md#geteachmessage) is called too late (i.e. after the other process called [`sendMessage(message)`](api.md#sendmessagemessage)), it will miss the message. Also, it will keep waiting for the missed message, which might either make it hang forever, or throw when the other process exits.166167Therefore, listening to messages should always be done early, before the other process sends any message.168169### Prefer `exchangeMessage()` over `sendMessage()` + `getOneMessage()`169### First `getOneMessage()` call170171On the other hand, the _very first_ call to [`getOneMessage()`](api.md#getonemessagegetonemessageoptions), [`getEachMessage()`](api.md#geteachmessage) or [`exchangeMessage(message)`](api.md#exchangemessagemessage-getonemessageoptions) never misses any message. That's because, when a subprocess is just starting, any message sent to it is buffered.172173This allows the current process to call [`subprocess.sendMessage(message)`](api.md#subprocesssendmessagemessage) right after spawning the subprocess, even if it is still initializing.174175```js176// parent.js177import {execaNode} from 'execa';178179const subprocess = execaNode`child.js`;180await subprocess.sendMessage('Hello');181await subprocess.sendMessage('world');182```183184```js185// child.js186import {getOneMessage} from 'execa';187188// This always retrieves the first message.189// Even if `sendMessage()` was called while the subprocess was still initializing.190const helloMessage = await getOneMessage();191192// But this might miss the second message, and hang forever.193// That's because it is not the first call to `getOneMessage()`.194const worldMessage = await getOneMessage();195```196197### Prefer `getEachMessage()` over multiple `getOneMessage()`198199If you call [`getOneMessage()`](api.md#getonemessagegetonemessageoptions) multiple times (like the example above) and the other process sends any message in between those calls, that message [will be missed](#call-getonemessagegeteachmessage-early).200201This can be prevented by using [`getEachMessage()`](api.md#geteachmessage) instead.202203### Prefer `exchangeMessage(message)` over `sendMessage(message)` + `getOneMessage()`204205When _first_ sending a message, _then_ receiving a response, the following code should be avoided.206207```js208import {sendMessage, getOneMessage} from 'execa';
6 unmodified lines
215```216217Indeed, when [`getOneMessage()`](api.md#getonemessagegetonemessageoptions) is called, the other process might have already sent a response. This only happens when the other process is very fast. However, when it does happen, `getOneMessage()` will miss that response.218185Using [`exchangeMessage()`](api.md#exchangemessagemessage-getonemessageoptions) prevents this race condition.219Using [`exchangeMessage(message)`](api.md#exchangemessagemessage-getonemessageoptions) prevents this race condition.220221```js222import {exchangeMessage} from 'execa';223
21 unmodified lines