Created
March 6, 2018 06:05
-
-
Save s-c-p/edfe8bba13c64e5c8cdcc0b7117cb07e to your computer and use it in GitHub Desktop.
This file contains 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 Main exposing (..) | |
import Html exposing (Html, button, div, text) | |
import Html.Events exposing (onClick) | |
import Http | |
import Json.Decode as Decode | |
main = | |
Html.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
-- model | |
type alias Model = | |
{ value : Int | |
, delta : Int | |
, netErr : String | |
} | |
init : ( Model, Cmd Msg ) | |
init = | |
( Model 0 1 "", Cmd.none ) | |
-- update | |
type Msg | |
= Inc | |
| Dec | |
| Update | |
| UpdateResult (Result Http.Error String) | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
Inc -> | |
( { model | value = model.value + model.delta }, Cmd.none ) | |
Dec -> | |
( { model | value = model.value - model.delta }, Cmd.none ) | |
Update -> | |
( model, getDeltaUpdate "latest_delta" ) | |
UpdateResult (Ok newDelta) -> | |
case String.toInt newDelta of | |
Ok i -> | |
( { model | delta = i }, Cmd.none ) | |
Err xi -> | |
( model, Cmd.none ) | |
UpdateResult (Err x) -> | |
( model, Cmd.none ) | |
getDeltaUpdate : String -> Cmd Msg | |
getDeltaUpdate url = | |
let | |
u = "http://localhost:8080/" ++ url | |
in | |
Http.send | |
UpdateResult | |
(Http.get u decodeJSON) | |
decodeJSON : Decode.Decoder String | |
decodeJSON = | |
Decode.at [ "data", "delta" ] Decode.string | |
-- view | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ button [ onClick Inc ] [ text "+" ] | |
, button [ onClick Dec ] [ text "-" ] | |
, div [] [ text (toString model) ] | |
, button [ onClick Update ] [ text "update delta" ] | |
] | |
-- subscriptions | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none |
This file contains 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
Install Forefox add-on CORS-Everywhere and make sure it's icon is green colored |
This file contains 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
import time | |
import bottle | |
@bottle.route("/latest_delta") | |
def latest_delta(): | |
time.sleep(1.5) | |
return {'data':{'delta':'4'}} | |
if __name__ == '__main__': | |
bottle.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment