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

@ -43,11 +43,14 @@ module.exports = function (source) {
} }
modDir = path.join(modDir, ".."); modDir = path.join(modDir, "..");
} }
if (!found) { if (!found) {
return cb(new Error("Could not find go.mod in any parent directory of " + this.resourcePath)); 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 wasmOrigPath = path.join(process.env.GOROOT, "misc", "wasm", "wasm_exec.js");
const wasmSavePath = path.join(__dirname, 'wasm_exec.js'); const wasmSavePath = path.join(__dirname, 'wasm_exec.js');
const errorPaths = ["\t" + wasmOrigPath, "\t" + wasmSavePath]; const errorPaths = ["\t" + wasmOrigPath, "\t" + wasmSavePath];
@ -73,7 +76,6 @@ module.exports = function (source) {
const emitPath = path.basename(outFile); const emitPath = path.basename(outFile);
this.emitFile(emitPath, contents); this.emitFile(emitPath, contents);
this.addContextDependency(modDir);
cb(null, cb(null,
`require('!${wasmSavePath}'); `require('!${wasmSavePath}');