diff --git a/app/components/Scan.vue b/app/components/Scan.vue index 2a6c5bd..227a6be 100644 --- a/app/components/Scan.vue +++ b/app/components/Scan.vue @@ -4,7 +4,7 @@ import { binaryToBlock, readFileHeaderMetaFromBuffer } from 'luby-transform' import QrScanner from 'qr-scanner' import { useKiloBytesNumberFormat } from '~/composables/intlNumberFormat' -import { createDecodeWorker } from '~/composables/lt-decode' +import { createLTDecodeWorker } from '~/composables/lt-decode' import { useBytesRate } from '~/composables/timeseries' import { CameraSignalStatus } from '~/types' @@ -163,7 +163,7 @@ async function updateCameraStatus() { } } -const decoderWorker = createDecodeWorker() +const decoderWorker = createLTDecodeWorker() onUnmounted(() => decoderWorker.dispose()) const decoderStatus = ref>>({ encodedBlocks: new Set(), diff --git a/app/composables/lt-decode.ts b/app/composables/lt-decode.ts index 92e1557..735f2fa 100644 --- a/app/composables/lt-decode.ts +++ b/app/composables/lt-decode.ts @@ -1,49 +1,19 @@ -import type { EncodedBlock } from 'luby-transform' -import type { DecoderWorkerFunctions } from './workers/lt-decode.worker' +import type { LTDecoderWorkerFunctions } from './workers/lt-decode.worker' import { createBirpc } from 'birpc' -import DecodeWorkerConstructor from './workers/lt-decode.worker?worker' +import DecodeWorker from './workers/lt-decode.worker?worker' -export function createDecodeWorker() { +export function createLTDecodeWorker() { const worker = new DecodeWorker() - const rpc = Object.assign(createBirpc({}, { - post: worker.decodeWorker.postMessage.bind(worker.decodeWorker), - on: fn => worker.decodeWorker.addEventListener('message', event => fn(event.data)), + const rpc = Object.assign(createBirpc({}, { + post: worker.postMessage.bind(worker), + on: fn => worker.addEventListener('message', event => fn(event.data)), }), { worker, dispose() { - worker.decodeWorker.terminate() + worker.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) - } - } -} diff --git a/app/composables/workers/lt-decode.worker.ts b/app/composables/workers/lt-decode.worker.ts index 5d7e6cf..07cbaa9 100644 --- a/app/composables/workers/lt-decode.worker.ts +++ b/app/composables/workers/lt-decode.worker.ts @@ -35,7 +35,7 @@ const workerFunctions = { }, } -export type DecoderWorkerFunctions = typeof workerFunctions +export type LTDecoderWorkerFunctions = typeof workerFunctions createBirpc(workerFunctions, { post: globalThis.postMessage,