-
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.
feat: support publish async from worker (#14)
* feat: support publish async from worker * test: add timeout case * docs: update
- Loading branch information
1 parent
18d00dc
commit 8592679
Showing
11 changed files
with
316 additions
and
16 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
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,24 @@ | ||
import { ChildProcessEventBus } from '../../src'; | ||
|
||
async function createWorker() { | ||
const bus = new ChildProcessEventBus({ | ||
isWorker: true, | ||
}); | ||
|
||
await bus.start(); | ||
|
||
// 在子进程中发送异步消息到主进程 | ||
const result = await bus.publishAsync<{data: string}>({ | ||
data: 'request from child', | ||
}); | ||
|
||
if (result.data === 'response from main') { | ||
bus.publish({ | ||
data: 'ok', | ||
}); | ||
} | ||
} | ||
|
||
createWorker().then(() => { | ||
console.log('Child process is ready'); | ||
}); |
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 { ChildProcessEventBus } from '../../src'; | ||
|
||
async function createWorker() { | ||
const bus = new ChildProcessEventBus({ | ||
isWorker: true, | ||
}); | ||
|
||
await bus.start(); | ||
|
||
try { | ||
// 设置较短的超时时间 | ||
await bus.publishAsync<{data: string}>({ | ||
data: 'request from child', | ||
}, { | ||
timeout: 1000 // 1秒超时 | ||
}); | ||
} catch (err) { | ||
// 发送超时错误给主进程以验证 | ||
bus.publish({ | ||
error: { | ||
message: err.message | ||
} | ||
}); | ||
} | ||
} | ||
|
||
createWorker().then(() => { | ||
console.log('Child process is ready'); | ||
}); |
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,24 @@ | ||
import { LocalEventBus } from '../../src'; | ||
|
||
async function createWorker() { | ||
const bus = new LocalEventBus({ | ||
isWorker: true, | ||
}); | ||
|
||
await bus.start(); | ||
|
||
// 在子进程中发送异步消息到主进程 | ||
const result = await bus.publishAsync<{data: string}>({ | ||
data: 'request from child', | ||
}); | ||
|
||
if (result.data === 'response from main') { | ||
bus.publish({ | ||
data: 'ok', | ||
}); | ||
} | ||
} | ||
|
||
createWorker().then(() => { | ||
console.log('Local worker is ready'); | ||
}); |
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 { LocalEventBus } from '../../src'; | ||
|
||
async function createWorker() { | ||
const bus = new LocalEventBus({ | ||
isWorker: true, | ||
}); | ||
|
||
await bus.start(); | ||
|
||
try { | ||
// 设置较短的超时时间 | ||
await bus.publishAsync<{data: string}>({ | ||
data: 'request from child', | ||
}, { | ||
timeout: 1000 // 1秒超时 | ||
}); | ||
} catch (err) { | ||
// 发送超时错误给主进程以验证 | ||
bus.publish({ | ||
error: { | ||
message: err.message | ||
} | ||
}); | ||
} | ||
} | ||
|
||
createWorker().then(() => { | ||
console.log('Local worker is ready'); | ||
}); |
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,24 @@ | ||
import { ThreadEventBus } from '../../src'; | ||
|
||
async function createWorker() { | ||
const bus = new ThreadEventBus({ | ||
isWorker: true, | ||
}); | ||
|
||
await bus.start(); | ||
|
||
// 在子进程中发送异步消息到主进程 | ||
const result = await bus.publishAsync<{data: string}>({ | ||
data: 'request from child', | ||
}); | ||
|
||
if (result.data === 'response from main') { | ||
bus.publish({ | ||
data: 'ok', | ||
}); | ||
} | ||
} | ||
|
||
createWorker().then(() => { | ||
console.log('Thread worker is ready'); | ||
}); |
Oops, something went wrong.