Last active
June 2, 2016 12:46
-
-
Save sgillis/6cb6666238ace7252d9dd51a852dfe9d to your computer and use it in GitHub Desktop.
Elm message
This file contains 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
module Component exposing (..) | |
import Html exposing (..) | |
import Html.Events exposing (..) | |
import Message | |
type alias Model = | |
{} | |
init : ( Model, Cmd Msg ) | |
init = | |
{} ! [] | |
type Msg | |
= NoOp | |
| SetMessage | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
NoOp -> | |
model ! [] | |
SetMessage -> | |
( model | |
, Message.setMessage "Ow yeah!" | |
|> Cmd.map (\_ -> NoOp) | |
) | |
view : Model -> Html Msg | |
view model = | |
button [ onClick SetMessage ] | |
[ text "Set message" ] |
This file contains 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
module Main exposing (main) | |
import Component | |
import Html exposing (..) | |
import Html.App as App | |
import Message | |
main = | |
App.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
type alias Model = | |
{ message : Message.Model | |
, component : Component.Model | |
} | |
init : ( Model, Cmd Msg ) | |
init = | |
let | |
( message, messageCmds ) = | |
Message.init | |
( component, componentCmds ) = | |
Component.init | |
in | |
{ message = message | |
, component = component | |
} | |
! [] | |
type Msg | |
= NoOp | |
| ComponentMsg Component.Msg | |
| Message Message.Msg | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
let | |
_ = | |
Debug.log msg msg | |
in | |
case msg of | |
NoOp -> | |
model ! [] | |
ComponentMsg msg' -> | |
let | |
( component, componentCmd ) = | |
Component.update msg' model.component | |
in | |
( { model | component = component } | |
, Cmd.map ComponentMsg componentCmd | |
) | |
Message msg' -> | |
let | |
( message, messageCmd ) = | |
Message.update msg' model.message | |
in | |
( { model | message = message } | |
, Cmd.map Message messageCmd | |
) | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ App.map ComponentMsg (Component.view model.component) | |
, text model.message | |
] | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.batch | |
[ Sub.map Message (Message.subscriptions model.message) | |
] |
This file contains 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
module Message exposing (..) | |
import Task | |
type alias Model = | |
String | |
init : ( Model, Cmd Msg ) | |
init = | |
( "", Cmd.none ) | |
type Msg | |
= NoOp | |
| SetMessage String | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
NoOp -> | |
model ! [] | |
SetMessage message -> | |
( message, Cmd.none ) | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none | |
setMessage : String -> Cmd Msg | |
setMessage msg = | |
let | |
_ = | |
Debug.log "received" msg | |
in | |
Task.perform (\_ -> NoOp) | |
(\m -> SetMessage m) | |
(Task.succeed msg) | |
This file contains 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
module Message (Model, Action, init, update, actions, clearMessageAddress, clearMessage, setMessage) where | |
import Effects exposing (Effects) | |
import Effects.Extra exposing (noFx) | |
import Html exposing (..) | |
import Signal | |
import Time exposing (Time) | |
-- MODEL | |
type alias Model = | |
{ message : Maybe Html | |
, timestate : Maybe TimeState | |
} | |
type alias TimeState = | |
{ elapsedTime : Time | |
, prevClock : Time | |
} | |
init : Model | |
init = | |
{ message = Nothing | |
, timestate = Nothing | |
} | |
-- UPDATE | |
type Action | |
= NoOp | |
| SetMessage Html | |
| ClearMessage | |
| Tick Time | |
update : Action -> Model -> ( Model, Effects Action ) | |
update action model = | |
case action of | |
NoOp -> | |
model |> noFx | |
SetMessage html -> | |
( { message = Just html | |
, timestate = Nothing | |
} | |
, Effects.tick Tick | |
) | |
Tick time -> | |
let | |
elapsedTime' = | |
case model.timestate of | |
Nothing -> | |
0 | |
Just { elapsedTime, prevClock } -> | |
elapsedTime + (time - prevClock) | |
in | |
if elapsedTime' > 5000 then | |
( { message = Nothing, timestate = Nothing } | |
, Effects.none | |
) | |
else | |
( { model | |
| timestate = | |
Just | |
{ elapsedTime = elapsedTime' | |
, prevClock = time | |
} | |
} | |
, Effects.tick Tick | |
) | |
ClearMessage -> | |
{ message = Nothing, timestate = Nothing } |> noFx | |
-- SIGNALS | |
mailbox : Signal.Mailbox Action | |
mailbox = | |
Signal.mailbox NoOp | |
actions : Signal Action | |
actions = | |
mailbox.signal | |
clearMessageAddress : Signal.Address () | |
clearMessageAddress = | |
Signal.forwardTo mailbox.address (\_ -> ClearMessage) | |
setMessageAddress : Signal.Address Html | |
setMessageAddress = | |
Signal.forwardTo mailbox.address SetMessage | |
clearMessage : Effects () | |
clearMessage = | |
Signal.send clearMessageAddress () | |
|> Effects.task | |
setMessage : Html -> Effects () | |
setMessage html = | |
Signal.send setMessageAddress html | |
|> Effects.task |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment