Skip to content

Commit

Permalink
Restructure to avoid internal window.Turbo access
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
seanpdoyle committed Nov 27, 2023
1 parent 1aa7a87 commit db84bb5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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 { 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, fetch }

Expand Down
6 changes: 4 additions & 2 deletions src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,14 +35,17 @@ export class Session {
frameRedirector = new FrameRedirector(this, document.documentElement)
streamMessageRenderer = new StreamMessageRenderer()
cache = new Cache(this)
recentRequests = new LimitedSet(20)

drive = true
enabled = true
progressBarDelay = 500
started = false
formMode = "on"

constructor(recentRequests) {
this.recentRequests = recentRequests
}

start() {
if (!this.started) {
this.pageObserver.start()
Expand Down
5 changes: 4 additions & 1 deletion src/http/fetch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { uuid } from "../util"
import { LimitedSet } from "../core/drive/limited_set"

export const recentRequests = new LimitedSet(20)

export function fetch(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 window.fetch(url, {
Expand Down

0 comments on commit db84bb5

Please sign in to comment.