elm / http

Make HTTP requests in Elm
https://package.elm-lang.org/packages/elm/http/latest
BSD 3-Clause "New" or "Revised" License
155 stars 46 forks source link

Http.cancel cancelling subsequent requests #64

Open WhileTruu opened 5 years ago

WhileTruu commented 5 years ago

Quick Summary:

When a variable using Task.perform or Task.attempt is created in the update function, Http.cancel cancels http request in next update loop.

SSCCE

https://ellie-app.com/6kdQ4HXVJ2ga1

module Main exposing (main)

import Browser
import Html exposing (text)
import Http
import Json.Decode
import Task
import Time

sequentialCmds : Cmd Msg -> List (Cmd Msg) -> Cmd Msg
sequentialCmds cmd cmds =
    case cmds of
        [] ->
            cmd

        first :: others ->
            Cmd.batch
                [ cmd
                , Task.perform (\_ -> SequentialCmds first others) (Task.succeed ())
                ]

init : Cmd Msg
init =
    sequentialCmds (Http.cancel getBrassicaOleraceaTracker)
        [ getBrassicaOleracea ]

type Msg
    = GotBrassicaOleracea (Result Http.Error Json.Decode.Value)
    | SequentialCmds (Cmd Msg) (List (Cmd Msg))
    | DoSomething

update : Msg -> Cmd Msg
update msg =
    case msg of
        GotBrassicaOleracea _ ->
            Cmd.none

        SequentialCmds cmd cmds ->
            sequentialCmds cmd cmds

        DoSomething ->
            Cmd.none

main : Program () () Msg
main =
    Browser.element
        { init = \_ -> init |> Tuple.pair ()
        , view = \_ -> text ""
        , update =
            \msg _ ->
                update msg
                    |> doSomething
                    |> Tuple.pair ()
        , subscriptions = \_ -> Sub.none
        }

performImmediately : msg -> Cmd msg
performImmediately msg =
    Task.perform (\_ -> msg) (Task.succeed ())

doSomething : Cmd Msg -> Cmd Msg
doSomething cmd =
    let
        doSomethingCmd : Cmd Msg
        doSomethingCmd =
            Task.perform (always DoSomething) (Task.succeed ())
    in
    cmd

getBrassicaOleraceaTracker : String
getBrassicaOleraceaTracker =
    "getBrassicaOleraceaTracker"

getBrassicaOleracea : Cmd Msg
getBrassicaOleracea =
    Http.request
        { method = "GET"
        , headers = []
        , url = "https://en.wikipedia.org/api/rest_v1/page/summary/Brassica_oleracea"
        , body = Http.emptyBody
        , expect = Http.expectJson GotBrassicaOleracea Json.Decode.value
        , timeout = Nothing
        , tracker = Just getBrassicaOleraceaTracker
        }

Additional Details

Variables with other types of cmds such as Cmd.none do not cause this issue, which can be tested by changing the value of doSomethingCmd to Cmd.none.