-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into swift-async-scenario-11
- Loading branch information
Showing
23 changed files
with
279 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
module EasyRacer.Scenario11 exposing (main) | ||
|
||
-- Elm does not really have a higher-level racing construct | ||
-- We are building everything by hand anyway, so this is basically just scenario 6 | ||
|
||
import EasyRacer.Ports as Ports | ||
import Http | ||
import Platform exposing (Program) | ||
import Set exposing (Set) | ||
|
||
|
||
type alias Flags = | ||
String | ||
|
||
|
||
type alias Model = | ||
{ inflightTrackers : Set String | ||
} | ||
|
||
|
||
type Msg | ||
= HttpResponse String (Result Http.Error String) | ||
|
||
|
||
scenarioPath : String | ||
scenarioPath = | ||
"/11" | ||
|
||
|
||
init : Flags -> ( Model, Cmd Msg ) | ||
init baseUrl = | ||
let | ||
requestTrackers = | ||
[ "first", "second", "third" ] | ||
|
||
httpRequest tracker = | ||
{ method = "GET" | ||
, headers = [] | ||
, url = baseUrl ++ scenarioPath | ||
, body = Http.emptyBody | ||
, expect = Http.expectString (HttpResponse tracker) | ||
, timeout = Nothing | ||
, tracker = Just tracker | ||
} | ||
in | ||
( { inflightTrackers = Set.fromList requestTrackers | ||
} | ||
, requestTrackers | ||
|> List.map (Http.request << httpRequest) | ||
|> Cmd.batch | ||
) | ||
|
||
|
||
update : Msg -> Model -> ( Model, Cmd Msg ) | ||
update msg model = | ||
case msg of | ||
HttpResponse tracker httpResult -> | ||
let | ||
remainingTrackers : Set String | ||
remainingTrackers = | ||
model.inflightTrackers |> Set.remove tracker | ||
in | ||
( { model | inflightTrackers = remainingTrackers } | ||
, case httpResult of | ||
Ok bodyText -> | ||
let | ||
cancellations : List (Cmd Msg) | ||
cancellations = | ||
remainingTrackers | ||
|> Set.toList | ||
|> List.map Http.cancel | ||
|
||
returnResult : Cmd Msg | ||
returnResult = | ||
Ok bodyText |> Ports.sendResult | ||
in | ||
returnResult :: cancellations |> Cmd.batch | ||
|
||
Err _ -> | ||
if not <| Set.isEmpty remainingTrackers then | ||
Cmd.none | ||
|
||
else | ||
Err "all requests completed without a single success response" |> Ports.sendResult | ||
) | ||
|
||
|
||
main : Program Flags Model Msg | ||
main = | ||
Platform.worker | ||
{ init = init | ||
, update = update | ||
, subscriptions = always 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
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
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
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
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
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
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
Oops, something went wrong.