2023-05-09 00:22:00 +00:00
|
|
|
import { ConcurrentQueue } from '~/util/ConcurrentQueue';
|
|
|
|
import { WorkerClientBus } from '~/util/WorkerEventBus';
|
2023-05-08 16:36:10 +00:00
|
|
|
import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
|
|
|
|
|
|
|
|
// TODO: Worker pool?
|
2023-05-08 15:23:46 +00:00
|
|
|
export const workerClient = new Worker(new URL('./worker', import.meta.url), { type: 'module' });
|
2023-05-08 16:36:10 +00:00
|
|
|
|
2023-05-13 19:03:32 +00:00
|
|
|
// FIXME: report the error so is obvious to the user.
|
|
|
|
workerClient.onerror = (err) => console.error(err);
|
|
|
|
|
2023-05-08 16:36:10 +00:00
|
|
|
class DecryptionQueue extends ConcurrentQueue<{ id: string; blobURI: string }> {
|
2023-05-14 23:09:26 +00:00
|
|
|
constructor(private workerClientBus: WorkerClientBus<DECRYPTION_WORKER_ACTION_NAME>, maxQueue?: number) {
|
2023-05-08 16:36:10 +00:00
|
|
|
super(maxQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handler(item: { id: string; blobURI: string }): Promise<void> {
|
|
|
|
return this.workerClientBus.request(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, item.blobURI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 23:09:26 +00:00
|
|
|
export const workerClientBus = new WorkerClientBus<DECRYPTION_WORKER_ACTION_NAME>(workerClient);
|
|
|
|
export const decryptionQueue = new DecryptionQueue(workerClientBus);
|