Skip to content

Commit

Permalink
await for FetchRequest delegate to handle response
Browse files Browse the repository at this point in the history
Closes [hotwired#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

[hotwired#884]: hotwired#884
  • Loading branch information
seanpdoyle committed Nov 29, 2023
1 parent f92fb0f commit 88c76e1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/core/frames/frame_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -333,7 +333,7 @@ export class FrameController {
this.#currentFetchRequest = null
resolve()
}
request.perform()
return request.perform()
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/http/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/tests/functional/frame_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,9 @@ 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 <turbo-frame>")

assert.equal(await nextAttributeMutationNamed(page, "html", "aria-busy"), "true", "sets aria-busy on the <html>")

assert.notOk(await nextAttributeMutationNamed(page, "frame", "aria-busy"), "removes aria-busy from the <turbo-frame>")
await nextEventOnTarget(page, "html", "turbo:before-visit")
await nextEventOnTarget(page, "html", "turbo:visit")
await nextEventOnTarget(page, "html", "turbo:before-cache")
Expand Down

0 comments on commit 88c76e1

Please sign in to comment.