Skip to content

Instantly share code, notes, and snippets.

@mfeineis
Last active March 13, 2018 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfeineis/0f2da3ed04b8a2b459661a97daaaa842 to your computer and use it in GitHub Desktop.
Save mfeineis/0f2da3ed04b8a2b459661a97daaaa842 to your computer and use it in GitHub Desktop.
A gist for making the update function better testable by allowing it to return union types that are then interpreted https://ellie-app.com/SZCJ3rJ3a1/3
module ElmFxAndCmds exposing (main)
import Html exposing (Html)
import Html.Events exposing (onClick)
import Http
import Json.Decode as Decode exposing (Decoder, Value)
type alias Model =
{}
type Msg
= WhoIsTheKingInTheNorth
| GotAnswer (Result Http.Error Character)
type Fx
= FetchJonSnow
type alias Character =
{ name : String
, titles : List String
}
decodeCharacter : Decoder Character
decodeCharacter =
Decode.map2 Character
(Decode.field "name" Decode.string)
(Decode.field "titles" (Decode.list Decode.string))
main : Program Never Model Msg
main =
Html.program
{ init = init |> doFx evalFx
, subscriptions = \_ -> Sub.none
, update = \msg model -> update msg model |> doFx evalFx
, view = view
}
init : ( Model, List Fx )
init =
( {}, [] )
update : Msg -> Model -> ( Model, List Fx )
update msg model =
case msg of
WhoIsTheKingInTheNorth ->
( model, [ FetchJonSnow ] )
GotAnswer (Ok { name, titles }) ->
Debug.log (name ++ "/" ++ String.join "," titles)
( model, [] )
GotAnswer (Err reason) ->
Debug.log ("Couldn't find the answer: " ++ toString reason)
( model, [] )
evalFx : Fx -> Model -> ( Model, List (Cmd Msg) )
evalFx fx model =
case fx of
FetchJonSnow ->
let
request =
Http.get
"https://anapioficeandfire.com/api/characters/583"
decodeCharacter
in
( model, [ Http.send GotAnswer request ] )
view : Model -> Html Msg
view model =
Html.button
[ onClick WhoIsTheKingInTheNorth
]
[ Html.text "The King in the North!"
]
-- Helpers
type alias ApplyFx fx msg model =
fx -> model -> ( model, List (Cmd msg) )
mergeFxs : ApplyFx fx msg model -> model -> List (Cmd msg) -> List fx -> ( model, Cmd msg )
mergeFxs apply model cmds fxs =
case fxs of
fx :: rest ->
let
( updatedModel, newCmds ) =
apply fx model
in
mergeFxs apply updatedModel (cmds ++ newCmds) rest
[] ->
( model, Cmd.batch cmds )
doFx : ApplyFx fx msg model -> ( model, List fx ) -> ( model, Cmd msg )
doFx apply (( model, fxs ) as pair) =
mergeFxs apply model [] fxs

MIT License

Copyright (c) 2018 Martin Feineis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment