Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http.cancel cancelling subsequent requests #64

Open
WhileTruu opened this issue Aug 12, 2019 · 0 comments
Open

Http.cancel cancelling subsequent requests #64

WhileTruu opened this issue Aug 12, 2019 · 0 comments

Comments

@WhileTruu
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant