Hi, I’m trying to implement a pattern that @rupert mentioned a couple of times.
My module handles some http requests with the server, I called it “Loader” and this is its signature:
module Loader exposing (init, Model, Msg, Out(..), update)
Then I created two kind of messages, internals (Msg) and externals (Out):
type Out
= GotSeriesOut Model
type Msg
= BatchFinished
| GotImages DicomTypes.DicomSeries (Result Http.Error (List DicomTypes.DicomImage))
| GotSeries (Result Http.Error (List DicomTypes.DicomSeries))
The update is something like:
update : Msg -> Model -> ( Model, Cmd Msg, Maybe Out )
update msg model =
case msg of
GotImages dicomseries result ->
case result of
Ok images ->
-- code removed
GotSeries result ->
case result of
Ok series ->
-- code removed
({model | dicomSeries = series}, cmds, Just (GotSeriesOut model))
BatchFinished ->
(model, Cmd.none, Nothing)
As you can see, when a GotSeries Msg is catched by the Loader’s update function, it fires a GotSeriesOut, that must be captured by the Main module.
In the Main module I had to define two messages, one for Loader.Out and other for Loader.Msg (please tell me if Loader.Msg is really needed).
type Msg
-- code removed
| LoaderMsg Loader.Msg
| LoaderOut Loader.Out
In the Model I also had to add a reference to Loader.Model:
type alias Model =
{ -- code removed
, loaderModel : Loader.Model
}
Now my problem, the update function.
I needed to catch the Loader.Msg (again, please tell me if there is a way to avoid this) as this:
LoaderMsg loaderMsg ->
let
(loaderModel, loaderCmd, loaderOut) =
Loader.update loaderMsg model.loaderModel
in
( { model | loaderModel = loaderModel, series = loaderModel.dicomSeries }
, Cmd.map LoaderMsg loaderCmd
)
Then, as I just want to catch Out messages I implemented this:
LoaderOut loaderOut ->
case loaderOut of
Loader.GotSeriesOut m ->
let
(loaderModel, loaderCmd, loaderOutCmd) =
Loader.update loaderOut model.loaderModel
_ =
Debug.log "LoaderOout" m
in
( { model | loaderModel = m, series = m.dicomSeries }
, Cmd.map LoaderOut loaderOutCmd
Btw, the above code doesn’t work, I get this error:
This `loaderOutCmd` value is a:
Maybe Loader.Out
But `map` needs the 2nd argument to be:
Cmd Loader.Out