Skip to content

Commit

Permalink
Import session instead of reading window.Turbo.session
Browse files Browse the repository at this point in the history
Follow-up to [hotwired#1049][]
Follow-up to [hotwired#1073][]
Related to [hotwired#1078][]

Replace an internal `window.Turbo.session` access with an `import {
session }` statement.

Prior attempts at this change introduced circular dependencies. This
attempt does not. Additionally, this change maintains the ability for
consumers to access StreamActions through `window.Turbo.StreamActions`,
while also enabling `import { StreamActions } from "@hotwired/turbo"`
(or `from "@hotwired/turbo-rails"`).

[hotwired#1049]: hotwired#1049
[hotwired#1073]: hotwired#1073
[hotwired#1078]: hotwired#1078
  • Loading branch information
seanpdoyle committed Dec 1, 2023
1 parent f92fb0f commit 7a7c6e2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ 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 { StreamActions } from "./streams/stream_actions"
import { fetch, recentRequests } from "../http/fetch"

const session = new Session(recentRequests)
const { cache, navigator } = session
export { navigator, session, cache, PageRenderer, PageSnapshot, FrameRenderer, StreamActions, fetch }
export { navigator, session, cache, PageRenderer, PageSnapshot, FrameRenderer, fetch }

/**
* Starts the main session.
Expand Down
4 changes: 3 additions & 1 deletion 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,6 +35,6 @@ export const StreamActions = {
},

refresh() {
window.Turbo.session.refresh(this.baseURI, this.requestId)
session.refresh(this.baseURI, this.requestId)
}
}
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import "./polyfills"
import "./elements"
import "./script_warning"
import { StreamActions } from "./core/streams/stream_actions"

import * as Turbo from "./core"

window.Turbo = Turbo
window.Turbo = { ...Turbo, StreamActions }
Turbo.start()

export { StreamActions }
export * from "./core"
export * from "./elements"
export * from "./http"

0 comments on commit 7a7c6e2

Please sign in to comment.