hotsock-turbo 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50980b51f56e5152a8b79b0c3f4fa6a21371d7c8a10c91e36966935fc1c4e074
4
- data.tar.gz: 1de2e6377d7db18dded354dfe6fbe6709c44fb7944837c83a7ffc519db0305b6
3
+ metadata.gz: 9e0c4f17c8d5a1de4293baf2a18161370c42dda911b960c826c1feb7389d09e6
4
+ data.tar.gz: 2bb1a931c63f385a146edc83ee1c2c999ccf32bf630c6494d263ee92c9118be7
5
5
  SHA512:
6
- metadata.gz: 8f171ae0a44c991eab8c4d75819210c2a3d802b8956c50c4f64c9bb22347a2c0fc1a9e2cba40e98874dfbd69f0f97d58fef7199c8ffe2d8cbb0e6c2ec09cdd03
7
- data.tar.gz: 16c93f748a8ce00867120dd8b6a50f49f3f66f5175960b2350fbb6817f66c0177037e6c81d83be2cd907a87e7855b4953bcb70b78ac1e0a64b534acd0a5160b1
6
+ metadata.gz: fcd31ac1f6d52ce280b8f282e3d84f97963be0330d9fd5544ff406c406a124eced6669f7ba63afc524b2a3fb2ada3ac6cae28de5b122df469c4b869376aea066
7
+ data.tar.gz: c293c5a55e8fa202dfb40aa2738cc6e8b8996215ccf90996177e4fcb06603de6993a8caf30d0a98dcbdc5de4b91c82cae3822285bae2402544b675f9bf006193
@@ -32,11 +32,33 @@ function createHotsockClient() {
32
32
  }
33
33
 
34
34
  let hotsockClient = window.Hotsock
35
- if (!hotsockClient && document.querySelector('meta[name="hotsock:wss-url"]')) {
36
- hotsockClient = createHotsockClient()
37
- window.Hotsock = hotsockClient
35
+ let hotsockClientOwned = false
36
+
37
+ function ensureHotsockClient() {
38
+ const wssUrl = document
39
+ .querySelector('meta[name="hotsock:wss-url"]')
40
+ ?.getAttribute("content")
41
+ if (!wssUrl) return
42
+
43
+ if (
44
+ hotsockClient &&
45
+ hotsockClientOwned &&
46
+ hotsockClient.webSocketUrl !== wssUrl
47
+ ) {
48
+ hotsockClient.terminate()
49
+ hotsockClient = null
50
+ window.Hotsock = null
51
+ }
52
+
53
+ if (!hotsockClient) {
54
+ hotsockClient = createHotsockClient()
55
+ hotsockClientOwned = true
56
+ window.Hotsock = hotsockClient
57
+ }
38
58
  }
39
59
 
60
+ ensureHotsockClient()
61
+
40
62
  const subscriptions = new Map() // channel -> { binding, elements: Set, unsubscribeTimer }
41
63
  const lastMessageIds = new Map() // channel -> lastMessageId (ULID)
42
64
  const UNSUBSCRIBE_DELAY_MS = 250
@@ -163,4 +185,22 @@ if (customElements.get("hotsock-turbo-stream-source") === undefined) {
163
185
  )
164
186
  }
165
187
 
188
+ // Before Turbo renders a new page (morph or replace), disconnect the websocket
189
+ // if hotsock-turbo owns the connection and the new page no longer has the
190
+ // hotsock:wss-url meta tag. Turbo merges the new <head> before this event
191
+ // fires, so we can check document.head directly.
192
+ document.addEventListener("turbo:before-render", () => {
193
+ if (!hotsockClientOwned || !hotsockClient) return
194
+
195
+ if (!document.querySelector('meta[name="hotsock:wss-url"]')) {
196
+ hotsockClient.terminate()
197
+ }
198
+ })
199
+
200
+ // After Turbo renders a new page, create the client if the meta tag appeared
201
+ // (e.g. navigating from a login page to the application).
202
+ document.addEventListener("turbo:load", () => {
203
+ ensureHotsockClient()
204
+ })
205
+
166
206
  export { hotsockClient }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Hotsock
4
4
  module Turbo
5
- VERSION = "0.7.0"
5
+ VERSION = "0.8.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotsock-turbo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Miller