Last active
September 22, 2015 15:11
-
-
Save urfolomeus/da694b3b06ed51c227ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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 = | |
Signal.send outbox.address "" | |
|> Effects.task | |
|> Effects.map (always NoOp) |
This file contains hidden or 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
<!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> |
Updated to show the final solution :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mgold pointed out that
(\_ -> ...)
is such a common pattern in Elm that there is a more common way to do it(always ...)