generated from ulivz/ts-lib-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
317 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Unport, Unrpc, ChannelMessage } from '../../lib'; | ||
import { ChildPort } from './port'; | ||
|
||
// 1. Initialize a port | ||
const childPort: ChildPort = new Unport(); | ||
|
||
// 2. Implement a Channel based on underlying IPC capabilities | ||
childPort.implementChannel({ | ||
send(message) { | ||
process.send && process.send(message); | ||
}, | ||
accept(pipe) { | ||
process.on('message', (message: ChannelMessage) => { | ||
pipe(message); | ||
}); | ||
}, | ||
}); | ||
|
||
// 3. Initialize a rpc client | ||
const childRpcClient = new Unrpc(childPort); | ||
childRpcClient.implement('getChildInfo', request => ({ | ||
clientKey: `[child] ${request.name}`, | ||
})); | ||
childRpcClient.port.onMessage('syn', async payload => { | ||
console.log('[child] [event] [syn] [result]', payload); | ||
const response = await childRpcClient.call('getInfo', { id: '<child>' }); | ||
console.log('[child] [rpc] [getInfo] [response]', response); | ||
childPort.postMessage('ack', { pid: 'child' }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { join } from 'path'; | ||
import { fork } from 'child_process'; | ||
import { Unport, Unrpc, ChannelMessage } from '../../lib'; | ||
import { ParentPort } from './port'; | ||
|
||
// 1. Initialize a port | ||
const parentPort: ParentPort = new Unport(); | ||
|
||
// 2. Implement a Channel based on underlying IPC capabilities | ||
const childProcess = fork(join(__dirname, './child.js')); | ||
parentPort.implementChannel({ | ||
send(message) { | ||
childProcess.send(message); | ||
}, | ||
accept(pipe) { | ||
childProcess.on('message', (message: ChannelMessage) => { | ||
pipe(message); | ||
}); | ||
}, | ||
destroy() { | ||
childProcess.removeAllListeners('message'); | ||
childProcess.kill(); | ||
}, | ||
}); | ||
|
||
// 3. Initialize a rpc client from port. | ||
const parentRpcClient = new Unrpc(parentPort); | ||
|
||
parentRpcClient.implement('getInfo', request => ({ | ||
user: `parent (${request.id})`, | ||
})); | ||
parentRpcClient.port.postMessage('syn', { pid: 'parent' }); | ||
parentRpcClient.port.onMessage('ack', async payload => { | ||
console.log('[parent] [event] [ack] [result]', payload); | ||
const response = await parentRpcClient.call('getChildInfo', { | ||
name: 'parent', | ||
}); | ||
console.log('[parent] [rpc] [getChildInfo] [response]', response); | ||
setTimeout(() => { | ||
console.log('destroy'); | ||
parentPort.destroy(); | ||
}, 1000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* eslint-disable camelcase */ | ||
import { Unport } from '../../lib'; | ||
|
||
export type Definition = { | ||
parent2child: { | ||
syn: { | ||
pid: string; | ||
}; | ||
getInfo__callback: { | ||
user: string; | ||
}; | ||
getChildInfo: { | ||
name: string; | ||
} | ||
}; | ||
child2parent: { | ||
getInfo: { | ||
id: string; | ||
}; | ||
getChildInfo__callback: { | ||
clientKey: string; | ||
}; | ||
ack: { | ||
pid: string; | ||
}; | ||
}; | ||
}; | ||
|
||
export type ChildPort = Unport<Definition, 'child'>; | ||
export type ParentPort = Unport<Definition, 'parent'>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.