From 7a7c6e2b83837ff830467106b67f965bca578aa9 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 1 Dec 2023 11:42:59 -0500 Subject: [PATCH] Import `session` instead of reading `window.Turbo.session` Follow-up to [#1049][] Follow-up to [#1073][] Related to [#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"`). [#1049]: https://github.com/hotwired/turbo/pull/1049 [#1073]: https://github.com/hotwired/turbo/pull/1073 [#1078]: https://github.com/hotwired/turbo/pull/1078 --- src/core/index.js | 3 +-- src/core/streams/stream_actions.js | 4 +++- src/index.js | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/core/index.js b/src/core/index.js index cc51ede06..a4a4f2d23 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -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. diff --git a/src/core/streams/stream_actions.js b/src/core/streams/stream_actions.js index 0e0ed2372..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,6 +35,6 @@ export const StreamActions = { }, refresh() { - window.Turbo.session.refresh(this.baseURI, this.requestId) + session.refresh(this.baseURI, this.requestId) } } diff --git a/src/index.js b/src/index.js index 07e3e097d..b83342ce1 100644 --- a/src/index.js +++ b/src/index.js @@ -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"