-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use web worker to decode (#12)
- Loading branch information
1 parent
971014b
commit 4c20f75
Showing
6 changed files
with
132 additions
and
17 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,49 @@ | ||
import type { EncodedBlock } from 'luby-transform' | ||
import type { DecoderWorkerFunctions } from './workers/decode' | ||
import { createBirpc } from 'birpc' | ||
import DecodeWorkerConstructor from './workers/decode?worker' | ||
|
||
export function createDecodeWorker() { | ||
const worker = new DecodeWorker() | ||
|
||
const rpc = Object.assign(createBirpc<DecoderWorkerFunctions>({}, { | ||
post: worker.decodeWorker.postMessage.bind(worker.decodeWorker), | ||
on: fn => worker.decodeWorker.addEventListener('message', event => fn(event.data)), | ||
}), { | ||
worker, | ||
dispose() { | ||
worker.decodeWorker.terminate() | ||
}, | ||
}) | ||
|
||
return rpc | ||
} | ||
|
||
class DecodeWorker { | ||
decodeWorker: Worker | ||
constructor() { | ||
this.decodeWorker = new DecodeWorkerConstructor() | ||
} | ||
|
||
initDecoder(data?: EncodedBlock[]) { | ||
this.decodeWorker.postMessage({ type: 'createDecoder', data }) | ||
} | ||
|
||
addBlock(data: EncodedBlock) { | ||
this.decodeWorker.postMessage({ type: 'addBlock', data }) | ||
} | ||
|
||
onDecoded(callback: (data: Uint8Array | undefined) => void) { | ||
const eventFn = (event: MessageEvent) => { | ||
const { type, data } = event.data | ||
if (type === 'decoded') { | ||
callback(data) | ||
} | ||
} | ||
this.decodeWorker.addEventListener('message', eventFn) | ||
|
||
return () => { | ||
this.decodeWorker.removeEventListener('message', eventFn) | ||
} | ||
} | ||
} |
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,49 @@ | ||
import type { EncodedBlock, LtDecoder } from 'luby-transform' | ||
import { createBirpc } from 'birpc' | ||
import { createDecoder } from 'luby-transform' | ||
|
||
let decoder: LtDecoder | ||
|
||
const workerFunctions = { | ||
createDecoder(data?: EncodedBlock[]) { | ||
decoder = createDecoder(data) | ||
}, | ||
isInitialized() { | ||
return !!decoder | ||
}, | ||
addBlock(...args: Parameters<LtDecoder['addBlock']>) { | ||
checkDecoder() | ||
return decoder.addBlock(...args) | ||
}, | ||
propagateDecoded(...args: Parameters<LtDecoder['propagateDecoded']>) { | ||
checkDecoder() | ||
decoder.propagateDecoded(...args) | ||
}, | ||
getDecoded(...args: Parameters<LtDecoder['getDecoded']>) { | ||
checkDecoder() | ||
return decoder.getDecoded(...args) | ||
}, | ||
getStatus() { | ||
checkDecoder() | ||
return { | ||
decodedCount: decoder.decodedCount, | ||
encodedCount: decoder.encodedCount, | ||
meta: decoder.meta, | ||
decodedData: decoder.decodedData, | ||
encodedBlocks: decoder.encodedBlocks, | ||
} | ||
}, | ||
} | ||
|
||
export type DecoderWorkerFunctions = typeof workerFunctions | ||
|
||
createBirpc(workerFunctions, { | ||
post: globalThis.postMessage, | ||
on: fn => globalThis.onmessage = event => fn(event.data), | ||
}) | ||
|
||
function checkDecoder() { | ||
if (!decoder) { | ||
throw new Error('Decoder not initialized') | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ dictionaryDefinitions: [] | |
dictionaries: [] | ||
words: | ||
- Attributify | ||
- birpc | ||
- composables | ||
- luby | ||
- Nuxt | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.