Skip to content

Instantly share code, notes, and snippets.

@pdamoc pdamoc/Main2.elm
Created Jun 17, 2016

Embed
What would you like to do?
SAM with AnimationFrame
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/animation-frame": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "4.0.1 <= v < 5.0.0",
"elm-lang/html": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
}
module Main exposing (..)
import Html exposing (..)
import Html.App as App
import Html.Events exposing (onClick)
import AnimationFrame
import Time exposing (Time, second)
-- MODEL
type Model
= Ready
| Countdown Time
| Aborted
| Launched
init : Model
init =
Ready
-- UPDATE
type Msg
= StartCountdown
| UpdateTime Time
| Abort
update : Msg -> Model -> Model
update msg model =
case ( msg, model ) of
( StartCountdown, Ready ) ->
Countdown (10 * second)
( UpdateTime diff, Countdown c ) ->
if (c - diff) < 0 then
Launched
else
Countdown (c - diff)
( Abort, Countdown _ ) ->
Aborted
( _, _ ) ->
model
-- VIEW
view : Model -> Html Msg
view model =
case model of
Ready ->
div []
[ button [ onClick StartCountdown ] [ text "Start Countdown" ] ]
Countdown count ->
div []
[ p [] [ text ("Countdown: " ++ (toString (ceiling (count / 1000)))) ]
, button [ onClick Abort ] [ text "Abort" ]
]
Aborted ->
div [] [ text "Launch Aborted!" ]
Launched ->
div [] [ text "Rocket Launched!" ]
-- WIRING
subscriptions : Model -> Sub Msg
subscriptions model =
case model of
Countdown _ ->
AnimationFrame.diffs UpdateTime
_ ->
Sub.none
main : Program Never
main =
App.program
{ init = init ! []
, update = \msg model -> update msg model ! []
, view = view
, subscriptions = subscriptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.