From c753e95b362db7cdd1dccb52832e3d1926d74148 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Sun, 8 Oct 2023 18:25:10 -0400 Subject: [PATCH] Delegate `StreamActions.refresh` to `Session` 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. --- src/core/session.js | 8 ++++++++ src/core/streams/stream_actions.js | 9 +++------ src/elements/stream_element.js | 7 +++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/core/session.js b/src/core/session.js index 9bf990dc4..33f8f0449 100644 --- a/src/core/session.js +++ b/src/core/session.js @@ -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) } diff --git a/src/core/streams/stream_actions.js b/src/core/streams/stream_actions.js index 631767698..064e94ca4 100644 --- a/src/core/streams/stream_actions.js +++ b/src/core/streams/stream_actions.js @@ -1,3 +1,5 @@ +import { session } from "../" + export const StreamActions = { after() { this.targetElements.forEach((e) => e.parentElement?.insertBefore(this.templateContent, e.nextSibling)) @@ -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) } } diff --git a/src/elements/stream_element.js b/src/elements/stream_element.js index 38f463fb3..cd6dd3321 100644 --- a/src/elements/stream_element.js +++ b/src/elements/stream_element.js @@ -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}`) }