From aafb655c01e23aa574046e8cf0d64be8e358509e Mon Sep 17 00:00:00 2001 From: chenhaoli Date: Fri, 17 Nov 2023 00:50:41 +0800 Subject: [PATCH] chore: add `child-process` example --- examples/child-process/channel.ts | 21 +++++++++++++++++++++ examples/child-process/child.ts | 28 ++++++++++++++++++++++++++++ examples/child-process/parent.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 examples/child-process/channel.ts create mode 100644 examples/child-process/child.ts create mode 100644 examples/child-process/parent.ts diff --git a/examples/child-process/channel.ts b/examples/child-process/channel.ts new file mode 100644 index 0000000..1ce30c8 --- /dev/null +++ b/examples/child-process/channel.ts @@ -0,0 +1,21 @@ +import { UnPort } from '../../src'; + +export type ChildProcessChannel = { + parent2child: { + syn: { + pid: string; + }; + body: { + name: string; + path: string; + } + }; + child2parent: { + ack: { + pid: string; + }; + }; +}; + +export type ChildPort = UnPort; +export type ParentPort = UnPort; diff --git a/examples/child-process/child.ts b/examples/child-process/child.ts new file mode 100644 index 0000000..74ee78b --- /dev/null +++ b/examples/child-process/child.ts @@ -0,0 +1,28 @@ +import { defineIntermediatePort, UnPort } from '../../src'; +import { ChildPort } from './channel'; + +// 1. Initialize a port +const port: ChildPort = new UnPort(); + +// 2. Implement a intermediate port based on underlying IPC capabilities +port.implement(() => { + const intermediatePort = defineIntermediatePort({ + postMessage: message => { + process.send && process.send(JSON.stringify(message)); + }, + }); + + process.on('message', (message: string) => { + intermediatePort.onmessage && intermediatePort.onmessage(JSON.parse(message)); + }); + return intermediatePort; +}); + +// 3. Post message +port.onMessage('syn', payload => { + console.log('[child] [syn]', payload.pid); + port.postMessage('ack', { pid: 'child' }); +}); +port.onMessage('body', payload => { + console.log('[child] [body]', JSON.stringify(payload.name)); +}); diff --git a/examples/child-process/parent.ts b/examples/child-process/parent.ts new file mode 100644 index 0000000..5f92e89 --- /dev/null +++ b/examples/child-process/parent.ts @@ -0,0 +1,31 @@ +import { fork } from 'child_process'; +import { defineIntermediatePort, UnPort } from '../../src'; +import { ParentPort } from './channel'; + +// 1. Initialize a port +const port: ParentPort = new UnPort(); + +// 2. Implement a universal port based on underlying IPC capabilities +port.implement(() => { + const childProcess: import('child_process').ChildProcess = fork('./child'); + const intermediatePort = defineIntermediatePort({ + postMessage: message => { + childProcess.send(JSON.stringify(message)); + }, + }); + + childProcess.on('message', (message: string) => { + intermediatePort.onmessage && intermediatePort.onmessage(JSON.parse(message)); + }); + + return intermediatePort; +}); + +// 3. Post message +port.postMessage('syn', { pid: 'parent' }); +port.onMessage('ack', p => { + port.postMessage('body', { + name: 'index', + path: ' /', + }); +});