vugu / vugu

Vugu: A modern UI library for Go+WebAssembly (experimental)
https://www.vugu.org
MIT License
4.86k stars 174 forks source link

K/V Store #138

Closed charles-d-burton closed 4 years ago

charles-d-burton commented 4 years ago

Question Is there a mechanism for browser persistence? Meaning something like a K/V store that persists similar to Vuex or Redux?

Is your question related to a problem? Please describe. No, mostly just curious.

Additional context Mostly interested in this for login persistence. So that once I establish a session with a backend I can resume it.

bradleypeabody commented 4 years ago

There are a few different aspects to this.

To preserve a token from the backend I would suggest localStorage, which you can access via something like js.Global().Get("localStorage").Call("setItem", "somekey", "somevalue").

As for what Vuex addresses, which is shared state throughout the application, I think this will work quite differently in Vugu than it does in Vue, due to the nature of how Go deals with data vs Javascript. The general idea is to just use structs and pointers and not have "data binding" per-se. But it does need more experimentation to find and document good patterns.

charles-d-burton commented 4 years ago

Is that callable from within a go function or do I have to get it in the HTML? Kind of noob question but I'm probably the most clueless frontend dev on the planet.

bradleypeabody commented 4 years ago

The code above is regular Go code that uses syscall/js, you can call it from anywhere.

charles-d-burton commented 4 years ago

I ended up wirting a tiny little helper function and that did the trick perfectly, plus it's re-usable:


package main

import "syscall/js"

func setLocal(key, value string) {
    js.Global().Get("localStorage").Call("setItem", key, value)
}

func getLocalString(key string) string {
    return js.Global().Get("localStorage").Call("getItem", key).String()
}```