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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa8e10033b8676f3818b4c8988046b24f410258c07f0632cd4082e9a1ed9865d
4
- data.tar.gz: 575ebb35a85d1bc5ab915b6ab83c32dba6bdb542cd36c8fde03c6ad21a38946b
3
+ metadata.gz: b67599699f411cccd916179d8160bd4275620bfb291c192387a15911c7a5c953
4
+ data.tar.gz: cfaec3f98b3c30eb7d2b9b5c2f67a5e29c2fc7d7e112730f718f106f912b7c3a
5
5
  SHA512:
6
- metadata.gz: 443b7bae1effc3d21e06d5d1249cf8d93fda03a38f8876eacc60d9d414360887e8b20d88689a508a7b181124493bdd2ca8af2b81750fbeafc8324903bec3aa4d
7
- data.tar.gz: b29f910dd278d6143812b0ab65ebbb01deb75dbde72614084880e10be081a1a1450fe7730d443a119f9e4230f09191275f100143f0d43525fb2177121f82f1c9
6
+ metadata.gz: 9a7940056a3bc984e7ffd12efa3d44d1d5145e407b3fef4ff8379fd1169b741a3661c81fa334648710992ddcd1bcf166c2a76529afff568695b806a6cbb6dab6
7
+ data.tar.gz: b4c9f15c8662985b588e7f92950fb5bd00caffa3ba24d8d3487576b47c3685d8aee772e750455bf0400c956140179f23b25ab10579544c04f47f8f13397f7e58
data/Gemfile CHANGED
@@ -11,6 +11,6 @@ group :development, :test do
11
11
  end
12
12
 
13
13
  group :test do
14
- gem "maxitest"
14
+ gem "maxitest", "< 7"
15
15
  gem "ostruct"
16
16
  end
@@ -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
- ).content
11
+ 'meta[name="hotsock:connect-token-path"]',
12
+ )?.content
13
13
  const csrfToken = document.querySelector(
14
- 'meta[name="csrf-token"]'
15
- ).content
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
- const hotsockClient = window.Hotsock || createHotsockClient()
33
- window.Hotsock = hotsockClient
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
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Hotsock
4
4
  module Turbo
5
- VERSION = "0.2.1"
5
+ VERSION = "0.4.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.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Miller