hotsock-turbo 0.2.1 → 0.4.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/Gemfile +1 -1
- data/app/assets/javascripts/hotsock-turbo.js +33 -11
- 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: b67599699f411cccd916179d8160bd4275620bfb291c192387a15911c7a5c953
|
|
4
|
+
data.tar.gz: cfaec3f98b3c30eb7d2b9b5c2f67a5e29c2fc7d7e112730f718f106f912b7c3a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9a7940056a3bc984e7ffd12efa3d44d1d5145e407b3fef4ff8379fd1169b741a3661c81fa334648710992ddcd1bcf166c2a76529afff568695b806a6cbb6dab6
|
|
7
|
+
data.tar.gz: b4c9f15c8662985b588e7f92950fb5bd00caffa3ba24d8d3487576b47c3685d8aee772e750455bf0400c956140179f23b25ab10579544c04f47f8f13397f7e58
|
data/Gemfile
CHANGED
|
@@ -8,11 +8,11 @@ function createHotsockClient() {
|
|
|
8
8
|
{
|
|
9
9
|
connectTokenFn: async () => {
|
|
10
10
|
const connectTokenPath = document.querySelector(
|
|
11
|
-
'meta[name="hotsock:connect-token-path"]'
|
|
12
|
-
)
|
|
11
|
+
'meta[name="hotsock:connect-token-path"]',
|
|
12
|
+
)?.content
|
|
13
13
|
const csrfToken = document.querySelector(
|
|
14
|
-
'meta[name="csrf-token"]'
|
|
15
|
-
)
|
|
14
|
+
'meta[name="csrf-token"]',
|
|
15
|
+
)?.content
|
|
16
16
|
const response = await fetch(connectTokenPath, {
|
|
17
17
|
method: "POST",
|
|
18
18
|
headers: {
|
|
@@ -25,17 +25,29 @@ function createHotsockClient() {
|
|
|
25
25
|
lazyConnection: true,
|
|
26
26
|
logLevel: document.querySelector('meta[name="hotsock:log-level"]')
|
|
27
27
|
?.content,
|
|
28
|
-
}
|
|
28
|
+
},
|
|
29
29
|
)
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
let hotsockClient = window.Hotsock
|
|
33
|
+
if (!hotsockClient && document.querySelector('meta[name="hotsock:wss-url"]')) {
|
|
34
|
+
hotsockClient = createHotsockClient()
|
|
35
|
+
window.Hotsock = hotsockClient
|
|
36
|
+
}
|
|
34
37
|
|
|
35
|
-
// Track active subscriptions by channel
|
|
36
38
|
const subscriptions = new Map() // channel -> { binding, elements: Set, unsubscribeTimer }
|
|
39
|
+
const lastMessageIds = new Map() // channel -> lastMessageId (ULID)
|
|
37
40
|
const UNSUBSCRIBE_DELAY_MS = 250
|
|
38
41
|
|
|
42
|
+
function isReplaceOrUpdateAction(html) {
|
|
43
|
+
return /<turbo-stream[^>]+action=["'](replace|update)["']/.test(html)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isNewerMessage(newId, lastId) {
|
|
47
|
+
if (!lastId) return true
|
|
48
|
+
return newId > lastId
|
|
49
|
+
}
|
|
50
|
+
|
|
39
51
|
class HotsockTurboStreamSourceElement extends HTMLElement {
|
|
40
52
|
#channel = null
|
|
41
53
|
|
|
@@ -75,15 +87,24 @@ class HotsockTurboStreamSourceElement extends HTMLElement {
|
|
|
75
87
|
const { Turbo } = window
|
|
76
88
|
const binding = hotsockClient.bind(
|
|
77
89
|
"turbo_stream",
|
|
78
|
-
({ data }) => {
|
|
90
|
+
({ id, data }) => {
|
|
79
91
|
if (!data?.html) return
|
|
92
|
+
|
|
93
|
+
if (isReplaceOrUpdateAction(data.html)) {
|
|
94
|
+
const lastId = lastMessageIds.get(channel)
|
|
95
|
+
if (!isNewerMessage(id, lastId)) {
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
lastMessageIds.set(channel, id)
|
|
99
|
+
}
|
|
100
|
+
|
|
80
101
|
try {
|
|
81
102
|
Turbo.renderStreamMessage(data.html)
|
|
82
103
|
} catch (error) {
|
|
83
104
|
console.error("Failed to render Turbo Stream message:", error)
|
|
84
105
|
}
|
|
85
106
|
},
|
|
86
|
-
{ channel, subscribeTokenFn: () => token }
|
|
107
|
+
{ channel, subscribeTokenFn: () => token },
|
|
87
108
|
)
|
|
88
109
|
|
|
89
110
|
sub = { binding, elements: new Set([this]), unsubscribeTimer: null }
|
|
@@ -115,6 +136,7 @@ class HotsockTurboStreamSourceElement extends HTMLElement {
|
|
|
115
136
|
if (sub.elements.size === 0) {
|
|
116
137
|
sub.binding.unbind()
|
|
117
138
|
subscriptions.delete(channel)
|
|
139
|
+
lastMessageIds.delete(channel)
|
|
118
140
|
}
|
|
119
141
|
}, UNSUBSCRIBE_DELAY_MS)
|
|
120
142
|
}
|
|
@@ -135,7 +157,7 @@ class HotsockTurboStreamSourceElement extends HTMLElement {
|
|
|
135
157
|
if (customElements.get("hotsock-turbo-stream-source") === undefined) {
|
|
136
158
|
customElements.define(
|
|
137
159
|
"hotsock-turbo-stream-source",
|
|
138
|
-
HotsockTurboStreamSourceElement
|
|
160
|
+
HotsockTurboStreamSourceElement,
|
|
139
161
|
)
|
|
140
162
|
}
|
|
141
163
|
|