mrmiguu / coco

Golang WebAssembly Framework
MIT License
110 stars 4 forks source link

Add Global Store Pattern #9

Open mrmiguu opened 5 years ago

mrmiguu commented 5 years ago

Allow users to embed a global store in their components, having them diff/recompile on mutation. This should be sprinkled intermittently throughout a component hierarchy and patch as needed.

mrmiguu commented 5 years ago

The pattern would look something like this:

func main() {
    coco.Globals(NewGlobals())
    coco.Render(NewApp())
}

and

type Up struct {
    Globals
}

func (u Up) OnUpClick() {
    u.Cocos = append(u.Cocos, "🥥")
    coco.Set(u)
}
mrmiguu commented 5 years ago

image

mrmiguu commented 5 years ago

Currently patching adds new listeners on the new component instance/copy. So when they're fired again, they're using the most up-to-date instance (e.g. coco.Set(...)).

To cause updating of globals to refresh other components embedding them, this would require new listeners to be assigned for all components using those globals; component instances embedding globals can only use the new context (w/ the new global) for the listeners if they're reassigned.