-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Experiment] Separation of update-cycle through components #25
Comments
@vshapenko This might interest you. |
At first look, looks very promising. As external message i usually use navigation message in my project. Looks worth to try. |
Do i understand correctly that such code lets us make independent mvu cycle for any chosen view element? @TimLariviere |
Yes, I've generalized the |
I like this experiment, FuncUI does something like this as well, provides a Component host as well as the Window host, I like the addition here of an external message (which is missing in FuncUI I think) I believe this could open a (perhaps small) window to let some users author component libraries entirely in F# |
Are there any samples showing the FuncUI Component host vs Window host? |
@JordanMarr Basically the HostWindow is a simple class that inherits from Avalonia's Window and ensures to call the proper functions to integrate the window into the Elmish loop the same goes for HostControl but that one inherits from ContentControl instead source |
Thanks! Trying it out now. That is definitely the idea I had in mind. |
I looked at the examples above and I have a couple of comments.
I also recommend to take a look at MVI architecture. It is very similar to MVU architecture and also have components. Anyway, it looks very promising. Thank you for your work! I think with components architecture we won't need FSharp.Data.Adaptive anymore. Ofcourse data adaptive is very useful in many cases but in case of Fabulous, a lot of boilerplate code is required. |
The AboutCard I created returns a So nothing prevents to use components in list like this:
In my example, I do have support for passing message to parent through the external msg pattern.
Here In this instance, when the user clicks the button inside the component, the component will receive a
The other way is also supported (see the commented line from the 1st example). A parent can pass a state to the component.
Thanks for the link! I'm taking a look at it. |
One big issue with MVU is how it scales. A big application will do a lot of things, which implies a lot of
Msg
to implement and handle.One could split up the application into smaller components and compose everyting, but the way MVU works means it's still up to the developer to manually wire all components messages into application messages. This leads to a lot of work.
Inspired by Feliz's UseElmish, I thought Fabulous could achieve the same use case by encapsulating the "runner" (responsible for the update-view loop) inside a
ViewElement
for making it transparent when declaring a view.So, leveraging the (potential) new architecture of Fabulous, I wrote a
ComponentViewElement
that creates a runner for its child, just likeProgram.run
creates a runner for the application.This way, the component is isolated and has its own init/update/view & msgs that don't trigger a refresh of the parent.
It does support two-way communication with its parent.
A parent can pass down a state to the component (if the parent is updated, then the new state is pushed to the component) through the
withState
extension method.A component can push an update to its parent through the
withExternalMessages
extension method.Implementation:
https://github.com/TimLariviere/Fabulous/blob/play/Fabulous.XamarinForms/Fabulous.XamarinForms/Component.fs
This then allows to write independent components (can be pages, groups of controls, or even a single control).
Fabulous/Fabulous.XamarinForms/samples/CounterApp/CounterApp/AboutCard.fs
Lines 8 to 47 in 551d851
You can write init/update/view functions with their own
Model
andMsg
.They are linked together into a
program
.Then, a helper function will create a
ComponentView
taking the program definition.The parent view can mix usual controls with components.
Fabulous/Fabulous.XamarinForms/samples/CounterApp/CounterApp/CounterApp.fs
Lines 98 to 106 in 551d851
If a parent wants to listen to the component's external messages, it can simply add a new entry to its
Msg
union.Fabulous/Fabulous.XamarinForms/samples/CounterApp/CounterApp/CounterApp.fs
Lines 21 to 29 in 551d851
If a component wants to listen to the state its parent is giving it, it can also add a new entry to its
Msg
union.Fabulous/Fabulous.XamarinForms/samples/CounterApp/CounterApp/AboutCard.fs
Lines 11 to 13 in 551d851
Note: I got a working sample of it. Though the implementation is awful and most likely not working in every cases.
I just wanted to prove that it was possible to achieve such behavior in a way that is idiomatic to Fabulous.
Put another way: Works on my machine™
The text was updated successfully, but these errors were encountered: