go-app
go-app copied to clipboard
fix cleanArg and rework JSValue(app.Value)
This PR addresses https://github.com/maxence-charriere/go-app/issues/876
- Adds
func JSValue(v Value) any
tojs_nowasm.go
and changes return type on matching function injs_wasm.go
- Fixes cleanArg() where slices and functions were getting passed down incorrectly to syscall/js
- Some basic unit tests in
js_wasm.go
to validate fixes
Any updates?
@qizhanchan
Maxence mentioned here that this and other PRs might possibly be included in V10: https://github.com/maxence-charriere/go-app/issues/875#issuecomment-1788026436
HI @maxence-charriere , do you have any feedback on the comments on this PR?
As I mentioned before, this was designed to deal specifically with syscall/js. I remember being able to deal with promise with the current API. I would rather try help you figure out how to solve that usecase before touching this.
@mlctrez here is a working promise example:
func TestPromise(t *testing.T) {
callback := FuncOf(func(this Value, args []Value) any {
args[0].Invoke("hi")
return nil
})
defer callback.Release()
var wg sync.WaitGroup
wg.Add(1)
promise := Window().Get("Promise").New(callback)
promise.Then(func(v Value) {
fmt.Println(v.String())
wg.Done()
})
wg.Wait()
}
go-app/pkg/app v10 ✗ 1d20h ⚑ ◒
▶ GOARCH=wasm GOOS=js go test -run TestPromise
hi
PASS
ok github.com/maxence-charriere/go-app/v9/pkg/app 0.394s
@maxence-charriere - The PR was to address a few things:
-
"Fixes cleanArg() where slices and functions were getting passed down incorrectly to syscall/js" Not all of the changes to address this made it into https://github.com/maxence-charriere/go-app/pull/907 I commented on what was missing:
-
The other change was an attempt to address the issue raised by https://github.com/maxence-charriere/go-app/issues/876 Very specifically, returning an app.Value ( a promise object ) back to javascript results in
panic: ValueOf: invalid value
The following code demonstrates this:
func (r *Root) OnMount(context app.Context) {
app.Window().Set("goAppPromise", app.FuncOf(func(this app.Value, args []app.Value) any {
callback := app.FuncOf(func(this app.Value, args []app.Value) any {
args[0].Invoke("hi")
return nil
})
return app.Window().Get("Promise").New(callback)
}))
}
func (r *Root) Render() app.UI {
return app.Button().Text("goAppPromise").OnClick(func(ctx app.Context, e app.Event) {
app.Window().Get("goAppPromise").Invoke()
})
}
There might be a better way to convert app.Value to js.Value before it is returned, but I'm not seeing an obvious solution other than what was proposed. This change was just to fix https://github.com/maxence-charriere/go-app/issues/876, so if that's the only place that this has come up then maybe it does not need fixing.
Hey @mlctrez. Took me a bit to figure out but I think https://github.com/maxence-charriere/go-app/pull/920 should solve the problem.
Any chance you can try this and let me know if you find any issues?
Also made a unit test that reproduces your issue: https://github.com/maxence-charriere/go-app/pull/920/files#diff-e781a4648c64c93583360ae1713b104b0b4ac501867795b383b585794695b485R26-R48
@maxence-charriere - this looks like it solves the return value issue and incorporates the cleanArgs changes from this PR.
I've long since removed the code that I used from https://github.com/maxence-charriere/go-app/issues/876
Perhaps @g4bwy can test also?
resolved by https://github.com/maxence-charriere/go-app/pull/920