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
305 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 | ||
async function main() { | ||
const childRpc = new Unrpc(childPort); | ||
childRpc.implement('getChildInfo', request => ({ | ||
clientKey: `[child] ${request.name}`, | ||
})); | ||
const response = await childRpc.call('getInfo', { id: '<child>' }); | ||
console.log('[child] [getInfo] [response]', response); | ||
} | ||
|
||
main(); |
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. | ||
async function main() { | ||
const parentRpcClient = new Unrpc(parentPort); | ||
parentRpcClient.implement('getInfo', request => ({ | ||
user: `parent (${request.id})`, | ||
})); | ||
const response = await parentRpcClient.call('getChildInfo', { | ||
name: 'parent', | ||
}); | ||
console.log('[parent] [getChildInfo] [response]', response); | ||
|
||
setTimeout(() => { | ||
console.log('destroy'); | ||
parentPort.destroy(); | ||
}, 1000); | ||
} | ||
|
||
main(); |
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,24 @@ | ||
/* eslint-disable camelcase */ | ||
import { Unport } from '../../lib'; | ||
|
||
export type Definition = { | ||
parent2child: { | ||
getInfo__CALLBACK: { | ||
user: string; | ||
}; | ||
getChildInfo: { | ||
name: string; | ||
} | ||
}; | ||
child2parent: { | ||
getInfo: { | ||
id: string; | ||
}; | ||
getChildInfo__CALLBACK: { | ||
clientKey: 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.