maxence-charriere / go-app

A package to build progressive web apps with Go programming language and WebAssembly.
https://go-app.dev
MIT License
7.75k stars 354 forks source link

V10 property update issue #937

Closed mlctrez closed 3 months ago

mlctrez commented 3 months ago

With the recent V10 code base, I was testing the app update functionality and came across an issue that I can't resolve:

// the go-app version I used
github.com/maxence-charriere/go-app/v10 v10.0.0-20240312064243-fd8d583dcdfa

I was trying to develop a version component that allows click to check for new version and then click to reload the application if a new version is available. Here's the component ( it contains some additional logging of the pointer to *Version):

package compo

import (
    "fmt"
    "github.com/maxence-charriere/go-app/v10/pkg/app"
)

var _ app.AppUpdater = (*Version)(nil)
var _ app.Mounter = (*Version)(nil)

type Version struct {
    app.Compo
    UpdateAvailable string
}

func (v *Version) OnMount(ctx app.Context) {
    app.Log("OnMount", fmt.Sprintf("%v %q", &v, v.UpdateAvailable))
}

func (v *Version) OnAppUpdate(ctx app.Context) {
    app.Log("OnAppUpdate 1", fmt.Sprintf("%v %q", &v, v.UpdateAvailable))
    if ctx.AppUpdateAvailable() {
        v.UpdateAvailable = "yes"
        app.Log("OnAppUpdate 2", fmt.Sprintf("%v %q", &v, v.UpdateAvailable))
    }
}

func (v *Version) Render() app.UI {
    app.Log("Render", fmt.Sprintf("%v %q", &v, v.UpdateAvailable))
    if v.UpdateAvailable == "yes" {
        return app.Div().Class("version").Text("Update Available").
            OnClick(func(ctx app.Context, e app.Event) {
                ctx.Reload()
            })
    }
    return app.Div().Class("version").Text(app.Getenv("GOAPP_VERSION")).
        OnClick(func(ctx app.Context, e app.Event) {
            ctx.Dispatch(func(context app.Context) {
                context.TryUpdateApp()
            })
        })
}

The root object and route are as follows:

func (r *Root) Render() app.UI {
    return app.Div().Body(&Version{})
}

app.Route("/", func() app.Composer { return &Root{} })

The log indicates the pointer to *Version changes in between the OnAppUpdate() and Render() calls so the if condition in the Render() does not find the proper value for v.AppUpdateAvailable

The console log:

image

At 19:49:39 is when the version component is first clicked.

The full code is located here: https://github.com/mlctrez/mlctrez.github.io/tree/v0.0.7/goapp/compo

oderwat commented 3 months ago

Try "c.updateAvailable" instead of "c.UpdateAvailable"

maxence-charriere commented 3 months ago

Don’t export the UpdateAvailable field.

mlctrez commented 3 months ago

My bad. I could have sworn I knew that rule and already tried making it lowercase to see if that worked. Closing.