feat: get web worker mechanism working (pass file around)

This commit is contained in:
鲁树人
2023-05-08 17:36:10 +01:00
parent 29b169cce6
commit 911ee2a2fa
7 changed files with 150 additions and 6 deletions

View File

@@ -1 +1,18 @@
import { ConcurrentQueue } from '../util/ConcurrentQueue';
import { WorkerClientBus } from '../util/WorkerEventBus';
import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
// TODO: Worker pool?
export const workerClient = new Worker(new URL('./worker', import.meta.url), { type: 'module' });
class DecryptionQueue extends ConcurrentQueue<{ id: string; blobURI: string }> {
constructor(private workerClientBus: WorkerClientBus, maxQueue?: number) {
super(maxQueue);
}
async handler(item: { id: string; blobURI: string }): Promise<void> {
return this.workerClientBus.request(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, item.blobURI);
}
}
export const decryptionQueue = new DecryptionQueue(new WorkerClientBus(workerClient));

View File

@@ -0,0 +1,3 @@
export enum DECRYPTION_WORKER_ACTION_NAME {
DECRYPT = 'DECRYPT',
}

View File

@@ -1,3 +1,12 @@
onmessage = (e) => {
console.log(e.data);
};
import { WorkerServerBus } from '../util/WorkerEventBus';
import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
const bus = new WorkerServerBus();
onmessage = bus.onmessage;
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, async (blobURI) => {
const blob = await fetch(blobURI).then((r) => r.arrayBuffer());
// TODO: Implement decryptor for blob received here.
console.log(blob);
return { hello: true };
});