Created
July 14, 2016 14:24
-
-
Save folkertdev/7fb7b64b9de63a231c30c41915630971 to your computer and use it in GitHub Desktop.
Handling a list of OutMsg the nice way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Effective code. Nice and short | |
let | |
updateModel newEditor (Model generic _) = | |
Model generic (EditorView { apiToken = apiToken, staticToken = staticToken, editor = newEditor }) | |
in | |
Editor.State.update childMsg editor | |
|> mapChildCmd ChildEditorAction | |
|> outMsgUpdate updateModel editorOutMsg model | |
-- The magic that makes it possible | |
mapChildCmd f ( a, childCmd, c ) = | |
( a, Cmd.map f childCmd, c ) | |
outMsgUpdate : | |
(childComponent -> model -> model) | |
-> (outMsg -> model -> ( model, Cmd msg )) | |
-> model | |
-> ( childComponent, Cmd msg, List outMsg ) | |
-> ( model, Cmd msg ) | |
outMsgUpdate updateModel interpretOutMsg model ( childComponent, cmd, outMsgs ) = | |
let | |
newModel = | |
updateModel childComponent model | |
swap ( x, y ) = | |
( y, x ) | |
wrap : (outmsg -> model -> ( model, Cmd msg )) -> outmsg -> State model (Cmd msg) | |
wrap f msg = | |
State.advance (f msg >> swap) | |
in | |
State.traverse (wrap interpretOutMsg) outMsgs | |
|> State.map Cmd.batch | |
|> State.map (\outCmd -> Cmd.batch [ cmd, outCmd ]) | |
|> State.run newModel | |
|> swap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment