|
1 |
| -import type { EncodedBlock } from 'luby-transform' |
2 |
| -import type { DecoderWorkerFunctions } from './workers/lt-decode.worker' |
| 1 | +import type { LTDecoderWorkerFunctions } from './workers/lt-decode.worker' |
3 | 2 | import { createBirpc } from 'birpc'
|
4 |
| -import DecodeWorkerConstructor from './workers/lt-decode.worker?worker' |
| 3 | +import DecodeWorker from './workers/lt-decode.worker?worker' |
5 | 4 |
|
6 |
| -export function createDecodeWorker() { |
| 5 | +export function createLTDecodeWorker() { |
7 | 6 | const worker = new DecodeWorker()
|
8 | 7 |
|
9 |
| - const rpc = Object.assign(createBirpc<DecoderWorkerFunctions>({}, { |
10 |
| - post: worker.decodeWorker.postMessage.bind(worker.decodeWorker), |
11 |
| - on: fn => worker.decodeWorker.addEventListener('message', event => fn(event.data)), |
| 8 | + const rpc = Object.assign(createBirpc<LTDecoderWorkerFunctions>({}, { |
| 9 | + post: worker.postMessage.bind(worker), |
| 10 | + on: fn => worker.addEventListener('message', event => fn(event.data)), |
12 | 11 | }), {
|
13 | 12 | worker,
|
14 | 13 | dispose() {
|
15 |
| - worker.decodeWorker.terminate() |
| 14 | + worker.terminate() |
16 | 15 | },
|
17 | 16 | })
|
18 | 17 |
|
19 | 18 | return rpc
|
20 | 19 | }
|
21 |
| - |
22 |
| -class DecodeWorker { |
23 |
| - decodeWorker: Worker |
24 |
| - constructor() { |
25 |
| - this.decodeWorker = new DecodeWorkerConstructor() |
26 |
| - } |
27 |
| - |
28 |
| - initDecoder(data?: EncodedBlock[]) { |
29 |
| - this.decodeWorker.postMessage({ type: 'createDecoder', data }) |
30 |
| - } |
31 |
| - |
32 |
| - addBlock(data: EncodedBlock) { |
33 |
| - this.decodeWorker.postMessage({ type: 'addBlock', data }) |
34 |
| - } |
35 |
| - |
36 |
| - onDecoded(callback: (data: Uint8Array | undefined) => void) { |
37 |
| - const eventFn = (event: MessageEvent) => { |
38 |
| - const { type, data } = event.data |
39 |
| - if (type === 'decoded') { |
40 |
| - callback(data) |
41 |
| - } |
42 |
| - } |
43 |
| - this.decodeWorker.addEventListener('message', eventFn) |
44 |
| - |
45 |
| - return () => { |
46 |
| - this.decodeWorker.removeEventListener('message', eventFn) |
47 |
| - } |
48 |
| - } |
49 |
| -} |
0 commit comments