Skip to content

Commit

Permalink
chore: update readme and example
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Jun 4, 2024
1 parent baa5645 commit 66fed24
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 53 deletions.
56 changes: 12 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<child>' });
// 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:
Expand All @@ -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;
};
};
};
Expand All @@ -427,6 +391,10 @@ export type ParentPort = Unport<Definition, 'parent'>;

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!
Expand Down
4 changes: 2 additions & 2 deletions examples/child-process-rpc/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<child>' });
const response = await childRpcClient.call('getParentInfo', { user: 'child' });
console.log('[child] [rpc] [getInfo] [response]', response);
childPort.postMessage('ack', { pid: 'child' });
});
5 changes: 3 additions & 2 deletions examples/child-process-rpc/parent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
11 changes: 6 additions & 5 deletions examples/child-process-rpc/port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 66fed24

Please sign in to comment.