http icon indicating copy to clipboard operation
http copied to clipboard

Http.cancel cancelling subsequent requests

Open WhileTruu opened this issue 6 years ago • 0 comments

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
        }
  • Elm: 0.19 and elm-0.19.1-beta-1-mac
  • Browser: Google Chrome and Safari
  • Operating System: MacOS (compilers and ellie), Ubuntu 18.04 (ellie)

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.

WhileTruu avatar Aug 12 '19 09:08 WhileTruu