Skip to content

Instantly share code, notes, and snippets.

@s-m-i-t-a
Created March 24, 2017 11:12
Show Gist options
  • Save s-m-i-t-a/d202f4c1fe83b41ea5c3497c284bd36c to your computer and use it in GitHub Desktop.
Save s-m-i-t-a/d202f4c1fe83b41ea5c3497c284bd36c to your computer and use it in GitHub Desktop.
Dispatcher function for elm update functions.
module Update exposing (dispatch)
{-| Create update function
updater1 : Msg -> Model -> Maybe Model
updater1 msg model =
case msg of
Foo ->
Just { model | foo = "Foo" }
_ ->
Nothing
updater2 : Msg -> Model -> Maybe Model
updater2 msg model =
case msg of
Bar ->
Just { model | bar = "Bar" }
_ ->
Nothing
updaters : List (Msg -> Model -> Maybe Model)
updaters =
[ updater1
, updater2
]
main : Program Never Model Msg
main =
Html.beginnerProgram
{ model = init
, view = view
, update = dispatch updaters msg model
}
-}
dispatch : List (msg -> model -> Maybe model) -> msg -> model -> model
dispatch updaters msg model =
case updaters of
[] ->
model
f :: fs ->
case f msg model of
Just model ->
model
Nothing ->
dispatch fs msg model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment