ente/packages/shared/crypto/index.ts

28 lines
864 B
TypeScript
Raw Normal View History

2023-11-02 07:19:01 +00:00
import { Remote } from 'comlink';
import { DedicatedCryptoWorker } from './internal/crypto.worker';
import { ComlinkWorker } from '../worker/comlinkWorker';
class ComlinkCryptoWorker {
private comlinkWorkerInstance:
| Promise<Remote<DedicatedCryptoWorker>>
| undefined;
async getInstance() {
if (!this.comlinkWorkerInstance) {
const comlinkWorker = getDedicatedCryptoWorker();
this.comlinkWorkerInstance = comlinkWorker.remote;
}
return this.comlinkWorkerInstance;
}
}
export const getDedicatedCryptoWorker = () => {
const cryptoComlinkWorker = new ComlinkWorker<typeof DedicatedCryptoWorker>(
'ente-crypto-worker',
new Worker(new URL('worker/crypto.worker.ts', import.meta.url))
);
return cryptoComlinkWorker;
};
export default new ComlinkCryptoWorker();