@isometriks/live_cable 0.2.0 → 0.2.1

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.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,42 @@ The Ruby gem (`live_cable`) and the npm package (`@isometriks/live_cable`) are
6
6
  released together and share a single version number. Entries below note which
7
7
  side of the pair a change affects when it isn't both.
8
8
 
9
+ ## 0.2.1 - 2026-08-13
10
+
11
+ ### Removed
12
+
13
+ - `Component#channel_name` and `Connection#channel_name`. A component no longer
14
+ subscribes to a stream of its own now that payloads are written straight to its
15
+ channel, so the name identified a stream nothing published to or read from.
16
+ Subscribing to external streams with `stream_from` is unaffected (gem).
17
+
18
+ ### Fixed
19
+
20
+ - **The opening payload from `LiveChannel#subscribed` could be dropped.**
21
+ Payloads were published through the pubsub adapter, but ActionCable registers
22
+ `stream_from` asynchronously — so the initial render could be published before
23
+ the subscription it targets existed, and pubsub delivers only to subscribers
24
+ present at that moment, with no buffering or retry. The component was left
25
+ showing `data-live-status-value="disconnected"`, with no cached render on the
26
+ client for a Turbo reattach to replay, until some later action happened to
27
+ produce a refresh. When a subscribe render raised, the `_error` payload was
28
+ lost outright and the failure never surfaced in the browser at all. A
29
+ component's stream belongs to exactly one connection, so payloads are now
30
+ written straight to that connection rather than published, which removes the
31
+ race along with a pubsub round trip (gem).
32
+ - **Components not reattaching after a Turbo Drive navigation.** Navigating to a
33
+ page containing a component that is already subscribed deliberately keeps the
34
+ existing subscription, so `LiveChannel#subscribed` does not run again and the
35
+ server sends nothing. The newly rendered element was left with its
36
+ server-rendered status of `disconnected` and never received the component's
37
+ current state. A reconnecting controller now re-syncs the subscription's status
38
+ and replays the last render into the new element (npm).
39
+ - Building DOM from a server render used iterator helpers
40
+ (`childNodes.values().find(...)`), an ES2025 feature, which raised
41
+ `TypeError: ... .find is not a function` on runtimes that do not implement them
42
+ — Node 20 and earlier, and browsers older than Chrome 122 / Firefox 131 /
43
+ Safari 18.4. Rewritten with `Array.from` (npm).
44
+
9
45
  ## 0.2.0
10
46
 
11
47
  The gem and npm package versions are realigned in this release. The npm package
@@ -34,8 +34,7 @@ function createDOMFromHTML(html) {
34
34
  template.innerHTML = html
35
35
 
36
36
  // Find a node that isn't a comment
37
- const node = template.content.childNodes
38
- .values()
37
+ const node = Array.from(template.content.childNodes)
39
38
  .find(n => n.nodeName !== '#comment')
40
39
 
41
40
  if (node) {
@@ -230,6 +229,15 @@ class ComponentState {
230
229
  return this.#element
231
230
  }
232
231
 
232
+ /**
233
+ * Whether a render has been received and cached, and can therefore be
234
+ * replayed into a freshly attached element.
235
+ * @returns {boolean}
236
+ */
237
+ get hasRender() {
238
+ return this.#lastTemplate !== null && Boolean(this.#partsByTemplate[this.#lastTemplate])
239
+ }
240
+
233
241
  /**
234
242
  * Create a DOM element from stored state.
235
243
  * @param {Object} refresh - Optional refresh data to update state
@@ -345,12 +353,44 @@ class Subscription {
345
353
 
346
354
  /**
347
355
  * Update the controller reference.
348
- * Called when a Stimulus controller reconnects to an existing subscription.
356
+ * Called when a Stimulus controller reconnects to an existing subscription —
357
+ * most importantly after a Turbo Drive navigation to a page that contains the
358
+ * same component, where prune() deliberately keeps the subscription alive.
359
+ *
360
+ * In that case the ActionCable subscription is never recreated, so
361
+ * LiveChannel#subscribed does not run again and the server sends nothing.
362
+ * Without re-syncing here, the newly rendered element keeps the
363
+ * server-rendered status of "disconnected" forever and never receives the
364
+ * component's current state.
349
365
  *
350
366
  * @param {Object} controller - Stimulus controller instance
351
367
  */
352
368
  set controller(controller) {
369
+ const previous = this.#controller
370
+
353
371
  this.#controller = controller
372
+ this.#componentState.element = controller.element
373
+
374
+ if (previous && previous !== controller) {
375
+ this.#reattach()
376
+ }
377
+ }
378
+
379
+ /**
380
+ * Bring a freshly connected controller up to date with state this
381
+ * subscription already holds.
382
+ * @private
383
+ */
384
+ #reattach() {
385
+ if (this.#currentStatus) {
386
+ this.#controller.statusValue = this.#currentStatus
387
+ }
388
+
389
+ // Replay the last render into the new element. The server-side component
390
+ // is the same instance, so the cached parts are its current state.
391
+ if (this.#componentState.hasRender) {
392
+ this.#handleRefresh(null)
393
+ }
354
394
  }
355
395
 
356
396
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isometriks/live_cable",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "app/assets/javascript",