Last active
February 8, 2018 23:57
-
-
Save maxhoffmann/129d72fd2f87090af864c63e30098a6d to your computer and use it in GitHub Desktop.
Revisions
-
Maximilian Hoffmann revised this gist
May 29, 2016 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -19,6 +19,7 @@ type alias Model = type Token = Valid String | Invalid | Refreshing init : ( Model, Cmd Msg ) @@ -46,6 +47,9 @@ attemptWithToken token failureMsg successMsg requestWithoutToken = Invalid -> Cmd.none Refreshing -> Cmd.none Valid token -> Task.perform failureMsg successMsg (requestWithoutToken token) @@ -57,7 +61,7 @@ update msg model = ( model, attemptWithToken model.token FetchFailure SaveIp fetchIp ) FetchFailure error -> ( { model | token = Refreshing }, Task.perform RefreshTokenFailed UpdateToken (Task.succeed "valid") ) SaveIp ip -> ( { model | ip = ip }, Cmd.none ) @@ -111,4 +115,4 @@ main = , update = update , view = view , subscriptions = \_ -> Sub.none } -
Maximilian Hoffmann revised this gist
May 28, 2016 . 1 changed file with 20 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,7 @@ type Token init : ( Model, Cmd Msg ) init = ( Model (Valid "expired") [] "no ip", Cmd.none ) @@ -35,17 +35,18 @@ type Msg | FetchFailure Http.Error | SaveIp String | SaveRequest (String -> Task.Task Never Msg) | RefreshTokenFailed Never | UpdateToken String | SendPendingRequests () attemptWithToken : Token -> (x -> msg) -> (a -> msg) -> (String -> Task.Task x a) -> Cmd msg attemptWithToken token failureMsg successMsg requestWithoutToken = case token of Invalid -> Cmd.none Valid token -> Task.perform failureMsg successMsg (requestWithoutToken token) @@ -55,8 +56,8 @@ update msg model = Fetch -> ( model, attemptWithToken model.token FetchFailure SaveIp fetchIp ) FetchFailure error -> ( model, Task.perform RefreshTokenFailed UpdateToken (Task.succeed "valid") ) SaveIp ip -> ( { model | ip = ip }, Cmd.none ) @@ -65,6 +66,15 @@ update msg model = SaveRequest request -> ( { model | pendingRequests = request :: model.pendingRequests }, Cmd.none ) RefreshTokenFailed error -> ( model, Cmd.none ) UpdateToken newToken -> ( { model | token = Valid newToken }, Task.perform SendPendingRequests SendPendingRequests (Task.succeed ()) ) SendPendingRequests _ -> ( model, Cmd.none ) -- View @@ -84,7 +94,10 @@ view model = fetchIp : String -> Task.Task Http.Error String fetchIp token = if token == "expired" then Http.get ("ip" := Json.string) ("http://thisfails" ++ token) else Http.get ("ip" := Json.string) ("http://jsonip.com?token=" ++ token) @@ -98,4 +111,4 @@ main = , update = update , view = view , subscriptions = \_ -> Sub.none } -
Maximilian Hoffmann revised this gist
May 28, 2016 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,3 @@ import Html.App as Html import Html exposing (..) import Html.Events exposing (..) -
Maximilian Hoffmann revised this gist
May 28, 2016 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ module Main exposing (..) import Html.App as Html import Html exposing (..) import Html.Events exposing (..) @@ -42,11 +44,11 @@ attemptWithToken token failureMsg successMsg requestWithoutToken = case token of Invalid -> -- save requestWithoutToken Cmd.none Valid token -> -- attempt request Task.perform failureMsg successMsg (requestWithoutToken token) update : Msg -> Model -> ( Model, Cmd Msg ) -
Maximilian Hoffmann created this gist
May 28, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,101 @@ import Html.App as Html import Html exposing (..) import Html.Events exposing (..) import Task import Http import Json.Decode as Json exposing ((:=)) -- Model type alias Model = { token : Token , pendingRequests : List (String -> Task.Task Never Msg) , ip : String } type Token = Valid String | Invalid init : ( Model, Cmd Msg ) init = ( Model (Valid "secret") [] "no ip", Cmd.none ) -- Update type Msg = Fetch | FetchFailure Http.Error | SaveIp String | SaveRequest (String -> Task.Task Never Msg) attemptWithToken : Token -> (x -> msg) -> (a -> msg) -> (String -> Task.Task x a) -> Cmd msg attemptWithToken token failureMsg successMsg requestWithoutToken = case token of Invalid -> -- save requestWithoutToken Task.perform SaveRequest SaveRequest (Task.succeed requestWithoutToken) Valid token -> -- attempt request Cmd.none update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of Fetch -> ( model, attemptWithToken model.token FetchFailure SaveIp fetchIp ) FetchFailure _ -> ( model, Cmd.none ) SaveIp ip -> ( { model | ip = ip }, Cmd.none ) -- Token Logic SaveRequest request -> ( { model | pendingRequests = request :: model.pendingRequests }, Cmd.none ) -- View view : Model -> Html Msg view model = div [] [ button [ onClick Fetch ] [ text "send request" ] , text model.ip ] -- Request fetchIp : String -> Task.Task Http.Error String fetchIp token = Http.get ("ip" := Json.string) ("http://jsonip.com?token=" ++ token) -- Main main : Program Never main = Html.program { init = init , update = update , view = view , subscriptions = \_ -> Sub.none }