treilik / bubbleboxer

MIT License
64 stars 4 forks source link

CreateLeaf take model by value, why not by reference? #5

Closed Robert-M-Muench closed 2 years ago

Robert-M-Muench commented 2 years ago

func (b *Boxer) CreateLeaf(address string, model tea.Model) Node

Since tea.Model is an interface, all values are copied. This means, I can't create 1..N models in my app, add them to bubbleboxer, and work with the app models because those used by bubbleboxer are copies.

Wouldn't it be much better to use a reference inside bubbleboxer? With this, it would be possible/much simpler to add bubbleboxer later into an existing app.

treilik commented 2 years ago

using references undermines the elm architecture bubbletea is build after. The idea is that you could store each model instance and there by be able to return to the former state. When you are using references, this is no longer true, and thus in this design, bad. But maybe have a look at EditLeaf. I was think about a nicer way to edit the contained Models as well, and this was what i came up with. What do you think about it?

Robert-M-Muench commented 2 years ago

WRT references

Currently, I have one main model consisting of many sub-models (input, table, etc.).

I then add these sub-models to bubbleboxer, which creates the layout.

However, my idea was that my main model is the single point of truth, and boxer is just a different way to display it. But it seems that the boxer's idea is that it is the main owner of all the models added to the layout tree. Hence, the layout tree is the main model.

EditLeaf

Yes, looks good. I think I will try that. However, this means, that all my Update() functions, always have to use EditLeaf() or mirror it's behavior by getting the model from boxer, update it, and write it back, right?

Again, one thing that should be made clear is that boxer assumes it is the main model, taking ownership of the child models, and users shouldn't manage these models at different places too.

treilik commented 2 years ago

Yeah, so you have your Main Model and in it you have a bubbleboxer with all your models bubbleboxer should display. Because bubbleboxer is contained in your main model, i would argue that your main model is still your single source of truth. And yes for every change to one of the leaf-models, you would have to call EditLeaf which takes care of the changing and saving of the changed model.

Yes, your last sentence seems something for the readme. Only i would like to point out that it assumes to be the main model for all models it containes. but can and should still have at least one Model above it, to take care of the logic and maybe also to render something around the bubbleboxer View.