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
Provenance
- Commit
4414737- Tree
606c24a- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
7a0a1c6
1 file changed+39 added−5 removed
modified:docs/ipc.md+39 added-5 removed
34 unmodified lines35 ```36 37 ## Listening to messages38 39-The 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.39+The 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.40 41 [`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).42 43 ```js17 unmodified lines61 62 // The subprocess exits when hitting `break`63 for await (const message of getEachMessage()) {64 if (message === 10) {65- break65+ break;66 }67 68 console.log(message); // 0, 2, 4, 6, 869 await sendMessage(message + 1);91 unmodified lines161 ### Call `getOneMessage()`/`getEachMessage()` early162 163 If a process sends a message but the other process is not listening/receiving it, that message is silently ignored.164 165-This 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.165+This 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.166 167 Therefore, listening to messages should always be done early, before the other process sends any message.168 169-### Prefer `exchangeMessage()` over `sendMessage()` + `getOneMessage()`169+### First `getOneMessage()` call170 171+On 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.172+173+This allows the current process to call [`subprocess.sendMessage(message)`](api.md#subprocesssendmessagemessage) right after spawning the subprocess, even if it is still initializing.174+175+```js176+// parent.js177+import {execaNode} from 'execa';178+179+const subprocess = execaNode`child.js`;180+await subprocess.sendMessage('Hello');181+await subprocess.sendMessage('world');182+```183+184+```js185+// child.js186+import {getOneMessage} from 'execa';187+188+// This always retrieves the first message.189+// Even if `sendMessage()` was called while the subprocess was still initializing.190+const helloMessage = await getOneMessage();191+192+// But this might miss the second message, and hang forever.193+// That's because it is not the first call to `getOneMessage()`.194+const worldMessage = await getOneMessage();195+```196+197+### Prefer `getEachMessage()` over multiple `getOneMessage()`198+199+If 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).200+201+This can be prevented by using [`getEachMessage()`](api.md#geteachmessage) instead.202+203+### Prefer `exchangeMessage(message)` over `sendMessage(message)` + `getOneMessage()`204+205 When _first_ sending a message, _then_ receiving a response, the following code should be avoided.206 207 ```js208 import {sendMessage, getOneMessage} from 'execa';6 unmodified lines215 ```216 217 Indeed, 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.218 185-Using [`exchangeMessage()`](api.md#exchangemessagemessage-getonemessageoptions) prevents this race condition.219+Using [`exchangeMessage(message)`](api.md#exchangemessagemessage-getonemessageoptions) prevents this race condition.220 221 ```js222 import {exchangeMessage} from 'execa';223 21 unmodified lines