Git Product home page Git Product logo

fabulous's People

Contributors

edgarfgp avatar nicoviii avatar rayy77 avatar stroborobo avatar timlariviere avatar twop avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

fabulous's Issues

[Experiment] Separation of update-cycle through components

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 like Program.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).

module AboutCard =
type Model = { State: int; Text: string }
type Msg =
| StateChanged of int
| Toggle
type ExternalMsg =
| TextChanged of string
type CmdMsg = Nothing
let mapCmdMsg cmdMsg =
match cmdMsg with
| Nothing -> Cmd.none
let init() =
{ State = 0; Text = "Hello World!" }, [], []
let update msg model =
match msg with
| StateChanged state ->
{ model with State = state }, [], []
| Toggle ->
let newModel = { model with Text = model.Text + " It worked!" }
newModel, [], [ TextChanged newModel.Text ]
let view model =
StackLayout([
Button("Toggle", Toggle)
Label(sprintf "State is %i" model.State)
Label(model.Text)
])
let program = Program.forComponentWithCmdMsg init update view mapCmdMsg
let asComponent<'msg>(state, onExternalMsg: ExternalMsg -> 'msg) =
ComponentView(program)
//.withState(StateChanged, state)
.withExternalMessages(onExternalMsg)

You can write init/update/view functions with their own Model and Msg.
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.

Label(sprintf "Step size: %d" model.Step)
.automationId("StepSizeLabel")
.alignment(horizontal = LayoutOptions.Center)
Button("Reset", Reset)
.isEnabled(model <> initModel())
.alignment(horizontal = LayoutOptions.Center)
AboutCard.asComponent(model.Count, CardChanged)

If a parent wants to listen to the component's external messages, it can simply add a new entry to its Msg union.

type Msg =
| Increment
| Decrement
| Reset
| SetStep of int
| TimerToggled of bool
| TimedTick
| CloseApp
| CardChanged of AboutCard.ExternalMsg

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.

type Msg =
| StateChanged of int
| Toggle

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™

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.