What is the best way to call a http server (with fetch) asynchronously and update the model? I was thinking about a recursive promise loop, but I'm not sure how to update model in it.
(I'm afraid I asked the same question before, but I forget the answer :( )
Yes, there was a similar discussion here: https://github.com/fable-elmish/elmish/issues/113
This is better replied by Elmish experts (@et1975, @MangelMaxime, @forki) but this should be the case for commands, though I remember you had some issues to update your model this way. I suggested to have a special Elmish version that would accept an asynchronous update function but I haven't tried myself so I'm not sure how it'd work. Did you give it a shot?
let timerCmd intervalInMs msg =
let configure dispatch =
promise {
while true do
dispatch msg
do! Async.Sleep intervalInMs |> Async.StartAsPromise
}
[configure]
let init () = myModel, timerCmd 1000 RefreshData
let update (msg:Msg) model : Model*Cmd<Msg> =
match msg with
| RefreshData -> model,Cmd.ofPromise myfetchPromise NewData FetchError
| .....
something like that might work
Yes, you can use a custom command to trigger a message inside your application like propose by @forki .
However, you probably need to be able to cancel the timer. Because, if you switch to another page you want to stop it to preserve memory ressources etc.
Thanks, guy! I'm sorry I forget about than discussion :( I use explicit message reposting with sleep, works perfectly.