Skip to content

Instantly share code, notes, and snippets.

@jessitron
Forked from urfolomeus/EffectsTest.elm
Last active September 22, 2015 15:08
Show Gist options
  • Save jessitron/19629f54b9319d29c525 to your computer and use it in GitHub Desktop.
Save jessitron/19629f54b9319d29c525 to your computer and use it in GitHub Desktop.
The send Task can turn into the NoOp effect
module EffectsTest where
import Html exposing (..)
import Html.Events exposing (onClick)
import StartApp exposing (App)
import Task exposing (Task)
import Effects exposing (Effects, Never)
-- WIRING
app : App Model
app =
StartApp.start
{ init = initialModel "Hello"
, update = update
, view = view
, inputs = [incomingActions]
}
main : Signal Html
main =
app.html
port tasks : Signal (Task Never ())
port tasks =
app.tasks
-- MODEL
type alias Model = String
initialModel : String -> (Model, Effects Action)
initialModel string =
(string, Effects.none)
-- UPDATE
type Action = NoOp | RequestString | Write Model
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
NoOp ->
(model, Effects.none)
RequestString ->
(model, getNewString)
Write string ->
(string, Effects.none)
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
div [ ]
[ text model
, button
[ onClick address RequestString ]
[ text "Click me!" ]
]
-- PORTS
port incoming : Signal Model
port outgoing : Signal String
port outgoing =
outbox.signal
-- SIGNALS
incomingActions : Signal Action
incomingActions =
Signal.map (\string -> Write string) incoming
outbox : Signal.Mailbox String
outbox =
Signal.mailbox ""
-- EFFECTS
getNewString : Effects Action
getNewString =
getNewString =
Signal.send outbox.address "" |> Effects.task |> Effects.map (\_ -> NoOp)
<!DOCTYPE html>
<html>
<head>
<title>Effects Test</title>
<script src="elm.js"></script>
</head>
<body>
<div id="elmMain"></div>
</body>
<script>
var elmDiv = document.getElementById('elmMain')
, elmApp = Elm.embed(Elm.EffectsTest, elmDiv, {incoming: ""});
elmApp.ports.incoming.send("Hello from outside");
elmApp.ports.outgoing.subscribe(function () {
elmApp.ports.incoming.send("You rang?");
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment