Skip to content

Commit

Permalink
Delegate StreamActions.refresh to Session
Browse files Browse the repository at this point in the history
The bulk of the `StreamActions.refresh` implementation was reaching
through the global `window.Turbo` property, which itself was reaching
through either global variables or the `Session`.

This commit moves the implementation out of the `StreamActions` and into
a new `Session.refresh(url, requestId)` method. With that change, all
property access is encapsulated within the `Session`.

To support that change, this commit also introduces the
`StreamElement.requestId` property to read the `[request-id]` attribute.
  • Loading branch information
seanpdoyle committed Oct 23, 2023
1 parent a653d12 commit 0bfb97e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
8 changes: 2 additions & 6 deletions src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Session } from "./session"
import { Cache } from "./cache"
import { PageRenderer } from "./drive/page_renderer"
import { PageSnapshot } from "./drive/page_snapshot"
import { FrameRenderer } from "./frames/frame_renderer"
import { FormSubmission } from "./drive/form_submission"
import { LimitedSet } from "./drive/limited_set"

const session = new Session()
const cache = new Cache(session)
const recentRequests = new LimitedSet(20)
const { navigator } = session
export { navigator, session, cache, recentRequests, PageRenderer, PageSnapshot, FrameRenderer }
const { cache, navigator } = session
export { navigator, session, cache, PageRenderer, PageSnapshot, FrameRenderer }

export { StreamActions } from "./streams/stream_actions"

Expand Down
12 changes: 12 additions & 0 deletions src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { clearBusyState, dispatch, findClosestRecursively, getVisitAction, markA
import { PageView } from "./drive/page_view"
import { FrameElement } from "../elements/frame_element"
import { Preloader } from "./drive/preloader"
import { LimitedSet } from "./drive/limited_set"
import { Cache } from "./cache"

export class Session {
navigator = new Navigator(this)
Expand All @@ -33,6 +35,8 @@ export class Session {
formLinkClickObserver = new FormLinkClickObserver(this, document.documentElement)
frameRedirector = new FrameRedirector(this, document.documentElement)
streamMessageRenderer = new StreamMessageRenderer()
cache = new Cache(this)
recentRequests = new LimitedSet(20)

drive = true
enabled = true
Expand Down Expand Up @@ -91,6 +95,14 @@ export class Session {
}
}

refresh(url, requestId) {
const isRecentRequest = requestId && this.recentRequests.has(requestId)
if (!isRecentRequest) {
this.cache.exemptPageFromPreview()
this.visit(url, { action: "replace" })
}
}

connectStreamSource(source) {
this.streamObserver.connectStreamSource(source)
}
Expand Down
9 changes: 3 additions & 6 deletions src/core/streams/stream_actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { session } from "../"

export const StreamActions = {
after() {
this.targetElements.forEach((e) => e.parentElement?.insertBefore(this.templateContent, e.nextSibling))
Expand Down Expand Up @@ -33,11 +35,6 @@ export const StreamActions = {
},

refresh() {
const requestId = this.getAttribute("request-id")
const isRecentRequest = requestId && window.Turbo.recentRequests.has(requestId)
if (!isRecentRequest) {
window.Turbo.cache.exemptPageFromPreview()
window.Turbo.visit(window.location.href, { action: "replace" })
}
session.refresh(this.baseURI, this.requestId)
}
}
7 changes: 7 additions & 0 deletions src/elements/stream_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ export class StreamElement extends HTMLElement {
return this.getAttribute("targets")
}

/**
* Reads the request-id attribute
*/
get requestId() {
return this.getAttribute("request-id")
}

#raise(message) {
throw new Error(`${this.description}: ${message}`)
}
Expand Down
2 changes: 1 addition & 1 deletion src/http/recent_fetch_requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const originalFetch = window.fetch
window.fetch = async function(url, options = {}) {
const modifiedHeaders = new Headers(options.headers || {})
const requestUID = uuid()
window.Turbo.recentRequests.add(requestUID)
window.Turbo.session.recentRequests.add(requestUID)
modifiedHeaders.append("X-Turbo-Request-Id", requestUID)

return originalFetch(url, {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/functional/page_refresh_stream_action_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test("don't refresh the page on self-originated request ids", async ({ page }) =
assert.match(await textContent(page), /Hello/)

await page.locator("#content").evaluate((content)=>content.innerHTML = "")
page.evaluate(()=> { window.Turbo.recentRequests.add("123") })
page.evaluate(()=> { window.Turbo.session.recentRequests.add("123") })

await page.locator("#request-id").evaluate((input)=>input.value = "123")
await page.click("#refresh button")
Expand Down
2 changes: 1 addition & 1 deletion src/tests/unit/stream_element_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ test("test action=refresh", async () => {
})

test("test action=refresh discarded when matching request id", async () => {
Turbo.recentRequests.add("123")
Turbo.session.recentRequests.add("123")

document.body.setAttribute("data-modified", "")
assert.ok(document.body.hasAttribute("data-modified"))
Expand Down

0 comments on commit 0bfb97e

Please sign in to comment.