♻️ Some review/improvements

This commit is contained in:
Nicolas Lepage 2020-12-24 12:38:29 +01:00
parent da00fd7bd1
commit 45dd443a09
No known key found for this signature in database
GPG Key ID: B0879E35E66D8F6F
3 changed files with 23 additions and 8 deletions

View File

@ -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({

View File

@ -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
}
}

View File

@ -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, "/")