|
2 | 2 |
|
3 | 3 | ## Parent |
4 | 4 | ```ts |
5 | | -import { getParent } from 'https://deno.land/x/worker_ionic/mod.ts'; |
| 5 | +import { getParentWorker } from 'https://deno.land/x/worker_ionic/mod.ts'; |
6 | 6 |
|
7 | 7 | // load worker |
8 | | -const parent = getParent(new URL('./worker.ts', import.meta.url).href) |
| 8 | +const worker = getParentWorker(new URL('./worker.ts', import.meta.url).href) |
9 | 9 |
|
10 | 10 | // listen to worker events |
11 | | -parent.on('ping', (data) => { |
| 11 | +const event = worker.on('ping', (data) => { |
12 | 12 | console.log(data) |
13 | 13 | }) |
| 14 | +// remove event listener |
| 15 | +worker.remove('ping', event); |
| 16 | +// emit to worker |
| 17 | +worker.emit('channel', { potatoes: 'smashed' }); |
14 | 18 | ``` |
15 | 19 |
|
16 | 20 |
|
17 | 21 | ## Child |
18 | 22 | ```ts |
19 | | -const client = getClientSocket('localhost:1994', ['session', 'token']); |
20 | | -client.on('connected', () => { |
21 | | - // client is connected |
22 | | - |
23 | | - // listen to a channel |
24 | | - client.on('ping', (message) => { |
25 | | - // client emit message |
26 | | - client.emit('pong', { date: Date.now() }) |
27 | | - }); |
28 | | -}); |
29 | | -client.on('error', () => { |
30 | | - // client error |
31 | | -}); |
32 | | -client.on('disconnected', () => { |
33 | | - // client is disconnected |
34 | | -}); |
| 23 | +import { getChildWorker } from 'https://deno.land/x/worker_ionic/mod.ts'; |
| 24 | + |
| 25 | +const worker = getChildWorker(); |
| 26 | + |
| 27 | +// listen to parent events |
| 28 | +const event = worker.on('ping', (data) => { |
| 29 | + console.log(data) |
| 30 | +}) |
| 31 | +// remove event listener |
| 32 | +worker.remove('ping', event); |
| 33 | +// emit to parent |
| 34 | +worker.emit('channel', { potatoes: 'smashed' }); |
35 | 35 | ``` |
0 commit comments