mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-04-11 21:29:26 +02:00
add timeout when loading nostr-wasm
This commit is contained in:
parent
958a8506f4
commit
c136302c2c
@ -1,5 +1,6 @@
|
||||
import { NostrEvent, VerifiedEvent, verifiedSymbol, verifyEvent } from "nostr-tools";
|
||||
import { logger } from "../../helpers/debug";
|
||||
import { setNostrWasm, verifyEvent as wasmVerifyEvent } from "nostr-tools/wasm";
|
||||
|
||||
const localStorageKey = "verify-event-method";
|
||||
|
||||
@ -12,6 +13,26 @@ export function fakeVerifyEvent(event: NostrEvent): event is VerifiedEvent {
|
||||
return (event[verifiedSymbol] = true);
|
||||
}
|
||||
|
||||
function loadWithTimeout() {
|
||||
return new Promise<typeof verifyEvent>((res, rej) => {
|
||||
const timeout = setTimeout(() => {
|
||||
log("Timeout");
|
||||
rej(new Error("Timeout"));
|
||||
}, 5_000);
|
||||
|
||||
return import("nostr-wasm").then(({ initNostrWasm }) => {
|
||||
log("Initializing WebAssembly");
|
||||
|
||||
return initNostrWasm().then((nw) => {
|
||||
clearTimeout(timeout);
|
||||
setNostrWasm(nw);
|
||||
res(wasmVerifyEvent);
|
||||
return wasmVerifyEvent;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
selectedMethod = localStorage.getItem(localStorageKey) ?? "default";
|
||||
|
||||
@ -19,7 +40,7 @@ try {
|
||||
case "wasm":
|
||||
if (!("WebAssembly" in window)) throw new Error("WebAssembly not supported");
|
||||
log("Loading WebAssembly module");
|
||||
verifyEventMethod = alwaysVerify = (await import("./wasm")).default;
|
||||
verifyEventMethod = alwaysVerify = await loadWithTimeout();
|
||||
log("Loaded");
|
||||
break;
|
||||
case "none":
|
||||
@ -37,6 +58,7 @@ try {
|
||||
console.error("Failed to initialize event verification method, falling back to default");
|
||||
console.log(error);
|
||||
|
||||
localStorage.setItem(localStorageKey, "default");
|
||||
verifyEventMethod = alwaysVerify = verifyEvent;
|
||||
}
|
||||
|
||||
|
@ -1,7 +0,0 @@
|
||||
import { setNostrWasm, verifyEvent } from "nostr-tools/wasm";
|
||||
import { initNostrWasm } from "nostr-wasm";
|
||||
|
||||
const wasm = await initNostrWasm();
|
||||
setNostrWasm(wasm);
|
||||
|
||||
export default verifyEvent;
|
Loading…
x
Reference in New Issue
Block a user