mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
Merge pull request #83 from syumai/use-wasmimport-for-ready-func
use go:wasmimport for ready func
This commit is contained in:
commit
f97f5eb551
@ -13,7 +13,7 @@ globalThis.tryCatch = (fn) => {
|
||||
} catch(e) {
|
||||
return {
|
||||
error: e,
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,10 +22,16 @@ export function init(m) {
|
||||
}
|
||||
|
||||
async function run() {
|
||||
let ready;
|
||||
const readyPromise = new Promise((resolve) => {
|
||||
globalThis.ready = resolve;
|
||||
ready = resolve;
|
||||
});
|
||||
const instance = new WebAssembly.Instance(mod, {
|
||||
...go.importObject,
|
||||
workers: {
|
||||
ready: () => { ready() }
|
||||
},
|
||||
});
|
||||
const instance = new WebAssembly.Instance(mod, go.importObject);
|
||||
go.run(instance);
|
||||
await readyPromise;
|
||||
}
|
||||
@ -35,7 +41,7 @@ function createRuntimeContext(env, ctx) {
|
||||
env,
|
||||
ctx,
|
||||
connect,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetch(req, env, ctx) {
|
||||
|
@ -71,6 +71,9 @@ func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) {
|
||||
return w.ToJSResponse(), nil
|
||||
}
|
||||
|
||||
//go:wasmimport workers ready
|
||||
func ready()
|
||||
|
||||
// Server serves http.Handler on Cloudflare Workers.
|
||||
// if the given handler is nil, http.DefaultServeMux will be used.
|
||||
func Serve(handler http.Handler) {
|
||||
@ -78,6 +81,6 @@ func Serve(handler http.Handler) {
|
||||
handler = http.DefaultServeMux
|
||||
}
|
||||
httpHandler = handler
|
||||
js.Global().Call("ready")
|
||||
ready()
|
||||
select {}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user