Nhowka / Elmish.Bridge

Create client-server Fable-Elmish apps keeping a single mindset
MIT License
139 stars 17 forks source link

V4 subscription additions #50

Closed sheganinans closed 1 year ago

sheganinans commented 1 year ago

As a consequence of how Program.withSubscription is defined:

let withSubscription (subscribe : 'model -> Sub<'msg>) (program: Program<'arg, 'model, 'msg, 'view>) =
    { program with subscribe = subscribe }

The current implementation of Program.withBridge and Program.withBridgeConfig will overwrite any existing subscriptions. Alternatively, if there are subscriptions added after the bridge configuration, the Elmish.Bridge subscriptions will be overwritten and their events will never be received.

So I'm suggesting the following additional functions to be added (bikeshedding welcome):

let inline withBridgeAndSubs
  endpoint
  (subs : 'model -> (string list * (('msg -> unit) -> IDisposable)) list)
  (program : Program<_, 'model, 'msg, _>) =

   program
   |> Program.withSubscription
        (fun model ->
            subs model
          @ [["Elmish";"Bridge"], fun dispatch -> let config = Bridge.endpoint(endpoint) in config.Attach dispatch; config ])

let inline withBridgeConfigAndSubs
  (subs : 'model -> (string list * (('msg -> unit) -> IDisposable)) list)
  (config:BridgeConfig<_,_>)
  (program : Program<_, 'model, 'msg, _>) =

  program
  |> Program.withSubscription
       (fun model ->
            subs model
          @ ["Elmish"::"Bridge"::(config.name |> Option.map List.singleton |> Option.defaultValue []), fun dispatch -> config.Attach dispatch; config])

So that the new style of Elmish subscriptions can play nice with Elmish.Bridge

sheganinans commented 1 year ago

Solved by #52