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 8, 2023
1 parent 328c93a commit c753e95
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,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.session.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

0 comments on commit c753e95

Please sign in to comment.