Skip to content

Instantly share code, notes, and snippets.

@prozacchiwawa
Created March 22, 2016 01:47
Show Gist options
  • Save prozacchiwawa/dfad8b177044c0fc965f to your computer and use it in GitHub Desktop.
Save prozacchiwawa/dfad8b177044c0fc965f to your computer and use it in GitHub Desktop.
import Effects exposing (Effects(..), Never)
import Html exposing (Html, div, button, text)
import Html.Events exposing (onClick)
import Signal exposing (Mailbox)
import StartApp as SA
import Task exposing (Task(..))
type Request = DummyRequest | Request String
type Response = Response String
type Action = NoOp | Click | Incoming Response | Success String
requestBox : Mailbox Request
requestBox = Signal.mailbox DummyRequest
request : Request -> Effects Action
request r = Effects.task (Signal.send requestBox.address r) |> Effects.map (\_ -> NoOp)
update : Action -> String -> (String, Effects Action)
update action model =
case action of
Click -> (model, request (Request model))
Incoming (Response s) -> (model, Effects.task (Task.succeed (Success s)))
Success s -> (s, Effects.none)
_ -> (model, Effects.none)
view : Signal.Address Action -> String -> Html
view address model =
div [] [
text ("last response " ++ model)
, button [onClick address Click] [text "Send Request"]
]
app : SA.App String
app = SA.start {
init = ("", Effects.none)
, update = update
, view = view
, inputs = [Signal.map (\y -> Incoming (Response y)) responsePort]
}
port tasks : Signal (Task Never ())
port tasks = app.tasks
main = app.html
port responsePort : Signal String
port requestPort : Signal (List String)
port requestPort = Signal.map (\r ->
case r of
Request s -> [s]
DummyRequest -> []) requestBox.signal
<html>
<body>
<script src='testelmd.js'></script>
<script>
var testelmd = Elm.fullscreen(Elm.Main, { responsePort: "" });
function doSendReply(s) {
setTimeout(function() {
console.log('request ' + s);
testelmd.ports.responsePort.send("hi " + s);
}, 100);
}
testelmd.ports.requestPort.subscribe(function (x) {
for (var i = 0; i < x.length; i++) {
var s = x[i];
doSendReply(s);
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment