WASM Client

/views/components/WASM.html (833 B)

 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
{% import (
"{{{ .Package }}}/app/util"
"{{{ .Package }}}/assets"
) %}

{% func WASMScript(cfg any) %}
{%s= assets.ScriptElement(`wasm/wasm_exec.js`, true) %}
<script>
document.addEventListener("DOMContentLoaded", function() {
if (!WebAssembly.instantiateStreaming) {
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}

const start = new Date().getTime();
const go = new Go();
WebAssembly.instantiateStreaming(fetch("{%s assets.URL(`wasm/{{{ .Exec }}}.wasm`) %}"), go.importObject).then((result) => {
go.run(result.instance);
wasmInit(new Date().getTime() - start, {%s= util.ToJSONCompact(cfg) %});
});
});
</script>
{% endfunc %}