go-app icon indicating copy to clipboard operation
go-app copied to clipboard

fix cleanArg and rework JSValue(app.Value)

Open mlctrez opened this issue 1 year ago • 8 comments

This PR addresses https://github.com/maxence-charriere/go-app/issues/876

  • Adds func JSValue(v Value) any to js_nowasm.go and changes return type on matching function in js_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

mlctrez avatar Aug 23 '23 23:08 mlctrez

Any updates?

qizhanchan avatar Oct 23 '23 09:10 qizhanchan

@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

mlctrez avatar Oct 31 '23 21:10 mlctrez

HI @maxence-charriere , do you have any feedback on the comments on this PR?

mlctrez avatar Jan 13 '24 15:01 mlctrez

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.

maxence-charriere avatar Jan 15 '24 04:01 maxence-charriere

@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 avatar Jan 15 '24 05:01 maxence-charriere

@maxence-charriere - The PR was to address a few things:

  1. "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: image

  2. 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.

mlctrez avatar Jan 16 '24 02:01 mlctrez

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 avatar Jan 30 '24 09:01 maxence-charriere

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

mlctrez avatar Jan 30 '24 16:01 mlctrez

resolved by https://github.com/maxence-charriere/go-app/pull/920

mlctrez avatar Mar 12 '24 21:03 mlctrez