fable-compiler / Fable.Lit

Write Fable Elmish apps with Lit
https://fable.io/Fable.Lit/
MIT License
91 stars 13 forks source link

Add ReactiveControllerbase #35

Closed AngelMunoz closed 2 years ago

AngelMunoz commented 2 years ago

I gave it a thought after a while, I think if we use a common base interface we won't force users to implement each method of a reactive controller it gives the flexibility for the users to create controllers like the ones that subscribe to a global store (or even event listeners like the mouse controller)

type MyHalfController(host: ReactiveHost) as this =
  do
    let mutable value = 0 
    let mutable sub = None
    host.AddController(this)

  [<DefaultValue>]
  val Value: int 
    with get() = value

  interface ReactiveHostConnected with
    member _.hostConnected() =
      sub <-
          GlobalStore.Subscribe(fun v ->
            value <- v
            host.requestUpdate()
          )

  interface ReactiveHostDisconnected with
    member _.hostDisconnected() =
      Option.iter (fun s -> s.Dispose()) sub

I'm targeting your controllers branch because I'm not sure if this is a good enough change and if you want to add more to the other branch before merging the PR

alfonsogarciacaro commented 2 years ago

Thanks again @AngelMunoz and sorry for taking so long to reply. Too focused now in preparing things for Snake Island alpha release 😅 I'm ok with you merging the PRs if I take too long to reply 👍 I can also send you an invitation for the Lit Nuget packages so you can push new releases if necessary. This is your Nuget alias right? https://www.nuget.org/profiles/Tunaxor

AngelMunoz commented 2 years ago

No worries, I'm sure Snake Island is just around the corner and deserves all the attention it can get

and sure that's my NuGet alias!