Skip to content

Commit

Permalink
refactor: remove dead codes of it-decode
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSound committed Oct 15, 2024
1 parent 08a3840 commit f405d45
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 40 deletions.
4 changes: 2 additions & 2 deletions app/components/Scan.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -163,7 +163,7 @@ async function updateCameraStatus() {
}
}
const decoderWorker = createDecodeWorker()
const decoderWorker = createLTDecodeWorker()
onUnmounted(() => decoderWorker.dispose())
const decoderStatus = ref<Awaited<ReturnType<typeof decoderWorker.getStatus>>>({
encodedBlocks: new Set(),
Expand Down
44 changes: 7 additions & 37 deletions app/composables/lt-decode.ts
Original file line number Diff line number Diff line change
@@ -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<DecoderWorkerFunctions>({}, {
post: worker.decodeWorker.postMessage.bind(worker.decodeWorker),
on: fn => worker.decodeWorker.addEventListener('message', event => fn(event.data)),
const rpc = Object.assign(createBirpc<LTDecoderWorkerFunctions>({}, {
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)
}
}
}
2 changes: 1 addition & 1 deletion app/composables/workers/lt-decode.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const workerFunctions = {
},
}

export type DecoderWorkerFunctions = typeof workerFunctions
export type LTDecoderWorkerFunctions = typeof workerFunctions

createBirpc(workerFunctions, {
post: globalThis.postMessage,
Expand Down

0 comments on commit f405d45

Please sign in to comment.