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
3 changed files
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ChildProcessChannel, 'child2parent'>; | ||
export type ParentPort = UnPort<ChildProcessChannel, 'parent2child'>; |
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,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)); | ||
}); |
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,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: ' /', | ||
}); | ||
}); |