atmosphere explorer

use worker to validate logs

handle.invalid 72e12a98 2c8c3e13

verified
+24 -14
+13 -14
src/views/logs.tsx
··· 3 3 defs, 4 4 IndexedEntry, 5 5 IndexedEntryLog, 6 - processIndexedEntryLog, 7 6 } from "@atcute/did-plc"; 8 - import { createEffect, createResource, createSignal, For, Show } from "solid-js"; 7 + import { createEffect, createResource, createSignal, For, onCleanup, Show } from "solid-js"; 9 8 import { localDateFromTimestamp } from "../utils/date.js"; 10 9 import { createOperationHistory, DiffEntry, groupBy } from "../utils/plc-logs.js"; 10 + import PlcValidateWorker from "../workers/plc-validate.ts?worker"; 11 11 import { plcDirectory } from "./settings.jsx"; 12 12 13 13 type PlcEvent = "handle" | "rotation_key" | "service" | "verification_method"; ··· 32 32 return Array.from(groupBy(opHistory, (item) => item.orig)); 33 33 }; 34 34 35 - const validateLog = async (logs: IndexedEntryLog) => { 36 - try { 37 - await processIndexedEntryLog(props.did as any, logs); 38 - setValidLog(true); 39 - } catch (e) { 40 - console.error(e); 41 - setValidLog(false); 42 - } 43 - }; 44 - 45 35 const [plcOps] = 46 36 createResource<[IndexedEntry<CompatibleOperationOrTombstone>, DiffEntry[]][]>(fetchPlcLogs); 47 37 38 + let worker: Worker | undefined; 39 + onCleanup(() => worker?.terminate()); 40 + 48 41 createEffect(() => { 49 42 const logs = rawLogs(); 50 43 if (logs) { 51 44 setValidLog(undefined); 52 - // Defer validation to next tick to avoid blocking rendering 53 - setTimeout(() => validateLog(logs), 0); 45 + worker?.terminate(); 46 + worker = new PlcValidateWorker(); 47 + worker.onmessage = (e: MessageEvent<{ valid: boolean }>) => { 48 + setValidLog(e.data.valid); 49 + worker?.terminate(); 50 + worker = undefined; 51 + }; 52 + worker.postMessage({ did: props.did, logs }); 54 53 } 55 54 }); 56 55
+11
src/workers/plc-validate.ts
··· 1 + import { processIndexedEntryLog } from "@atcute/did-plc"; 2 + 3 + self.onmessage = async (e: MessageEvent<{ did: string; logs: any }>) => { 4 + const { did, logs } = e.data; 5 + try { 6 + await processIndexedEntryLog(did as any, logs); 7 + self.postMessage({ valid: true }); 8 + } catch { 9 + self.postMessage({ valid: false }); 10 + } 11 + };