Skip to content

Instantly share code, notes, and snippets.

@szabba
Forked from sgillis/Component.elm
Last active June 2, 2016 13:15
Show Gist options
  • Save szabba/51648aaa3376ae3500e432a4ea8a2e11 to your computer and use it in GitHub Desktop.
Save szabba/51648aaa3376ae3500e432a4ea8a2e11 to your computer and use it in GitHub Desktop.
Elm message
module Component exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
type alias Model =
{}
init : ( Model, Cmd Msg )
init =
{} ! []
type Msg
= NoOp
| SetMessage
update : { b | setMessage : String -> Cmd rootMsg } -> Msg -> Model -> ( Model, Cmd rootMsg )
update central msg model =
case msg of
NoOp ->
model ! []
SetMessage ->
( model
, central.setMessage "Ow yeah!"
)
view : Model -> Html Msg
view model =
button [ onClick SetMessage ]
[ text "Set message" ]
{
"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/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 (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 =
case msg of
NoOp ->
model ! []
ComponentMsg msg' ->
let
( component, cmd ) =
Component.update
{ setMessage = \text -> Cmd.map Message <| Message.setMessage text
}
msg'
model.component
in
( { model | component = component }
, cmd
)
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)
]
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)
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