Skip to content

Commit 66fed24

Browse files
committed
chore: update readme and example
1 parent baa5645 commit 66fed24

File tree

4 files changed

+23
-53
lines changed

4 files changed

+23
-53
lines changed

README.md

+12-44
Original file line numberDiff line numberDiff line change
@@ -349,46 +349,22 @@ Consequently, to associate a callback function, it becomes a requirement to incl
349349
`Unrpc` is provided to address this issue, enabling support for Typed RPC starting from the **protocol layer**:
350350

351351
```ts
352-
import { Unrpc } from 'unport';
353-
354352
// "parentPort" is a Port defined based on Unport in the previous example.
355353
const parent = new Unrpc(parentPort);
356354

357355
// Implementing an RPC method.
358-
parent.implement('callFoo', request => ({
359-
user: `parent (${request.id})`,
356+
parent.implement('getParentInfo', request => ({
357+
id: 'parent',
358+
from: request.user,
360359
}));
361-
362-
// Emit a SYN event.
363-
parent.port.postMessage('syn', { pid: 'parent' });
364-
365-
// Listen for the ACK message.
366-
parent.port.onMessage('ack', async payload => {
367-
// Call an RPC method as defined by the "child" port.
368-
const response = await parent.call('getChildInfo', {
369-
name: 'parent',
370-
});
371-
});
372360
```
373361

374362
The implementation on the `child` side is as follows:
375363

376364
```ts
377-
import { Unrpc } from 'unport';
378-
379365
// "parentPort" is a Port also defined based on Unport.
380366
const child = new Unrpc(childPort);
381-
382-
child.implement('getChildInfo', request => ({
383-
clientKey: `[child] ${request.name}`,
384-
}));
385-
386-
// Listen for the SYN message.
387-
child.port.onMessage('syn', async payload => {
388-
const response = await child.call('getInfo', { id: '<child>' });
389-
// Acknowledge the SYN event.
390-
child.port.postMessage('ack', { pid: 'child' });
391-
});
367+
const response = await child.call('getParentInfo', { user: "child" }); // => { id: "parent", from: "child" }
392368
```
393369

394370
The types are defined as such:
@@ -398,25 +374,13 @@ import { Unport } from 'unport';
398374

399375
export type Definition = {
400376
parent2child: {
401-
syn: {
402-
pid: string;
403-
};
404-
getInfo__callback: {
405-
user: string;
377+
getParentInfo__callback: {
378+
content: string;
406379
};
407-
getChildInfo: {
408-
name: string;
409-
}
410380
};
411381
child2parent: {
412-
getInfo: {
413-
id: string;
414-
};
415-
getChildInfo__callback: {
416-
clientKey: string;
417-
};
418-
ack: {
419-
pid: string;
382+
getParentInfo: {
383+
user: string;
420384
};
421385
};
422386
};
@@ -427,6 +391,10 @@ export type ParentPort = Unport<Definition, 'parent'>;
427391

428392
In comparison to Unport, the only new concept to grasp is that the RPC response message key must end with `__callback`. Other than that, no additional changes are necessary! `Unrpc` also offers comprehensive type inference based on this convention; for instance, you won't be able to implement an RPC method that is meant to serve as a response.
429393

394+
> [!NOTE]
395+
> You can find the full code example here: [child-process-rpc](https://github.com/web-infra-dev/unport/tree/main/examples/child-process-rpc).
396+
>
397+
430398
## 🤝 Contributing
431399

432400
Contributions, issues and feature requests are welcome!

examples/child-process-rpc/child.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ childPort.implementChannel({
1919
// 3. Initialize a rpc client
2020
const childRpcClient = new Unrpc(childPort);
2121
childRpcClient.implement('getChildInfo', request => ({
22-
clientKey: `[child] ${request.name}`,
22+
childId: 'child_123',
2323
}));
2424
childRpcClient.port.onMessage('syn', async payload => {
2525
console.log('[child] [event] [syn] [result]', payload);
26-
const response = await childRpcClient.call('getInfo', { id: '<child>' });
26+
const response = await childRpcClient.call('getParentInfo', { user: 'child' });
2727
console.log('[child] [rpc] [getInfo] [response]', response);
2828
childPort.postMessage('ack', { pid: 'child' });
2929
});

examples/child-process-rpc/parent.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ parentPort.implementChannel({
2626
// 3. Initialize a rpc client from port.
2727
const parentRpcClient = new Unrpc(parentPort);
2828

29-
parentRpcClient.implement('getInfo', request => ({
30-
user: `parent (${request.id})`,
29+
parentRpcClient.implement('getParentInfo', request => ({
30+
from: request.user,
31+
parentId: 'parent123',
3132
}));
3233
parentRpcClient.port.postMessage('syn', { pid: 'parent' });
3334
parentRpcClient.port.onMessage('ack', async payload => {

examples/child-process-rpc/port.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ export type Definition = {
66
syn: {
77
pid: string;
88
};
9-
getInfo__callback: {
10-
user: string;
9+
getParentInfo__callback: {
10+
parentId: string;
11+
from: string;
1112
};
1213
getChildInfo: {
1314
name: string;
1415
}
1516
};
1617
child2parent: {
17-
getInfo: {
18-
id: string;
18+
getParentInfo: {
19+
user: string;
1920
};
2021
getChildInfo__callback: {
21-
clientKey: string;
22+
childId: string;
2223
};
2324
ack: {
2425
pid: string;

0 commit comments

Comments
 (0)