Skip to content

Instantly share code, notes, and snippets.

@dallinbeutler
Created July 17, 2019 23:04
Show Gist options
  • Save dallinbeutler/b99f9c00c7c00c389824a7bf895759ca to your computer and use it in GitHub Desktop.
Save dallinbeutler/b99f9c00c7c00c389824a7bf895759ca to your computer and use it in GitHub Desktop.
add any number of views to your Elmish program
// a cheesy workaround to pass the resulting view as a message to a view program, then vice versa
// call this last because it starts 2 listener threads.
let WithViewSub
(viewToVMsg)
(viewToPMsg)
vargs
(vprog :Program<'vinit,'vstate,'vmsg,'vview>)
(prog :Program<'init,'state,'msg,'view>)
=
let vmsgagent = MailboxProcessor.Start(fun inbox ->
// the message processing function
let rec messageLoop () = async{
// loop to top
return! messageLoop ()
}
// start the loop
messageLoop ()
)
let pmsgagent = MailboxProcessor.Start(fun inbox ->
// the message processing function
let rec messageLoop () = async{
// loop to top
return! messageLoop ()
}
// start the loop
messageLoop ()
)
vprog
|> Program.withSetState (fun state vdisp -> pmsgagent.Post (viewToPMsg ((vprog|>Program.view) state vdisp ) ) )
|> Program.withSubscription (fun state -> Cmd.ofSub(fun disp -> while true do (disp ( Async.RunSynchronously (vmsgagent.Receive 10000) ))))
|> Program.runWith vargs
prog
|> Program.withSetState (fun m d -> vmsgagent.Post ( viewToVMsg ((prog |> Program.view) m d ) ))
|> Program.withSubscription (fun state -> Cmd.ofSub(fun disp -> while true do (disp ( Async.RunSynchronously (pmsgagent.Receive 10000) ))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment