fix(js): fix hotcode dependencies, allow wasm to init when browser unfocused

pull/5/head
ALI Hamza 2021-03-22 10:50:57 +07:00
parent 421c331333
commit 08c04bf325
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
2 changed files with 18 additions and 11 deletions

@ -7,7 +7,6 @@ const maxTime = 3 * 1000;
const bridge = g.__go_wasm__;
/**
* Wrapper is used by Go to run all Go functions in JS.
* Go functions always return an object of the following spec:
@ -25,28 +24,33 @@ function wrapper(goFunc) {
return result.result;
}
}
bridge.__wrapper__ = wrapper
function sleep() {
return new Promise(requestAnimationFrame);
return new Promise((res) => {
requestAnimationFrame(() => res())
setTimeout(() => {
res()
}, 50);
});
}
export default function (getBytes) {
let proxy;
async function init() {
bridge.__wrapper__ = wrapper
const go = new g.Go();
let bytes = await getBytes;
let result = await WebAssembly.instantiate(bytes, go.importObject);
go.run(result.instance);
bridge.__proxy__ = proxy
setTimeout(() => {
if (bridge.__ready__ !== true) {
console.warn("Golang Wasm Bridge (__go_wasm__.__ready__) still not true after max time");
}
}, maxTime);
}
init();
setTimeout(() => {
if (bridge.__ready__ !== true) {
console.warn("Golang Wasm Bridge (__go_wasm__.__ready__) still not true after max time");
}
}, maxTime);
proxy = new Proxy(
@ -75,5 +79,6 @@ export default function (getBytes) {
}
);
bridge.__proxy__ = proxy
return proxy;
}

@ -43,11 +43,14 @@ module.exports = function (source) {
}
modDir = path.join(modDir, "..");
}
if (!found) {
return cb(new Error("Could not find go.mod in any parent directory of " + this.resourcePath));
}
// Having context dependency before compilation means if any file apart from the imported one fails in
// compilation, the updates are still watched.
this.addContextDependency(modDir);
const wasmOrigPath = path.join(process.env.GOROOT, "misc", "wasm", "wasm_exec.js");
const wasmSavePath = path.join(__dirname, 'wasm_exec.js');
const errorPaths = ["\t" + wasmOrigPath, "\t" + wasmSavePath];
@ -73,7 +76,6 @@ module.exports = function (source) {
const emitPath = path.basename(outFile);
this.emitFile(emitPath, contents);
this.addContextDependency(modDir);
cb(null,
`require('!${wasmSavePath}');