Skip to content

Instantly share code, notes, and snippets.

@urfolomeus
Last active September 22, 2015 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save urfolomeus/da694b3b06ed51c227ea to your computer and use it in GitHub Desktop.
Save urfolomeus/da694b3b06ed51c227ea to your computer and use it in GitHub Desktop.
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)
<!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>
@urfolomeus
Copy link
Author

Updated to show the final solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment