Skip to content

Instantly share code, notes, and snippets.

@prozacchiwawa
Last active February 2, 2016 08:37
Show Gist options
  • Save prozacchiwawa/456ea2986f316dee400b to your computer and use it in GitHub Desktop.
Save prozacchiwawa/456ea2986f316dee400b to your computer and use it in GitHub Desktop.
module PortTest where
{--
<html>
<head>
<script src='port-test.js'></script>
</head>
<body>
<div id='port-test'></div>
<script>
var portTest = Elm.embed(Elm.PortTest, document.getElementById('port-test'));
portTest.ports.outputPort.subscribe(function (t) {
console.log("js-output",t);
});
</script>
</body>
</html>
--}
import Debug exposing (log)
import StartApp
import Effects exposing (Effects(..), Never)
import Html exposing (div, text)
import Html.Events exposing (onClick)
import Signal
import Task exposing (Task (..))
outputSignal : Signal.Mailbox String
outputSignal =
Signal.mailbox ""
port outputPort : Signal String
port outputPort =
Signal.map (log "output") outputSignal.signal
type Action = Act Int
type Model = Model Int
update : Action -> Model -> Model
update action model =
Model ((case model of Model x -> x) + (case (log "action" action) of Act x -> x))
view : Signal.Address Action -> Model -> Html.Html
view address model =
div [onClick address (Act 1)] [text (toString model)]
getActVal a =
case a of
Act x -> x
main : Signal Html.Html
main =
let app = StartApp.start {
init = (Model 0, Effects.none)
, update = \a -> \m -> (
update a m,
if getActVal a == 0 then Effects.none else Signal.send outputSignal.address (toString m) |> Task.map (\_ -> Act 0) |> Effects.task
)
, view = view
, inputs = []
} in
app.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment