Skip to content

Commit

Permalink
http request canceling +
Browse files Browse the repository at this point in the history
  • Loading branch information
lue-bird committed Nov 24, 2023
1 parent 3c693ab commit 2f5d3ee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
9 changes: 3 additions & 6 deletions runner/http/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { HttpResponse, HttpRequest, HttpError } from "./index.js";

export function http(request: HttpRequest): Promise<HttpResponse> {
let controller: AbortController | undefined;

export function http(request: HttpRequest, abortController: AbortController): Promise<HttpResponse> {
if (request.timeout) {
controller = new AbortController();
setTimeout(() => controller?.abort(), request.timeout);
setTimeout(() => abortController?.abort(), request.timeout);
}

return fetch(request.url, {
method: request.method,
body: request.body || null,
headers: new Headers(request.headers),
signal: controller?.signal,
signal: abortController?.signal,
})
.then((res: Response) => {
const headers = Object.fromEntries(res.headers.entries());
Expand Down
16 changes: 15 additions & 1 deletion runner/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fetchAdapter from "./http/fetch.js";
import { HttpRequest } from "./http/index.js";
/*
import {
DomError,
Expand Down Expand Up @@ -61,7 +62,18 @@ export function start(ports: ElmPorts, appElement: HTMLElement) {
{
on: event => event?.addHttpRequest,
run: (config, sendToElm) => {
fetchAdapter.http(config).then(response => { sendToElm(response) })
const abortController = new AbortController()
httpRequestAbortControllers[config] = abortController
fetchAdapter.http(config, abortController).then(response => { sendToElm(response) })
}
},
{
on: event => event?.removeHttpRequest,
run: (config, _sendToElm) => {
const maybeAbortController = httpRequestAbortControllers[config]
if (maybeAbortController != undefined) {
maybeAbortController.abort()
}
}
},
{
Expand Down Expand Up @@ -143,6 +155,8 @@ export function start(ports: ElmPorts, appElement: HTMLElement) {
}
}

const httpRequestAbortControllers: { [key: string]: AbortController } = {}

// Equivalent Elm Kernel code: https://github.com/elm/time/blob/1.0.0/src/Elm/Kernel/Time.js#L27-L35
function getTimezoneName(): string | number {
try {
Expand Down
9 changes: 6 additions & 3 deletions src/BrowserApp.elm
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,8 @@ interfaceDiffToCmds =
ConsoleLog _ ->
Nothing

HttpRequest _ ->
-- cancel request? Help appreciated!
Nothing
HttpRequest request ->
RemoveHttpRequest (request |> httpRequestToId) |> Just

DomNodeRender _ ->
RemoveDom |> Just
Expand Down Expand Up @@ -774,6 +773,9 @@ interfaceDiffToJson =
AddHttpRequest httpRequestId ->
( "addHttpRequest", httpRequestId |> httpRequestIdToJson )

RemoveHttpRequest httpRequestId ->
( "removeHttpRequest", httpRequestId |> httpRequestIdToJson )

AddWindowEventListener eventName ->
( "addWindowEventListener", eventName |> Json.Encode.string )

Expand Down Expand Up @@ -1373,6 +1375,7 @@ type InterfaceDiff
| AddConsoleLog String
| ReplaceDomNode { path : List Int, domNode : DomNodeId }
| AddHttpRequest HttpRequestId
| RemoveHttpRequest HttpRequestId
| RemoveDom
| AddWindowEventListener String
| RemoveWindowEventListener String
Expand Down

0 comments on commit 2f5d3ee

Please sign in to comment.