Add initial webui
This commit is contained in:
91
webui/src/utils/wasmLoader.ts
Normal file
91
webui/src/utils/wasmLoader.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
// WASM loader for Rhai CodeMirror mode
|
||||
import init, {
|
||||
RhaiMode,
|
||||
init_codemirror_pass,
|
||||
} from "@/wasm/rhai-codemirror/rhai_codemirror.js";
|
||||
|
||||
let wasmInitialized = false;
|
||||
let wasmModule: any = null;
|
||||
let wasmLoadPromise: Promise<any> | null = null;
|
||||
|
||||
export const loadRhaiWasm = async () => {
|
||||
if (wasmInitialized) {
|
||||
return wasmModule;
|
||||
}
|
||||
if (wasmLoadPromise) {
|
||||
return wasmLoadPromise;
|
||||
}
|
||||
|
||||
wasmLoadPromise = (async () => {
|
||||
try {
|
||||
// Initialize the WASM module
|
||||
wasmModule = await init();
|
||||
wasmInitialized = true;
|
||||
|
||||
return wasmModule;
|
||||
} catch (error) {
|
||||
console.error("Failed to load Rhai WASM module:", error);
|
||||
wasmLoadPromise = null; // Reset on error
|
||||
throw error;
|
||||
}
|
||||
})();
|
||||
|
||||
return wasmLoadPromise;
|
||||
};
|
||||
|
||||
export const initRhaiMode = (CodeMirror: any) => {
|
||||
if (!wasmInitialized || !wasmModule) {
|
||||
throw new Error("WASM module not loaded. Call loadRhaiWasm() first.");
|
||||
}
|
||||
|
||||
// Initialize CodeMirror Pass for the WASM module
|
||||
init_codemirror_pass(CodeMirror.Pass);
|
||||
|
||||
// Define the Rhai mode using the WASM-based RhaiMode
|
||||
CodeMirror.defineMode("rhai", (config: any) => {
|
||||
return new RhaiMode(config.indentUnit || 4);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Function to preload all WASM modules used by the application
|
||||
export const loadAllWasm = async (): Promise<void> => {
|
||||
try {
|
||||
|
||||
await loadRhaiWasm();
|
||||
|
||||
// Load Script Runner WASM by creating and immediately terminating a worker
|
||||
const worker = new Worker(
|
||||
new URL("../lib/worker_bulk.ts", import.meta.url)
|
||||
);
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
worker.terminate();
|
||||
reject(new Error("Script runner WASM load timeout"));
|
||||
}, 10000);
|
||||
|
||||
worker.postMessage({ type: "init" });
|
||||
worker.onmessage = (event) => {
|
||||
if (event.data.type === "ready") {
|
||||
clearTimeout(timeout);
|
||||
worker.terminate();
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
|
||||
worker.onerror = (error) => {
|
||||
clearTimeout(timeout);
|
||||
worker.terminate();
|
||||
reject(error);
|
||||
};
|
||||
});
|
||||
|
||||
console.log("All WASM modules loaded successfully");
|
||||
} catch (error) {
|
||||
console.error("Failed to load WASM modules:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export { RhaiMode };
|
||||
Reference in New Issue
Block a user