TimLariviere / Fabulous-new

Fabulous v2 - Work in progress
https://timothelariviere.com/Fabulous-new/
Other
41 stars 3 forks source link

[Architecture] Add lifecycle events to widget (mount, dismount, create, destroy) #25

Closed TimLariviere closed 2 years ago

SergejDK commented 2 years ago

In which order should this happen?

Create -> Mount -> Dismount -> Destroy ?

TimLariviere commented 2 years ago

Yes. I think that's the good order. Just after dismount, the widget can be re-mounted.

An example is ListView with virtualization

SergejDK commented 2 years ago

Yes that sounds correct. Do you have an idea or a preference how to act on them?

e.g. like a extensionmethod ?

StackLayout().onCreate(fun _ => printfn "OnCreate")
twop commented 2 years ago

I think instead of lambda we want it to accept a concrete message, or a function that creates a concrete message (in case we need to pass data in).

like so

StackLayout().onCreate(StackCreated)

// if we need to pass args
StackLayout().onCreate(fun args -> StackCreated args)
TimLariviere commented 2 years ago

@SergejDK Yes, an extension method is good. That lambda should return a 'msg type like @twop said. I think we can start without parameters.

Contrary to what we do in v1 (create lets you access the XF control), I think we will only notify that the control has been created. Then with ViewRef, developers will be able to access the XF control itself.

type Msg =
    | LabelCreated

let labelRef = ViewRef<_>()

let update msg model =
    match msg with
    | LabelCreated ->
        labelRef.Value.BackgroundColor <- Color.Blue

let view model =
    StackLayout() {
        Label("Text")
            .reference(labelRef)
            .onCreate(LabelCreated)
    }