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
5 changed files
with
233 additions
and
11 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 '../../src'; | ||
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: `${request.name} ${request.path}`, | ||
})); | ||
const response = 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,30 @@ | ||
import { join } from 'path'; | ||
import { fork } from 'child_process'; | ||
import { Unport, ChannelMessage } from '../../src'; | ||
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); | ||
}); | ||
}, | ||
}); | ||
|
||
// 3. Initialize a rpc client | ||
async function main() { | ||
const parentRpc = parentPort.rpc(); | ||
parentRpc.implement('getInfo', request => ({ | ||
user: `parent (${request.id})`, | ||
})); | ||
} | ||
|
||
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,25 @@ | ||
/* eslint-disable camelcase */ | ||
import { Unport } from '../../src'; | ||
|
||
export type Definition = { | ||
parent2child: { | ||
getInfo__CALLBACK: { | ||
user: string; | ||
}; | ||
getChildInfo: { | ||
name: string; | ||
path: 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