From 36a565de2b01409316270378006e5aba930a8198 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 13 Oct 2023 17:04:11 -0400 Subject: [PATCH] `await` for `FetchRequest` delegate to handle response Closes [#884] Despite the fact that `Visit` and `FrameController` are `FetchRequest` delegate classes that implement their delegate methods asynchronously on success ([Visit.requestSucceededWithResponse][] and [FrameController.requestSucceededWithResponse][]) and on failure ([Visit.requestFailedWithResponse][]) and [FrameController.requestFailedWithResponse][]), the `async FetchRequest.receive` method doesn't use the `await` keyword to wait for those delegates to finish handling their callbacks. This commit adds those `await` clauses. [Visit.requestSucceededWithResponse]: https://github.com/hotwired/turbo/blob/c207f5b25758e4a084e8ae42e49712b91cf37114/src/core/drive/visit.js#L297 [FrameController.requestSucceededWithResponse]: https://github.com/hotwired/turbo/blob/c207f5b25758e4a084e8ae42e49712b91cf37114/src/core/frames/frame_controller.js#L210 [Visit.requestFailedWithResponse]: https://github.com/hotwired/turbo/blob/c207f5b25758e4a084e8ae42e49712b91cf37114/src/core/drive/visit.js#L311 [FrameController.requestFailedWithResponse]: https://github.com/hotwired/turbo/blob/c207f5b25758e4a084e8ae42e49712b91cf37114/src/core/frames/frame_controller.js#L215 [#884]: https://github.com/hotwired/turbo/issues/884 --- src/core/frames/frame_controller.js | 4 ++-- src/http/fetch_request.js | 4 ++-- src/tests/functional/frame_tests.js | 19 +++++++++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/core/frames/frame_controller.js b/src/core/frames/frame_controller.js index 5940ba761..f06241ba0 100644 --- a/src/core/frames/frame_controller.js +++ b/src/core/frames/frame_controller.js @@ -321,7 +321,7 @@ export class FrameController { } } - async #visit(url) { + #visit(url) { const request = new FetchRequest(this, FetchMethod.get, url, new URLSearchParams(), this.element) this.#currentFetchRequest?.cancel() @@ -333,7 +333,7 @@ export class FrameController { this.#currentFetchRequest = null resolve() } - request.perform() + return request.perform() }) } diff --git a/src/http/fetch_request.js b/src/http/fetch_request.js index 3d79f24ae..6f3b73322 100644 --- a/src/http/fetch_request.js +++ b/src/http/fetch_request.js @@ -148,9 +148,9 @@ export class FetchRequest { if (event.defaultPrevented) { this.delegate.requestPreventedHandlingResponse(this, fetchResponse) } else if (fetchResponse.succeeded) { - this.delegate.requestSucceededWithResponse(this, fetchResponse) + await this.delegate.requestSucceededWithResponse(this, fetchResponse) } else { - this.delegate.requestFailedWithResponse(this, fetchResponse) + await this.delegate.requestFailedWithResponse(this, fetchResponse) } return fetchResponse } diff --git a/src/tests/functional/frame_tests.js b/src/tests/functional/frame_tests.js index b090aff8a..64cdb8be8 100644 --- a/src/tests/functional/frame_tests.js +++ b/src/tests/functional/frame_tests.js @@ -608,16 +608,27 @@ test("navigating pushing URL state from a frame navigation fires events", async await nextEventOnTarget(page, "frame", "turbo:before-fetch-response") await nextEventOnTarget(page, "frame", "turbo:frame-render") await nextEventOnTarget(page, "frame", "turbo:frame-load") - assert.notOk(await nextAttributeMutationNamed(page, "frame", "aria-busy"), "removes aria-busy from the ") - - assert.equal(await nextAttributeMutationNamed(page, "html", "aria-busy"), "true", "sets aria-busy on the ") + assert.equal( + await nextAttributeMutationNamed(page, "html", "aria-busy"), + "true", + "sets aria-busy on the " + ) + assert.equal( + await nextAttributeMutationNamed(page, "frame", "aria-busy"), + null, + "removes aria-busy from the " + ) await nextEventOnTarget(page, "html", "turbo:before-visit") await nextEventOnTarget(page, "html", "turbo:visit") await nextEventOnTarget(page, "html", "turbo:before-cache") await nextEventOnTarget(page, "html", "turbo:before-render") await nextEventOnTarget(page, "html", "turbo:render") await nextEventOnTarget(page, "html", "turbo:load") - assert.notOk(await nextAttributeMutationNamed(page, "html", "aria-busy"), "removes aria-busy from the ") + assert.equal( + await nextAttributeMutationNamed(page, "html", "aria-busy"), + null, + "removes aria-busy from the " + ) }) test("navigating a frame with a form[method=get] that does not redirect still updates the [src]", async ({