From 23d0cb4357ad1789b8fc98899c6bf72459e2b27d Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Sun, 26 Nov 2023 21:51:23 -0500 Subject: [PATCH] Restructure to avoid internal `window.Turbo` access Replace accessing the `recentRequests` property through the `window.Turbo.session` object. In its place, restructure the object's dependency graph so that the `http/fetch` module constructs the `LimitedSet` instance, and the `Session` accepts that instance as a property. --- src/core/index.js | 4 ++-- src/core/session.js | 6 ++++-- src/http/fetch.js | 5 ++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/core/index.js b/src/core/index.js index bfa0148d8..cc51ede06 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -4,9 +4,9 @@ import { PageSnapshot } from "./drive/page_snapshot" import { FrameRenderer } from "./frames/frame_renderer" import { FormSubmission } from "./drive/form_submission" import { StreamActions } from "./streams/stream_actions" -import { fetch } from "../http/fetch" +import { fetch, recentRequests } from "../http/fetch" -const session = new Session() +const session = new Session(recentRequests) const { cache, navigator } = session export { navigator, session, cache, PageRenderer, PageSnapshot, FrameRenderer, StreamActions, fetch } diff --git a/src/core/session.js b/src/core/session.js index 928e03aae..be28a8287 100644 --- a/src/core/session.js +++ b/src/core/session.js @@ -16,7 +16,6 @@ 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 { @@ -36,7 +35,6 @@ export class Session { frameRedirector = new FrameRedirector(this, document.documentElement) streamMessageRenderer = new StreamMessageRenderer() cache = new Cache(this) - recentRequests = new LimitedSet(20) drive = true enabled = true @@ -44,6 +42,10 @@ export class Session { started = false formMode = "on" + constructor(recentRequests) { + this.recentRequests = recentRequests + } + start() { if (!this.started) { this.pageObserver.start() diff --git a/src/http/fetch.js b/src/http/fetch.js index c69b8c270..b70a0bbdd 100644 --- a/src/http/fetch.js +++ b/src/http/fetch.js @@ -1,11 +1,14 @@ import { uuid } from "../util" +import { LimitedSet } from "../core/drive/limited_set" + +export const recentRequests = new LimitedSet(20) const nativeFetch = window.fetch function fetchWithTurboHeaders(url, options = {}) { const modifiedHeaders = new Headers(options.headers || {}) const requestUID = uuid() - window.Turbo.session.recentRequests.add(requestUID) + recentRequests.add(requestUID) modifiedHeaders.append("X-Turbo-Request-Id", requestUID) return nativeFetch(url, {