Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate StreamActions.refresh to Session #1026

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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