inertia_cable 0.2.0 → 0.2.2

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: 25d0d8cfcadeb38db163745b520998196f61fa48865253227abe0e3ee22d5686
4
- data.tar.gz: 7f7f8affa983fa0c0a1543d1118cf67eb3abd609606b1c16fac076b8e5767ad4
3
+ metadata.gz: 77598665249ec9fc7f6d8fa69ad070661e2db15513a1720c83e937e318e4b828
4
+ data.tar.gz: 388cc18e32909027f18da2b65352859299120f18984e2a1e7f15fc1e3fff905a
5
5
  SHA512:
6
- metadata.gz: 504d3453823e7cf2887da87a0aa9d56839292e2d8fbe2504e7ca76648d0addafef076e2c655eb3d4a1a88d6f688c7709b60237c3c9ed320676dd9fc7a8544a9f
7
- data.tar.gz: aba9941405aea7ca05100162c5f7555c0257a9aee672662614167b30d4d75b3e32dd418f8e24d1ce8d014e2e81c4638d3e7d2ab172714d14cc677b51f3bac3a3
6
+ metadata.gz: 4b9ae8be00c92656c2ce2787e2ec939028a3cebc77cb7c54adbc2acf4c673cebea5e828fae83e3e8f76bef7ee7c10e266de0eea3cdcce5ba3a3180f2705586ef
7
+ data.tar.gz: 16182946b7e2f836b14aed916c937762002052c8cab9c48617d47eb3599b42e4fcb35b307a1c7c93c59f1e3a08c5f8ef979b09db46a4431a365647a252d6a4b8
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ActionCable broadcast DSL for [Inertia.js](https://inertiajs.com/) Rails applications. Three lines of code to get real-time updates.
4
4
 
5
- InertiaCable broadcasts lightweight JSON signals over ActionCable. The client receives them and calls `router.reload()` to re-fetch props through Inertia's normal HTTP flow. No data travels over the WebSocket — your controller stays the single source of truth.
5
+ InertiaCable broadcasts lightweight JSON signals over ActionCable. The client receives them and calls `router.reload()` to re-fetch props through Inertia's normal HTTP flow — your controller stays the single source of truth. For ephemeral data like job progress or notifications, [direct messages](#direct-messages) stream data over the WebSocket without triggering a reload.
6
6
 
7
7
  ```
8
8
  Model save → after_commit → ActionCable broadcast (signal)
@@ -12,6 +12,8 @@ React hook subscribes → receives signal → router.reload({ only: ['messages']
12
12
  Inertia HTTP request → controller re-evaluates props → React re-renders
13
13
  ```
14
14
 
15
+ > **Coming from Turbo Streams?** `broadcasts_to` replaces `broadcasts_refreshes_to`, and `broadcast_message_to` covers use cases where you'd reach for `broadcast_append_to` or `broadcast_replace_to` — but without HTML partials, since Inertia reloads props from your controller instead.
16
+
15
17
  ## Table of Contents
16
18
 
17
19
  - [Installation](#installation)
@@ -381,6 +383,15 @@ useInertiaCable(stream, {
381
383
  })
382
384
  ```
383
385
 
386
+ ### Broadcasting without a model instance
387
+
388
+ Use `InertiaCable.broadcast_message_to` from anywhere — jobs, services, controllers — without needing a model instance:
389
+
390
+ ```ruby
391
+ InertiaCable.broadcast_message_to("dashboard", data: { alert: "Deployment complete" })
392
+ InertiaCable.broadcast_message_to(user, :notifications, data: { count: 5 })
393
+ ```
394
+
384
395
  Messages are delivered immediately with no debouncing. Each `broadcast_message_to` call triggers exactly one `onMessage` callback.
385
396
 
386
397
  ---
@@ -407,13 +418,24 @@ end
407
418
 
408
419
  Optionally coalesce rapid broadcasts using Rails cache. **Not used by default** — the client-side 100ms debounce handles most cases.
409
420
 
410
- Requires a shared cache store (Redis, Memcached, or SolidCache) in multi-process deployments.
421
+ Server-side debounce is useful when a single operation triggers many model callbacks (e.g., bulk imports, cascading updates) and you want to reduce the number of ActionCable messages sent. The client-side debounce already coalesces rapid reloads into one, so server-side debounce is only needed when the volume of WebSocket messages itself is a concern.
422
+
423
+ Requires a shared cache store (Redis, Memcached, or SolidCache) in multi-process deployments. `MemoryStore` (the Rails default) only works within a single process.
411
424
 
412
425
  ```ruby
413
- InertiaCable.debounce_delay = 0.5 # seconds (default)
426
+ # Via the model DSL
427
+ broadcasts_to :board, debounce: true # uses InertiaCable.debounce_delay (0.5s)
428
+ broadcasts_to :board, debounce: 1.0 # custom delay in seconds
429
+
430
+ # Via instance methods
431
+ post.broadcast_refresh_later_to(board, debounce: 2.0)
414
432
 
433
+ # Direct usage
415
434
  InertiaCable::Debounce.broadcast("my_stream", payload)
416
435
  InertiaCable::Debounce.broadcast("my_stream", payload, delay: 2.0)
436
+
437
+ # Configure the global default
438
+ InertiaCable.debounce_delay = 0.5 # seconds (default)
417
439
  ```
418
440
 
419
441
  ---
@@ -464,6 +486,21 @@ end
464
486
 
465
487
  All three accept splat streamables: `assert_broadcasts_on(chat, :messages) { ... }`
466
488
 
489
+ ### Broadcast callbacks
490
+
491
+ `InertiaCable.on_broadcast` registers a callback that fires for every broadcast (including debounced ones). The test helpers use this internally, but you can use it for custom instrumentation or logging:
492
+
493
+ ```ruby
494
+ callback = ->(stream_name, payload) {
495
+ Rails.logger.info "[InertiaCable] #{payload[:type]} on #{stream_name}"
496
+ }
497
+
498
+ InertiaCable.on_broadcast(&callback)
499
+
500
+ # Later, to unregister:
501
+ InertiaCable.off_broadcast(&callback)
502
+ ```
503
+
467
504
  ---
468
505
 
469
506
  ## Configuration
@@ -6,6 +6,7 @@ module InertiaCable
6
6
  return if Rails.cache.exist?(cache_key)
7
7
 
8
8
  Rails.cache.write(cache_key, true, expires_in: delay)
9
+ InertiaCable.broadcast_callbacks.each { |cb| cb.call(stream_name, payload) }
9
10
  ActionCable.server.broadcast(stream_name, payload)
10
11
  end
11
12
  end
@@ -1,3 +1,3 @@
1
1
  module InertiaCable
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/inertia_cable.rb CHANGED
@@ -28,6 +28,15 @@ module InertiaCable
28
28
  ActionCable.server.broadcast(resolved, payload)
29
29
  end
30
30
 
31
+ # Broadcast a direct message without a model instance.
32
+ #
33
+ # InertiaCable.broadcast_message_to("dashboard", data: { alert: "done" })
34
+ # InertiaCable.broadcast_message_to(user, :notifications, data: { count: 5 })
35
+ #
36
+ def self.broadcast_message_to(*streamables, data:)
37
+ broadcast(streamables, { type: "message", data: data })
38
+ end
39
+
31
40
  def self.suppressing_broadcasts(&block)
32
41
  InertiaCable::Suppressor.suppressing(&block)
33
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inertia_cable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cole Robertson