upgrades
This commit is contained in:
@@ -3,22 +3,31 @@ import init, { RhaiMode, init_codemirror_pass } from '@/wasm/rhai-codemirror/rha
|
||||
|
||||
let wasmInitialized = false;
|
||||
let wasmModule: any = null;
|
||||
let wasmLoadPromise: Promise<any> | null = null;
|
||||
|
||||
export const loadRhaiWasm = async () => {
|
||||
if (wasmInitialized) {
|
||||
return wasmModule;
|
||||
}
|
||||
|
||||
try {
|
||||
// Initialize the WASM module
|
||||
wasmModule = await init();
|
||||
wasmInitialized = true;
|
||||
|
||||
return wasmModule;
|
||||
} catch (error) {
|
||||
console.error('Failed to load Rhai WASM module:', error);
|
||||
throw error;
|
||||
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) => {
|
||||
@@ -35,4 +44,41 @@ export const initRhaiMode = (CodeMirror: any) => {
|
||||
});
|
||||
};
|
||||
|
||||
// Function to preload all WASM modules used by the application
|
||||
export const loadAllWasm = async (): Promise<void> => {
|
||||
try {
|
||||
// Load Rhai CodeMirror WASM
|
||||
await loadRhaiWasm();
|
||||
|
||||
// Load Script Runner WASM by creating and immediately terminating a worker
|
||||
const worker = new Worker(new URL('../lib/script-runner.worker.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