Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (45 loc) · 1.45 KB
/
Copy pathindex.js
File metadata and controls
54 lines (45 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import createManifoldModule from "./vendor/manifold.js"
import embeddedWasmBase64 from "./vendor/manifold.wasm.base64.js"
let manifoldModule = null
let manifoldModulePromise = null
let wasmUrl = null
export { createManifoldModule }
export const setWasmUrl = (url) => {
wasmUrl = String(url)
}
const decodeEmbeddedWasm = () => {
const binary = globalThis.atob(embeddedWasmBase64)
const bytes = new Uint8Array(binary.length)
for (let index = 0; index < binary.length; index++) {
bytes[index] = binary.charCodeAt(index)
}
return bytes
}
export const instantiateManifold = async () => {
const options = wasmUrl
? { locateFile: () => wasmUrl }
: {
// Emscripten resolves a filename before consulting wasmBinary. A
// locateFile hook prevents its bundled `new URL(..., import.meta.url)`
// fallback from running when import.meta.url is unavailable.
locateFile: () => "manifold.wasm",
wasmBinary: decodeEmbeddedWasm(),
}
const module = await createManifoldModule(options)
module.setup()
return module
}
export const getManifoldModule = async () => {
if (manifoldModule) return manifoldModule
manifoldModulePromise ??= instantiateManifold()
.then((module) => {
manifoldModule = module
return module
})
.catch((error) => {
manifoldModulePromise = null
throw error
})
return manifoldModulePromise
}
export const getManifoldModuleSync = () => manifoldModule