Skip to content

Commit 4a3919d

Browse files
committed
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]: #884
1 parent cf0c68f commit 4a3919d

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

src/core/drive/form_submission.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class FormSubmission {
137137
this.result = { success: response.succeeded, fetchResponse: response }
138138
}
139139

140-
requestSucceededWithResponse(request, response) {
140+
async requestSucceededWithResponse(request, response) {
141141
if (response.clientError || response.serverError) {
142142
this.delegate.formSubmissionFailedWithResponse(this, response)
143143
return
@@ -151,7 +151,7 @@ export class FormSubmission {
151151
} else {
152152
this.state = FormSubmissionState.receiving
153153
this.result = { success: true, fetchResponse: response }
154-
this.delegate.formSubmissionSucceededWithResponse(this, response)
154+
await this.delegate.formSubmissionSucceededWithResponse(this, response)
155155
}
156156
}
157157

src/core/frames/frame_controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ export class FrameController {
233233
markAsBusy(formElement, this.#findFrameElement(formElement))
234234
}
235235

236-
formSubmissionSucceededWithResponse(formSubmission, response) {
236+
async formSubmissionSucceededWithResponse(formSubmission, response) {
237237
const frame = this.#findFrameElement(formSubmission.formElement, formSubmission.submitter)
238238

239239
frame.delegate.proposeVisitIfNavigatedWithAction(frame, getVisitAction(formSubmission.submitter, formSubmission.formElement, frame))
240-
frame.delegate.loadResponse(response)
240+
await frame.delegate.loadResponse(response)
241241

242242
if (!formSubmission.isSafe) {
243243
session.clearCache()

src/http/fetch_request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class FetchRequest {
155155
if (event.defaultPrevented) {
156156
this.delegate.requestPreventedHandlingResponse(this, fetchResponse)
157157
} else if (fetchResponse.succeeded) {
158-
this.delegate.requestSucceededWithResponse(this, fetchResponse)
158+
await this.delegate.requestSucceededWithResponse(this, fetchResponse)
159159
} else {
160160
this.delegate.requestFailedWithResponse(this, fetchResponse)
161161
}

src/tests/functional/form_submission_tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,11 @@ test("frame POST form targeting frame submission", async ({ page }) => {
647647

648648
await nextEventNamed(page, "turbo:before-fetch-response")
649649

650-
expect(await formSubmitEnded(page), "fires turbo:submit-end").toEqual("true")
651-
652650
await nextEventNamed(page, "turbo:frame-render")
653651
await nextEventNamed(page, "turbo:frame-load")
654652

653+
await nextEventNamed(page, "turbo:submit-end")
654+
655655
const otherEvents = await readEventLogs(page)
656656
expect(otherEvents.length, "no more events").toEqual(0)
657657

@@ -698,11 +698,11 @@ test("frame GET form targeting frame submission", async ({ page }) => {
698698

699699
await nextEventNamed(page, "turbo:before-fetch-response")
700700

701-
expect(await formSubmitEnded(page), "fires turbo:submit-end").toEqual("true")
702-
703701
await nextEventNamed(page, "turbo:frame-render")
704702
await nextEventNamed(page, "turbo:frame-load")
705703

704+
await nextEventNamed(page, "turbo:submit-end")
705+
706706
const otherEvents = await readEventLogs(page)
707707
expect(otherEvents.length, "no more events").toEqual(0)
708708

src/tests/functional/frame_tests.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,9 @@ test("navigating pushing URL state from a frame navigation fires events", async
719719
await nextEventOnTarget(page, "frame", "turbo:before-fetch-response")
720720
await nextEventOnTarget(page, "frame", "turbo:frame-render")
721721
await nextEventOnTarget(page, "frame", "turbo:frame-load")
722-
expect(await nextAttributeMutationNamed(page, "frame", "aria-busy"), "removes aria-busy from the <turbo-frame>").not.toBeTruthy()
722+
expect(await nextAttributeMutationNamed(page, "html", "aria-busy"), "true", "sets aria-busy on the <html>").toBeTruthy()
723723

724-
expect(await nextAttributeMutationNamed(page, "html", "aria-busy"), "sets aria-busy on the <html>").toEqual("true")
724+
expect(await nextAttributeMutationNamed(page, "frame", "aria-busy"), "removes aria-busy from the <turbo-frame>").not.toBeTruthy()
725725
await nextEventOnTarget(page, "html", "turbo:before-visit")
726726
await nextEventOnTarget(page, "html", "turbo:visit")
727727
await nextEventOnTarget(page, "html", "turbo:before-cache")
@@ -933,8 +933,14 @@ test("navigating frame with form[method=post][data-turbo-action=advance] to the
933933
await page.click("#form-post-frame-action-advance button")
934934
await nextEventNamed(page, "turbo:load")
935935

936-
await expect(page.locator("#frame"), "clears turbo-frame[aria-busy]").not.toHaveAttribute("aria-busy")
937-
await expect(page.locator("#html"), "clears html[aria-busy]").not.toHaveAttribute("aria-busy")
936+
expect(await nextAttributeMutationNamed(page, "form-post-frame-action-advance", "aria-busy"), "sets aria-busy on the <form>").toEqual("true")
937+
expect(await nextAttributeMutationNamed(page, "frame", "aria-busy"), "sets aria-busy on the <turbo-frame>").toEqual("true")
938+
expect(await nextAttributeMutationNamed(page, "html", "aria-busy"), "sets aria-busy on the <html>").toEqual("true")
939+
940+
expect(await nextAttributeMutationNamed(page, "form-post-frame-action-advance", "aria-busy"), "removes aria-busy from the <form>").toEqual(null)
941+
expect(await nextAttributeMutationNamed(page, "frame", "aria-busy"), "removes aria-busy from the <turbo-frame>").toEqual(null)
942+
expect(await nextAttributeMutationNamed(page, "html", "aria-busy"), "removes aria-busy from the <html>").toEqual(null)
943+
938944
await expect(page.locator("#html"), "clears html[data-turbo-preview]").not.toHaveAttribute("data-turbo-preview")
939945
await expect(page.locator("#frame"), "marks the frame as [complete]").toHaveAttribute("complete")
940946
})

0 commit comments

Comments
 (0)