live_cable 0.1.1 → 0.2.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/CHANGELOG.md +82 -0
- data/README.md +94 -8
- data/app/assets/javascript/controllers/live_controller.js +32 -5
- data/app/assets/javascript/live_cable.js +3 -3
- data/app/assets/javascript/live_cable_blessing.js +1 -1
- data/app/assets/javascript/loading.js +162 -0
- data/app/assets/javascript/observer.js +1 -1
- data/app/assets/javascript/subscriptions.js +63 -3
- data/config/importmap.rb +7 -6
- data/lib/live.rb +5 -0
- data/lib/live_cable/component/broadcasting.rb +14 -3
- data/lib/live_cable/component/events.rb +49 -0
- data/lib/live_cable/component.rb +1 -0
- data/lib/live_cable/connection/broadcasting.rb +13 -1
- data/lib/live_cable/connection/messaging.rb +18 -2
- data/lib/live_cable/engine.rb +4 -2
- data/lib/live_cable/rendering/compiler.rb +12 -6
- data/lib/live_cable/testing/render_state.rb +52 -0
- data/lib/live_cable/testing/test_cable_connection.rb +21 -0
- data/lib/live_cable/testing/test_channel.rb +49 -0
- data/lib/live_cable/testing/test_component.rb +147 -0
- data/lib/live_cable/testing.rb +84 -0
- data/lib/live_cable/version.rb +1 -1
- data/lib/live_cable.rb +6 -4
- metadata +39 -12
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a08ab617cc974271c9f4b2784bb52ca53d1430942fd72e761fe52cd650a86141
|
|
4
|
+
data.tar.gz: abd1b26ca97b149de015b9fb7b9b457ff6b6f21e224a61318788876115fcd9d9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e3b932fcbdfcf3fb5d6b939320798eb151c82d0e6bc85c61c2452ff729233855b477752e10465c8098f4601651d04f9e67449293b9635cde3eefa47bde08733c
|
|
7
|
+
data.tar.gz: 6476ba238c1960655922da93588b175ed0468294364dd1252b05ad044eb26cb0d94360254581cb791ddc6def81155940c82d310835398b7c791b8cb3daf6884c
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The Ruby gem (`live_cable`) and the npm package (`@isometriks/live_cable`) are
|
|
6
|
+
released together and share a single version number. Entries below note which
|
|
7
|
+
side of the pair a change affects when it isn't both.
|
|
8
|
+
|
|
9
|
+
## 0.2.0
|
|
10
|
+
|
|
11
|
+
The gem and npm package versions are realigned in this release. The npm package
|
|
12
|
+
jumps from 0.1.1 straight to 0.2.0, skipping 0.1.2, which was published for the
|
|
13
|
+
gem only.
|
|
14
|
+
|
|
15
|
+
### Added
|
|
16
|
+
|
|
17
|
+
- **Server-dispatched DOM events.** Components can queue browser events with
|
|
18
|
+
`dispatch_event`, delivered with the next broadcast and fired after the DOM has
|
|
19
|
+
been morphed, so handlers observe the updated markup. Events are bubbling
|
|
20
|
+
`CustomEvent`s dispatched from the component root (or from `window` with
|
|
21
|
+
`window: true`), so they can be wired up with plain Stimulus `data-action`
|
|
22
|
+
syntax. (`LiveCable::Component::Events`)
|
|
23
|
+
- **Loading states.** While a message is in flight, a `live-loading` attribute is
|
|
24
|
+
added to the component's root element and to the element that triggered the
|
|
25
|
+
message, so pending feedback can be styled with plain CSS. `live-disable-with`
|
|
26
|
+
on a button or submit button swaps its label and disables it for the duration
|
|
27
|
+
of the round trip; form values are serialized before anything is disabled.
|
|
28
|
+
Reactive inputs (`live-reactive`) receive `live-loading` but are never
|
|
29
|
+
disabled, so typing is not interrupted.
|
|
30
|
+
- **Component test harness.** `LiveCable::Testing` can be included in specs to
|
|
31
|
+
mount and drive components without a browser, via `live_mount`.
|
|
32
|
+
- `./loading` subpath export for the new loading module (npm).
|
|
33
|
+
- Gemspec `homepage_uri`, `documentation_uri`, and `bug_tracker_uri` metadata.
|
|
34
|
+
|
|
35
|
+
### Changed
|
|
36
|
+
|
|
37
|
+
- Minimum Rails version raised from 7.0 to 7.1 (`actioncable`, `actionview`,
|
|
38
|
+
`activemodel`, `activesupport`) (gem).
|
|
39
|
+
- Herb upgraded from `~> 0.8.10` to `~> 0.10.2`, and `prism >= 1.0` added as an
|
|
40
|
+
explicit dependency (gem).
|
|
41
|
+
- Gem homepage now points at https://livecable.io rather than the RubyGems page.
|
|
42
|
+
- The `Live` namespace for user components moved out of `lib/live_cable.rb` into
|
|
43
|
+
its own `lib/live.rb`, required explicitly and ignored by the gem's Zeitwerk
|
|
44
|
+
loader (gem).
|
|
45
|
+
- Dev dependencies updated: Vitest 2.x to 4.x, happy-dom 15.x to 20.x (npm).
|
|
46
|
+
|
|
47
|
+
### Fixed
|
|
48
|
+
|
|
49
|
+
- Template compiler: block sentinel tokens now carry a newline value so Herb's
|
|
50
|
+
whitespace helpers (`at_line_start?`, `preceding_token_ends_with_newline?`)
|
|
51
|
+
treat them as a line boundary instead of raising on a `nil` value.
|
|
52
|
+
- Template compiler: the closing `end` of an output block is emitted as plain
|
|
53
|
+
code instead of going through Herb's paren-balancing block-end helper, since
|
|
54
|
+
escaping is delegated to Rails' output buffer.
|
|
55
|
+
|
|
56
|
+
## 0.1.2 - 2026-03-25
|
|
57
|
+
|
|
58
|
+
Gem only; no corresponding npm release.
|
|
59
|
+
|
|
60
|
+
### Added
|
|
61
|
+
|
|
62
|
+
- JavaScript assets packaged so they can be consumed from package managers as
|
|
63
|
+
well as through the asset pipeline.
|
|
64
|
+
|
|
65
|
+
## 0.1.1 - 2026-03-25
|
|
66
|
+
|
|
67
|
+
### Added
|
|
68
|
+
|
|
69
|
+
- Component generator.
|
|
70
|
+
|
|
71
|
+
### Changed
|
|
72
|
+
|
|
73
|
+
- Herb pinned to `0.8.*`, and other library versions pinned.
|
|
74
|
+
|
|
75
|
+
### Fixed
|
|
76
|
+
|
|
77
|
+
- `render` inside a live template.
|
|
78
|
+
- Generator no longer creates `app/live` when the directory does not exist.
|
|
79
|
+
|
|
80
|
+
## 0.1.0
|
|
81
|
+
|
|
82
|
+
Initial public release.
|
data/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
LiveCable is a Phoenix LiveView-style live component system for Ruby on Rails that tracks state server-side and allows
|
|
4
4
|
you to call actions from the frontend using Stimulus with a React style state management API.
|
|
5
5
|
|
|
6
|
-
**Full documentation: [livecable.io](https://livecable.io)**
|
|
6
|
+
**Full documentation: [livecable.io](https://livecable.io)** | **Sandbox app: [live_cable_app](https://github.com/isometriks/live_cable_app)**
|
|
7
7
|
|
|
8
8
|
## Features
|
|
9
9
|
|
|
@@ -48,14 +48,32 @@ end
|
|
|
48
48
|
|
|
49
49
|
## JavaScript Setup
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
LiveCable works with both **importmap-rails** (default for Rails 7+) and **JavaScript bundlers** (esbuild, Vite, webpack, Rollup via jsbundling-rails or vite_ruby).
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
### Using importmap-rails (default)
|
|
54
|
+
|
|
55
|
+
The gem automatically pins all necessary modules when you install it. No additional setup is needed.
|
|
56
|
+
|
|
57
|
+
### Using a JavaScript bundler (yarn/npm)
|
|
58
|
+
|
|
59
|
+
Install the npm package:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
yarn add @isometriks/live_cable
|
|
63
|
+
# or
|
|
64
|
+
npm install @isometriks/live_cable
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
This will also install the required peer dependencies (`@rails/actioncable`, `@hotwired/stimulus`, and `morphdom`).
|
|
68
|
+
|
|
69
|
+
### Register the LiveController
|
|
70
|
+
|
|
71
|
+
Register the `LiveController` in your Stimulus application (`app/javascript/controllers/application.js`). The imports are the same regardless of whether you use importmap or a bundler:
|
|
54
72
|
|
|
55
73
|
```javascript
|
|
56
74
|
import { Application } from "@hotwired/stimulus"
|
|
57
|
-
import LiveController from "
|
|
58
|
-
import "live_cable" // Automatically starts DOM observer
|
|
75
|
+
import LiveController from "@isometriks/live_cable/controller"
|
|
76
|
+
import "@isometriks/live_cable" // Automatically starts DOM observer
|
|
59
77
|
|
|
60
78
|
const application = Application.start()
|
|
61
79
|
application.register("live", LiveController)
|
|
@@ -69,9 +87,9 @@ If you want to call LiveCable actions from your own Stimulus controllers, add th
|
|
|
69
87
|
|
|
70
88
|
```javascript
|
|
71
89
|
import { Application, Controller } from "@hotwired/stimulus"
|
|
72
|
-
import LiveController from "
|
|
73
|
-
import LiveCableBlessing from "
|
|
74
|
-
import "live_cable" // Automatically starts DOM observer
|
|
90
|
+
import LiveController from "@isometriks/live_cable/controller"
|
|
91
|
+
import LiveCableBlessing from "@isometriks/live_cable/blessing"
|
|
92
|
+
import "@isometriks/live_cable" // Automatically starts DOM observer
|
|
75
93
|
|
|
76
94
|
// Enable the blessing for all controllers
|
|
77
95
|
Controller.blessings = [
|
|
@@ -982,6 +1000,53 @@ The `live-key` attribute acts as a hint for the diffing algorithm to identify el
|
|
|
982
1000
|
<% end %>
|
|
983
1001
|
```
|
|
984
1002
|
|
|
1003
|
+
## Loading States
|
|
1004
|
+
|
|
1005
|
+
Every interaction is a round trip to the server. While a message is in flight, LiveCable adds a `live-loading` attribute to the component's root element and to the element that triggered the message, so you can show pending feedback with plain CSS:
|
|
1006
|
+
|
|
1007
|
+
```css
|
|
1008
|
+
button[live-loading] { opacity: 0.5; cursor: wait; }
|
|
1009
|
+
|
|
1010
|
+
.spinner { display: none; }
|
|
1011
|
+
[live-loading] .spinner { display: inline-block; }
|
|
1012
|
+
```
|
|
1013
|
+
|
|
1014
|
+
To prevent double-clicks, mark buttons with `live-disable-with`. The element is disabled while the message is in flight and restored when the server responds. Give the attribute a value to also swap the label:
|
|
1015
|
+
|
|
1016
|
+
```erb
|
|
1017
|
+
<button live-action="checkout" live-disable-with="Processing...">Checkout</button>
|
|
1018
|
+
|
|
1019
|
+
<form live-form="save">
|
|
1020
|
+
<input type="text" name="title">
|
|
1021
|
+
<button type="submit" live-disable-with="Saving...">Save</button>
|
|
1022
|
+
</form>
|
|
1023
|
+
```
|
|
1024
|
+
|
|
1025
|
+
For forms, put `live-disable-with` on the submit button(s); form values are serialized before anything is disabled. Reactive inputs (`live-reactive`) get the `live-loading` attribute but are never disabled, so typing is not interrupted.
|
|
1026
|
+
|
|
1027
|
+
The loading state is cleared when the server responds — with a re-render, an error, or a lightweight acknowledgement when the action didn't change any state — so it never gets stuck.
|
|
1028
|
+
|
|
1029
|
+
## Server Events
|
|
1030
|
+
|
|
1031
|
+
Components can trigger DOM events on the client with `dispatch_event` — for scroll-to-bottom, closing modals, toasts, or anything else the client should do after the server finishes:
|
|
1032
|
+
|
|
1033
|
+
```ruby
|
|
1034
|
+
def send_message(params)
|
|
1035
|
+
messages << { text: params[:text] }
|
|
1036
|
+
dispatch_event('chat:message-sent')
|
|
1037
|
+
end
|
|
1038
|
+
```
|
|
1039
|
+
|
|
1040
|
+
Events fire as bubbling `CustomEvent`s from the component's root element **after the DOM has been morphed**, so handlers see the updated markup. Plain Stimulus `data-action` syntax handles them — no LiveCable-specific JavaScript:
|
|
1041
|
+
|
|
1042
|
+
```erb
|
|
1043
|
+
<div data-controller="chat" data-action="chat:message-sent->chat#scrollToBottom">
|
|
1044
|
+
...
|
|
1045
|
+
</div>
|
|
1046
|
+
```
|
|
1047
|
+
|
|
1048
|
+
Pass a payload (`dispatch_event('toast:show', message: 'Saved')`, available as `event.detail`) or target global listeners with `dispatch_event('analytics:tracked', window: true)`. Events work from actions, lifecycle callbacks, and `stream_from` callbacks, and are delivered exactly once, in order, whether or not the action re-rendered.
|
|
1049
|
+
|
|
985
1050
|
## Compound Components
|
|
986
1051
|
|
|
987
1052
|
By default, components render the partial at `app/views/live/component_name.html.live.erb`. You can organize your templates differently by marking a component as `compound`.
|
|
@@ -1219,6 +1284,27 @@ When a broadcast is received:
|
|
|
1219
1284
|
- **Live dashboards**: Update metrics and charts in real-time
|
|
1220
1285
|
- **Presence tracking**: Show who's currently online or viewing a resource
|
|
1221
1286
|
|
|
1287
|
+
## Testing Components
|
|
1288
|
+
|
|
1289
|
+
LiveCable includes a test harness for fast, browser-free component tests. Actions go through the real message pipeline (whitelisting, parameter parsing, change tracking, re-rendering), so tests exercise what production runs:
|
|
1290
|
+
|
|
1291
|
+
```ruby
|
|
1292
|
+
RSpec.describe Live::Counter do
|
|
1293
|
+
include LiveCable::Testing
|
|
1294
|
+
|
|
1295
|
+
it 'increments by the step size' do
|
|
1296
|
+
counter = live_mount('counter', step: 2)
|
|
1297
|
+
|
|
1298
|
+
counter.perform(:increment)
|
|
1299
|
+
|
|
1300
|
+
expect(counter.count).to eq(2)
|
|
1301
|
+
expect(counter.rendered).to have_css('[data-testid="counter-value"]', text: '2')
|
|
1302
|
+
end
|
|
1303
|
+
end
|
|
1304
|
+
```
|
|
1305
|
+
|
|
1306
|
+
The harness supports client reactive updates (`set_reactive`), broadcast assertions (`broadcasts(:_refresh)`), simulated stream broadcasts (`receive_stream`), shared state across components mounted on the same connection, connection identifiers like `current_user`, and lifecycle testing via `unmount`. See the [testing guide](https://livecable.io/guide/testing) for the full API.
|
|
1307
|
+
|
|
1222
1308
|
## Error Handling
|
|
1223
1309
|
|
|
1224
1310
|
When an unhandled exception is raised inside a component action, LiveCable replaces the component in the DOM with an error message and cleans up the server-side component.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Controller } from "@hotwired/stimulus"
|
|
2
|
-
import SubscriptionManager from "
|
|
2
|
+
import SubscriptionManager from "@isometriks/live_cable/subscriptions"
|
|
3
|
+
import LoadingState from "@isometriks/live_cable/loading"
|
|
3
4
|
|
|
4
5
|
export default class extends Controller {
|
|
5
6
|
static values = {
|
|
@@ -12,16 +13,18 @@ export default class extends Controller {
|
|
|
12
13
|
|
|
13
14
|
#subscription
|
|
14
15
|
#debounces = new Map()
|
|
16
|
+
#loading
|
|
15
17
|
|
|
16
18
|
#callActionCallback = (event) => {
|
|
17
19
|
event.stopPropagation()
|
|
18
20
|
|
|
19
21
|
const { action, params } = event.detail
|
|
20
22
|
|
|
21
|
-
this.sendCall(action, params)
|
|
23
|
+
this.sendCall(action, params, event.target)
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
connect() {
|
|
27
|
+
this.#loading = new LoadingState(this.element)
|
|
25
28
|
this.element.addEventListener("call", this.#callActionCallback)
|
|
26
29
|
|
|
27
30
|
this.#subscription = SubscriptionManager.subscribe(
|
|
@@ -33,8 +36,8 @@ export default class extends Controller {
|
|
|
33
36
|
|
|
34
37
|
// Create callbacks for each action or form
|
|
35
38
|
this.actionsValue.forEach((action) => {
|
|
36
|
-
this[`action_$${action}`] = ({ params }) => {
|
|
37
|
-
this.sendCall(action, this.#convertKeysToSnakeCase(params))
|
|
39
|
+
this[`action_$${action}`] = ({ params, currentTarget }) => {
|
|
40
|
+
this.sendCall(action, this.#convertKeysToSnakeCase(params), currentTarget)
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
this[`form_$${action}`] = (event) => {
|
|
@@ -47,12 +50,30 @@ export default class extends Controller {
|
|
|
47
50
|
this.element.removeEventListener("call", this.#callActionCallback)
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
sendCall(action, params = {}) {
|
|
53
|
+
sendCall(action, params = {}, trigger = null) {
|
|
54
|
+
this.#loading.start(trigger)
|
|
51
55
|
this.#subscription.send(
|
|
52
56
|
this.#flushDebounced(this.#callMessage(params, action))
|
|
53
57
|
)
|
|
54
58
|
}
|
|
55
59
|
|
|
60
|
+
// Called by the subscription when the server answers a message
|
|
61
|
+
// (refresh, ack, or error). Restores any live-loading / live-disable-with
|
|
62
|
+
// state once all in-flight messages have been answered.
|
|
63
|
+
finishLoading() {
|
|
64
|
+
this.#loading?.finish()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Whether any message is still awaiting a server response
|
|
68
|
+
get isLoading() {
|
|
69
|
+
return this.#loading?.active ?? false
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Called by the subscription when the component is being torn down.
|
|
73
|
+
resetLoading() {
|
|
74
|
+
this.#loading?.reset()
|
|
75
|
+
}
|
|
76
|
+
|
|
56
77
|
#callMessage(params, action) {
|
|
57
78
|
return {
|
|
58
79
|
_action: action,
|
|
@@ -83,6 +104,8 @@ export default class extends Controller {
|
|
|
83
104
|
|
|
84
105
|
sendReactive(target) {
|
|
85
106
|
this.#clearDebounce(target)
|
|
107
|
+
// Never disable reactive inputs while in flight - it would drop focus
|
|
108
|
+
this.#loading.start(target, { disable: false })
|
|
86
109
|
this.#subscription.send(
|
|
87
110
|
this.#flushDebounced(this.#reactiveMessage(target))
|
|
88
111
|
)
|
|
@@ -114,9 +137,13 @@ export default class extends Controller {
|
|
|
114
137
|
sendForm(action, formEl) {
|
|
115
138
|
this.#clearDebounce(formEl)
|
|
116
139
|
|
|
140
|
+
// Serialize before starting the loading state - disabled controls
|
|
141
|
+
// (live-disable-with) are excluded from FormData
|
|
117
142
|
const formData = new FormData(formEl)
|
|
118
143
|
const params = new URLSearchParams(formData).toString()
|
|
119
144
|
|
|
145
|
+
this.#loading.start(formEl)
|
|
146
|
+
|
|
120
147
|
this.#subscription.send(
|
|
121
148
|
this.#flushDebounced(this.#callMessage(params, action))
|
|
122
149
|
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import LiveObserver from '
|
|
2
|
-
import SubscriptionManager from '
|
|
3
|
-
import DOM from '
|
|
1
|
+
import LiveObserver from '@isometriks/live_cable/observer'
|
|
2
|
+
import SubscriptionManager from '@isometriks/live_cable/subscriptions'
|
|
3
|
+
import DOM from '@isometriks/live_cable/dom'
|
|
4
4
|
|
|
5
5
|
const observer = new LiveObserver()
|
|
6
6
|
observer.start()
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loading state tracking for LiveCable components.
|
|
3
|
+
*
|
|
4
|
+
* Tracks in-flight messages for a single component and reflects them in the
|
|
5
|
+
* DOM so users can style pending states with CSS:
|
|
6
|
+
*
|
|
7
|
+
* - The component root element gets a `live-loading` attribute while any
|
|
8
|
+
* message is awaiting a server response.
|
|
9
|
+
* - The element that triggered the message (button, form, input) also gets
|
|
10
|
+
* a `live-loading` attribute.
|
|
11
|
+
* - Elements with a `live-disable-with` attribute are disabled while the
|
|
12
|
+
* message is in flight. If the attribute has a value, the element's label
|
|
13
|
+
* (textContent, or value for inputs) is swapped for it.
|
|
14
|
+
*
|
|
15
|
+
* The state is cleared when the server responds with a refresh, an error,
|
|
16
|
+
* or an ack (sent when an action didn't change any reactive variables).
|
|
17
|
+
* Multiple in-flight messages are counted; the DOM is only restored once
|
|
18
|
+
* all of them have been answered.
|
|
19
|
+
*/
|
|
20
|
+
export default class LoadingState {
|
|
21
|
+
/** @type {HTMLElement} */
|
|
22
|
+
#root
|
|
23
|
+
/** @type {number} */
|
|
24
|
+
#inFlight = 0
|
|
25
|
+
/** @type {Set<Element>} - Elements marked with the live-loading attribute */
|
|
26
|
+
#markedElements = new Set()
|
|
27
|
+
/** @type {Map<Element, Object>} - Original state of disabled elements */
|
|
28
|
+
#disabledElements = new Map()
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {HTMLElement} root - The component's root element
|
|
32
|
+
*/
|
|
33
|
+
constructor(root) {
|
|
34
|
+
this.#root = root
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Whether any message is currently awaiting a server response.
|
|
39
|
+
* @returns {boolean}
|
|
40
|
+
*/
|
|
41
|
+
get active() {
|
|
42
|
+
return this.#inFlight > 0
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Mark the component as loading.
|
|
47
|
+
* Called right before a message is sent to the server.
|
|
48
|
+
*
|
|
49
|
+
* @param {Element|null} trigger - The element that triggered the message
|
|
50
|
+
* @param {Object} options
|
|
51
|
+
* @param {boolean} options.disable - Whether to process live-disable-with
|
|
52
|
+
* elements. Disabled for reactive inputs so typing doesn't lose focus.
|
|
53
|
+
*/
|
|
54
|
+
start(trigger = null, { disable = true } = {}) {
|
|
55
|
+
this.#inFlight++
|
|
56
|
+
|
|
57
|
+
this.#mark(this.#root)
|
|
58
|
+
|
|
59
|
+
if (trigger instanceof Element && trigger !== this.#root) {
|
|
60
|
+
this.#mark(trigger)
|
|
61
|
+
|
|
62
|
+
if (disable) {
|
|
63
|
+
this.#disableElements(trigger)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Record a server response for this component.
|
|
70
|
+
* Restores the DOM once all in-flight messages have been answered.
|
|
71
|
+
*
|
|
72
|
+
* @returns {boolean} true if the loading state was fully cleared
|
|
73
|
+
*/
|
|
74
|
+
finish() {
|
|
75
|
+
if (this.#inFlight === 0) {
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
this.#inFlight--
|
|
80
|
+
|
|
81
|
+
if (this.#inFlight > 0) {
|
|
82
|
+
return false
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.#restore()
|
|
86
|
+
return true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Clear all loading state immediately, regardless of in-flight count.
|
|
91
|
+
* Used when the component is being torn down (e.g. on error).
|
|
92
|
+
*/
|
|
93
|
+
reset() {
|
|
94
|
+
this.#inFlight = 0
|
|
95
|
+
this.#restore()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
#mark(element) {
|
|
99
|
+
element.setAttribute('live-loading', '')
|
|
100
|
+
this.#markedElements.add(element)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Disable the trigger and/or its descendants marked with live-disable-with.
|
|
105
|
+
* For a button the attribute lives on the button itself; for a form it
|
|
106
|
+
* usually lives on the submit button(s) inside it.
|
|
107
|
+
*/
|
|
108
|
+
#disableElements(trigger) {
|
|
109
|
+
const elements = []
|
|
110
|
+
|
|
111
|
+
if (trigger.hasAttribute('live-disable-with')) {
|
|
112
|
+
elements.push(trigger)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
elements.push(...trigger.querySelectorAll('[live-disable-with]'))
|
|
116
|
+
|
|
117
|
+
elements.forEach(element => {
|
|
118
|
+
// Already disabled by an earlier in-flight message
|
|
119
|
+
if (this.#disabledElements.has(element)) {
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const text = element.getAttribute('live-disable-with')
|
|
124
|
+
const isInput = element instanceof HTMLInputElement
|
|
125
|
+
|
|
126
|
+
this.#disabledElements.set(element, {
|
|
127
|
+
disabled: element.disabled,
|
|
128
|
+
content: text ? (isInput ? element.value : element.textContent) : null,
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
element.disabled = true
|
|
132
|
+
|
|
133
|
+
if (text) {
|
|
134
|
+
if (isInput) {
|
|
135
|
+
element.value = text
|
|
136
|
+
} else {
|
|
137
|
+
element.textContent = text
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
#restore() {
|
|
144
|
+
this.#markedElements.forEach(element => {
|
|
145
|
+
element.removeAttribute('live-loading')
|
|
146
|
+
})
|
|
147
|
+
this.#markedElements.clear()
|
|
148
|
+
|
|
149
|
+
this.#disabledElements.forEach(({ disabled, content }, element) => {
|
|
150
|
+
element.disabled = disabled
|
|
151
|
+
|
|
152
|
+
if (content !== null) {
|
|
153
|
+
if (element instanceof HTMLInputElement) {
|
|
154
|
+
element.value = content
|
|
155
|
+
} else {
|
|
156
|
+
element.textContent = content
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
this.#disabledElements.clear()
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
|
|
21
21
|
import { createConsumer } from "@rails/actioncable"
|
|
22
22
|
import morphdom from "morphdom"
|
|
23
|
-
import DOM from "
|
|
23
|
+
import DOM from "@isometriks/live_cable/dom"
|
|
24
24
|
|
|
25
25
|
const consumer = createConsumer()
|
|
26
26
|
|
|
@@ -396,6 +396,10 @@ class Subscription {
|
|
|
396
396
|
* @param {string} [data._status] - Status update (e.g., 'subscribed', 'destroy')
|
|
397
397
|
* @param {string} [data._refresh] - HTML to morph into the DOM
|
|
398
398
|
* @param {string} [data._error] - Raw error HTML to replace the component with
|
|
399
|
+
* @param {boolean} [data._ack] - Acknowledgement that a message was processed
|
|
400
|
+
* without producing a re-render; clears the loading state
|
|
401
|
+
* @param {Array} [data._events] - Events to dispatch as CustomEvents; when
|
|
402
|
+
* attached to a _refresh they fire after the DOM has been morphed
|
|
399
403
|
* @private
|
|
400
404
|
*/
|
|
401
405
|
#received = (data) => {
|
|
@@ -405,7 +409,32 @@ class Subscription {
|
|
|
405
409
|
this.#handleRefresh(data['_refresh'])
|
|
406
410
|
} else if (data['_error']) {
|
|
407
411
|
this.#handleError(data['_error'])
|
|
412
|
+
} else if (data['_ack']) {
|
|
413
|
+
this.#controller?.finishLoading()
|
|
408
414
|
}
|
|
415
|
+
|
|
416
|
+
// Dispatch after the branch above so events attached to a refresh fire
|
|
417
|
+
// once the morph has completed and handlers see the updated DOM
|
|
418
|
+
if (data['_events']) {
|
|
419
|
+
this.#dispatchEvents(data['_events'])
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Fire server-dispatched events as bubbling CustomEvents from the
|
|
425
|
+
* component's root element, or from window when the event asks for it.
|
|
426
|
+
*
|
|
427
|
+
* @param {Array<{name: string, detail: Object, window: boolean}>} events
|
|
428
|
+
* @private
|
|
429
|
+
*/
|
|
430
|
+
#dispatchEvents(events) {
|
|
431
|
+
events.forEach(({ name, detail, window: onWindow }) => {
|
|
432
|
+
const target = onWindow ? window : this.#controller?.element
|
|
433
|
+
|
|
434
|
+
if (target) {
|
|
435
|
+
target.dispatchEvent(new CustomEvent(name, { detail, bubbles: true }))
|
|
436
|
+
}
|
|
437
|
+
})
|
|
409
438
|
}
|
|
410
439
|
|
|
411
440
|
/**
|
|
@@ -420,6 +449,7 @@ class Subscription {
|
|
|
420
449
|
return
|
|
421
450
|
}
|
|
422
451
|
|
|
452
|
+
this.#controller.resetLoading()
|
|
423
453
|
this.#controller.element.outerHTML = html
|
|
424
454
|
this.unsubscribe()
|
|
425
455
|
}
|
|
@@ -458,10 +488,40 @@ class Subscription {
|
|
|
458
488
|
return
|
|
459
489
|
}
|
|
460
490
|
|
|
461
|
-
|
|
491
|
+
// Restore live-loading / live-disable-with state before morphing so the
|
|
492
|
+
// morph applies the server-rendered truth on top of the original DOM.
|
|
493
|
+
// With multiple messages in flight this only restores once the last
|
|
494
|
+
// response arrives - until then the morph below preserves the pending
|
|
495
|
+
// elements so live-disable-with buttons can't be clicked early.
|
|
496
|
+
this.#controller.finishLoading()
|
|
497
|
+
|
|
498
|
+
const rootElement = this.#controller.element
|
|
499
|
+
const stillLoading = this.#controller.isLoading
|
|
500
|
+
|
|
501
|
+
const refreshDOM = this.#buildRefreshDOM(refresh)
|
|
502
|
+
|
|
503
|
+
if (stillLoading) {
|
|
504
|
+
refreshDOM.setAttribute('live-loading', '')
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
morphdom(rootElement, refreshDOM, {
|
|
462
508
|
// Preserve elements marked with live-ignore attribute
|
|
463
509
|
onBeforeElUpdated(fromEl, toEl) {
|
|
464
|
-
|
|
510
|
+
if (!fromEl.hasAttribute) {
|
|
511
|
+
return true
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (fromEl.hasAttribute('live-ignore')) {
|
|
515
|
+
return false
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// Keep elements that are still awaiting a server response untouched
|
|
519
|
+
// (the root is handled above so the rest of the tree still morphs)
|
|
520
|
+
if (stillLoading && fromEl !== rootElement && fromEl.hasAttribute('live-loading')) {
|
|
521
|
+
return false
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
return true
|
|
465
525
|
},
|
|
466
526
|
// Use stable keys for better morphing performance and state preservation
|
|
467
527
|
getNodeKey(node) {
|
data/config/importmap.rb
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
pin '@rails/actioncable', to: 'actioncable.esm.js'
|
|
4
4
|
pin 'morphdom', to: 'https://ga.jspm.io/npm:morphdom@2.7.8/dist/morphdom-esm.js'
|
|
5
5
|
|
|
6
|
-
pin '
|
|
7
|
-
pin '
|
|
8
|
-
pin '
|
|
9
|
-
pin '
|
|
10
|
-
pin '
|
|
11
|
-
pin 'live_cable', to: '
|
|
6
|
+
pin '@isometriks/live_cable/controller', to: 'controllers/live_controller.js'
|
|
7
|
+
pin '@isometriks/live_cable/blessing', to: 'live_cable_blessing.js'
|
|
8
|
+
pin '@isometriks/live_cable/subscriptions', to: 'subscriptions.js'
|
|
9
|
+
pin '@isometriks/live_cable/loading', to: 'loading.js'
|
|
10
|
+
pin '@isometriks/live_cable/observer', to: 'observer.js'
|
|
11
|
+
pin '@isometriks/live_cable/dom', to: 'dom.js'
|
|
12
|
+
pin '@isometriks/live_cable', to: 'live_cable.js'
|
data/lib/live.rb
ADDED
|
@@ -13,6 +13,12 @@ module LiveCable
|
|
|
13
13
|
broadcast({ _status: 'subscribed', id: live_id })
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
# Sent when a received message didn't change any reactive variables,
|
|
17
|
+
# so the client can clear its loading state without a re-render.
|
|
18
|
+
def broadcast_ack
|
|
19
|
+
broadcast({ _ack: true })
|
|
20
|
+
end
|
|
21
|
+
|
|
16
22
|
def broadcast_destroy
|
|
17
23
|
broadcast({ _status: 'destroy' })
|
|
18
24
|
@subscribed = false
|
|
@@ -20,9 +26,14 @@ module LiveCable
|
|
|
20
26
|
|
|
21
27
|
def broadcast_render
|
|
22
28
|
run_callbacks :render do
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
data = { _refresh: render.as_json }
|
|
30
|
+
|
|
31
|
+
# Events ride along with the render so the client can fire them
|
|
32
|
+
# after the DOM has been morphed
|
|
33
|
+
events = flush_events
|
|
34
|
+
data[:_events] = events if events.any?
|
|
35
|
+
|
|
36
|
+
broadcast(data)
|
|
26
37
|
end
|
|
27
38
|
end
|
|
28
39
|
end
|