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 +4 -4
- data/app/assets/javascripts/hotsock-turbo.js +43 -3
- data/lib/hotsock/turbo/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9e0c4f17c8d5a1de4293baf2a18161370c42dda911b960c826c1feb7389d09e6
|
|
4
|
+
data.tar.gz: 2bb1a931c63f385a146edc83ee1c2c999ccf32bf630c6494d263ee92c9118be7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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 }
|