From 45dd443a09a6da8b8de5e6b2573b460da3a40c8b Mon Sep 17 00:00:00 2001 From: Nicolas Lepage <19571875+nlepage@users.noreply.github.com> Date: Thu, 24 Dec 2020 12:38:29 +0100 Subject: [PATCH] :recycle: Some review/improvements --- index.js | 2 +- internal/whutil/promise.go | 27 +++++++++++++++++++++------ serve.go | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 5b378b6..a92d44a 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ window.wasmhttp = { register: async (wasm, { scope, base = '', swUrl = 'sw.js', args = [] } = {}) => { const options = {} if (scope) options.scope = scope - //FIXME register once (beware of changing scope ?) + // FIXME register once (beware of changing scope ?) const registration = await navigator.serviceWorker.register(swUrl, options) await navigator.serviceWorker.ready registration.active.postMessage({ diff --git a/internal/whutil/promise.go b/internal/whutil/promise.go index e3c0f7a..3e95959 100644 --- a/internal/whutil/promise.go +++ b/internal/whutil/promise.go @@ -25,14 +25,29 @@ func NewPromise(cb func(resolve PromiseResolve, reject PromiseReject)) Promise { } // Await waits for the Promise to be resolved and returns the value -func (p Promise) Await() js.Value { - ch := make(chan js.Value) +func (p Promise) Await() (js.Value, error) { + resCh := make(chan js.Value) var then js.Func then = js.FuncOf(func(_ js.Value, args []js.Value) interface{} { - defer then.Release() - ch <- args[0] + resCh <- args[0] return nil }) - p.Call("then", then) - return <-ch + defer then.Release() + + errCh := make(chan error) + var catch js.Func + catch = js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + errCh <- js.Error{args[0]} + return nil + }) + defer catch.Release() + + p.Call("then", then).Call("catch", catch) + + select { + case res := <-resCh: + return res, nil + case err := <-errCh: + return js.Undefined(), err + } } diff --git a/serve.go b/serve.go index 2dcf77f..c424565 100644 --- a/serve.go +++ b/serve.go @@ -22,7 +22,7 @@ func Serve(handler http.Handler) func() { path = path + "/" } - if path != "" { + if path != "" { // FIXME always true since / suffix is added to path prefix := os.Getenv("WASMHTTP_PATH") for strings.HasSuffix(prefix, "/") { prefix = strings.TrimSuffix(prefix, "/")