I have a project with model update view architecture using fable-elmish. And I have to download files every minute and read those files. How can I download in the update function and how can I read and parsing to Json?
I need to create dynamic charts using Fable too. Someone knows how?
I have part of my code here:
let update (msg : Msg) (model : Model) =
match msg with
| GetData ->
model,
Cmd.ofPromise
(fun () ->
promise {
let wc = new WebClient()
wc.DownloadData("https://www.quandl.com/api/v1/datasets/LBMA/SILVER.json", "SILVER.json")
wc.DownloadData("https://www.quandl.com/api/v1/datasets/LBMA/GOLD.json", "GOLD.json")
// Read 2 files
// Return 2 Json.Object
})
()
(fun silver gold -> GotData silver gold)
(fun e -> GotError e.Message)
| GotData silver gold ->
(Model.SilverData silver, Model.GoldData gold), // I think this doesn't work
Cmd.ofPromise
(fun () -> Promise.sleep 60000)
()
(fun () -> GetData)
(fun e -> GetData)
If you have a periodic event which should cause some action in your Elmish application I would use a subscription. The following code snippet shows a function which sets an interval that causes a command dispatch every 10 minutes.
You would use the
Program.withSubscription
function to add the subscription to your main dispatch loop.I would use the Fable PowerPack package for its
fetch
andpromise
support to get the datasets. The following code would fetch the documents from your specified endpoints, parse them as values of theDataSet
type and return them as a value of theSilverAndGold
model type on the successful path of the promise.In the update function of the Elmish app you can see how the promise execution is triggered. On every
LoadDataSet
message dispatched by our subscription we create a command of the promise which either results in aDataSetLoaded
message containing the datasets or in an Error.We can use the Fable bindings for the Recharts library to plot our datasets. The following code shows how we transform and trim the datasets (rendering all datapoints would be quite taxing in the browser) and display them as line charts in the
view
function.I cooked up a very little Elmish app which includes all these topics. You can find it here here and adapt it according to your needs.