feat(js): implement wrapper for interfacing with Go functions in JS

master
ALI Hamza 2021-03-21 20:49:28 +07:00
parent 3b119bd81a
commit da1769920a
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
1 changed files with 24 additions and 7 deletions

@ -7,6 +7,26 @@ 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:
* {
* result: undefined | any // undefined when error is returned, or function returns undefined
* error: Error | undefined // undefined when no error is present
* }
*/
function wrapper(goFunc) {
return (...args) => {
const result = goFunc.apply(undefined, args);
if (result.error instanceof Error) {
throw result.error;
}
return result.result;
}
}
bridge.__wrapper__ = wrapper
function sleep() {
return new Promise(requestAnimationFrame);
}
@ -44,14 +64,11 @@ export default function (getBytes) {
return;
}
const returnObj = bridge[key].apply(undefined, args);
if (returnObj.error instanceof Error) {
return rej(returnObj.error)
try {
res(bridge[key].apply(undefined, args));
} catch (e) {
rej(e)
}
if (returnObj.result) return res(returnObj.result);
return res(returnObj)
})
};
}