From 66fed24b0dba7285b81939cbab1e14a5c6e3f832 Mon Sep 17 00:00:00 2001 From: chenhaoli Date: Wed, 5 Jun 2024 01:36:06 +0800 Subject: [PATCH] chore: update readme and example --- README.md | 56 ++++++---------------------- examples/child-process-rpc/child.ts | 4 +- examples/child-process-rpc/parent.ts | 5 ++- examples/child-process-rpc/port.ts | 11 +++--- 4 files changed, 23 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 99a606d..04c578b 100644 --- a/README.md +++ b/README.md @@ -349,46 +349,22 @@ Consequently, to associate a callback function, it becomes a requirement to incl `Unrpc` is provided to address this issue, enabling support for Typed RPC starting from the **protocol layer**: ```ts -import { Unrpc } from 'unport'; - // "parentPort" is a Port defined based on Unport in the previous example. const parent = new Unrpc(parentPort); // Implementing an RPC method. -parent.implement('callFoo', request => ({ - user: `parent (${request.id})`, +parent.implement('getParentInfo', request => ({ + id: 'parent', + from: request.user, })); - -// Emit a SYN event. -parent.port.postMessage('syn', { pid: 'parent' }); - -// Listen for the ACK message. -parent.port.onMessage('ack', async payload => { - // Call an RPC method as defined by the "child" port. - const response = await parent.call('getChildInfo', { - name: 'parent', - }); -}); ``` The implementation on the `child` side is as follows: ```ts -import { Unrpc } from 'unport'; - // "parentPort" is a Port also defined based on Unport. const child = new Unrpc(childPort); - -child.implement('getChildInfo', request => ({ - clientKey: `[child] ${request.name}`, -})); - -// Listen for the SYN message. -child.port.onMessage('syn', async payload => { - const response = await child.call('getInfo', { id: '' }); - // Acknowledge the SYN event. - child.port.postMessage('ack', { pid: 'child' }); -}); +const response = await child.call('getParentInfo', { user: "child" }); // => { id: "parent", from: "child" } ``` The types are defined as such: @@ -398,25 +374,13 @@ import { Unport } from 'unport'; export type Definition = { parent2child: { - syn: { - pid: string; - }; - getInfo__callback: { - user: string; + getParentInfo__callback: { + content: string; }; - getChildInfo: { - name: string; - } }; child2parent: { - getInfo: { - id: string; - }; - getChildInfo__callback: { - clientKey: string; - }; - ack: { - pid: string; + getParentInfo: { + user: string; }; }; }; @@ -427,6 +391,10 @@ export type ParentPort = Unport; 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. +> [!NOTE] +> You can find the full code example here: [child-process-rpc](https://github.com/web-infra-dev/unport/tree/main/examples/child-process-rpc). +> + ## 🤝 Contributing Contributions, issues and feature requests are welcome! diff --git a/examples/child-process-rpc/child.ts b/examples/child-process-rpc/child.ts index 3091255..0a7e18e 100644 --- a/examples/child-process-rpc/child.ts +++ b/examples/child-process-rpc/child.ts @@ -19,11 +19,11 @@ childPort.implementChannel({ // 3. Initialize a rpc client const childRpcClient = new Unrpc(childPort); childRpcClient.implement('getChildInfo', request => ({ - clientKey: `[child] ${request.name}`, + childId: 'child_123', })); childRpcClient.port.onMessage('syn', async payload => { console.log('[child] [event] [syn] [result]', payload); - const response = await childRpcClient.call('getInfo', { id: '' }); + const response = await childRpcClient.call('getParentInfo', { user: 'child' }); console.log('[child] [rpc] [getInfo] [response]', response); childPort.postMessage('ack', { pid: 'child' }); }); diff --git a/examples/child-process-rpc/parent.ts b/examples/child-process-rpc/parent.ts index 986c0c4..cb525f2 100644 --- a/examples/child-process-rpc/parent.ts +++ b/examples/child-process-rpc/parent.ts @@ -26,8 +26,9 @@ parentPort.implementChannel({ // 3. Initialize a rpc client from port. const parentRpcClient = new Unrpc(parentPort); -parentRpcClient.implement('getInfo', request => ({ - user: `parent (${request.id})`, +parentRpcClient.implement('getParentInfo', request => ({ + from: request.user, + parentId: 'parent123', })); parentRpcClient.port.postMessage('syn', { pid: 'parent' }); parentRpcClient.port.onMessage('ack', async payload => { diff --git a/examples/child-process-rpc/port.ts b/examples/child-process-rpc/port.ts index aa20929..8707526 100644 --- a/examples/child-process-rpc/port.ts +++ b/examples/child-process-rpc/port.ts @@ -6,19 +6,20 @@ export type Definition = { syn: { pid: string; }; - getInfo__callback: { - user: string; + getParentInfo__callback: { + parentId: string; + from: string; }; getChildInfo: { name: string; } }; child2parent: { - getInfo: { - id: string; + getParentInfo: { + user: string; }; getChildInfo__callback: { - clientKey: string; + childId: string; }; ack: { pid: string;