An encrypted personal cloud built on the AT Protocol.
at main 27 lines 764 B view raw
1// Shared crypto worker singleton. 2// One Comlink-wrapped WASM worker for the entire app. 3 4import { wrap, type Remote } from "comlink"; 5import type { CryptoApi } from "@/workers/crypto.worker"; 6 7function createWorker(): Remote<CryptoApi> { 8 const raw = new Worker(new URL("../workers/crypto.worker.ts", import.meta.url), { 9 type: "module", 10 }); 11 raw.addEventListener("error", (e) => { 12 console.error("[worker] error:", e.message, e.filename, e.lineno); 13 }); 14 return wrap<CryptoApi>(raw); 15} 16 17const memo = /* @__PURE__ */ (() => { 18 const ref = { current: null as Remote<CryptoApi> | null }; 19 return () => { 20 ref.current ??= createWorker(); 21 return ref.current; 22 }; 23})(); 24 25export function getCryptoWorker(): Remote<CryptoApi> { 26 return memo(); 27}