MCPcopy Create free account
hub / github.com/ColmapView/Colmapview.github.io / loadColmapWasm

Function loadColmapWasm

src/wasm/init.ts:38–92  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

36 * Falls back to null if WASM is not supported or module fails to load.
37 */
38export async function loadColmapWasm(): Promise<ColmapWasmModule | null> {
39 // Return cached module if available
40 if (cachedModule) {
41 return cachedModule;
42 }
43
44 // Return existing promise if loading is in progress
45 if (modulePromise) {
46 return modulePromise;
47 }
48
49 // Check WASM support
50 if (!isWasmSupported()) {
51 appLogger.warn('WebAssembly is not supported in this browser');
52 return null;
53 }
54
55 // Start loading
56 modulePromise = (async () => {
57 try {
58 // Load WASM module from public directory
59 // Vite doesn't allow importing JS from public/, so we fetch and create a blob URL
60 const baseUrl = import.meta.env.BASE_URL || '/';
61 const wasmJsUrl = `${baseUrl}wasm/colmap_wasm.js`;
62
63 // Fetch the JS file and create a blob URL for dynamic import
64 const response = await fetch(wasmJsUrl);
65 if (!response.ok) {
66 throw new Error(`Failed to fetch WASM loader: ${response.status}`);
67 }
68 const jsText = await response.text();
69 const blob = new Blob([jsText], { type: 'application/javascript' });
70 const blobUrl = URL.createObjectURL(blob);
71
72 // Dynamic import from blob URL
73 const wasmModule = await import(/* @vite-ignore */ blobUrl);
74 URL.revokeObjectURL(blobUrl);
75
76 const createColmapWasm = wasmModule.default as CreateColmapWasm;
77
78 const module = await createColmapWasm({
79 locateFile: (path: string) => `${baseUrl}wasm/${path}`,
80 });
81 cachedModule = module;
82 appLogger.info('colmap-wasm module loaded successfully');
83 return module;
84 } catch (error) {
85 appLogger.warn('Failed to load colmap-wasm module:', error);
86 modulePromise = null;
87 return null;
88 }
89 })();
90
91 return modulePromise;
92}
93
94/**
95 * Get the loaded module synchronously (returns null if not yet loaded)

Callers 1

initializeMethod · 0.90

Calls 2

isWasmSupportedFunction · 0.85
createColmapWasmFunction · 0.85

Tested by

no test coverage detected