phlex-reactive 0.17.0 → 0.19.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 +62 -1
- data/README.md +77 -4
- data/app/controllers/phlex/reactive/actions_controller.rb +34 -20
- data/app/javascript/phlex/reactive/features/subscribe.js +48 -3
- data/app/javascript/phlex/reactive/features/subscribe.min.js +2 -2
- data/app/javascript/phlex/reactive/features/subscribe.min.js.map +3 -3
- data/app/javascript/phlex/reactive/reactive_controller.min.js +4 -4
- data/app/javascript/phlex/reactive/reactive_controller.min.js.map +3 -3
- data/lib/generators/phlex/reactive/install/templates/phlex_reactive.rb.erb +3 -1
- data/lib/phlex/reactive/authorization.rb +4 -1
- data/lib/phlex/reactive/component/dsl.rb +15 -2
- data/lib/phlex/reactive/component.rb +14 -1
- data/lib/phlex/reactive/inspector/report.rb +10 -2
- data/lib/phlex/reactive/inspector.rb +8 -2
- data/lib/phlex/reactive/mcp/tools/actions_tool.rb +3 -1
- data/lib/phlex/reactive/mcp/tools/find_tool.rb +4 -2
- data/lib/phlex/reactive/reply.rb +4 -3
- data/lib/phlex/reactive/settles.rb +7 -4
- data/lib/phlex/reactive/subscribe.rb +30 -5
- data/lib/phlex/reactive/test_helpers.rb +21 -7
- data/lib/phlex/reactive/version.rb +1 -1
- data/lib/phlex/reactive.rb +4 -2
- data/sig/phlex/reactive/component.rbs +4 -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: 60975530dead6bb72c6e179a2407c522c6e859c15a5afc597a267a59faf45178
|
|
4
|
+
data.tar.gz: d71e103dff040f6387ca71e7aef2089f935ccb5be608267c168042a4f936fcee
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f15ecfa8394087f4bea3454de5d3977f6e04375869f3615c6e9eaa05a86da24be043a38414d37b463ca7af9511c4d1d90334210ce0044e0c54200893fc52e282
|
|
7
|
+
data.tar.gz: d03468cd274a132459eff64683943f403582da921dfc38d1f45f9cc9b22f905a3842ec65ce0c5b1e62e0777d0b7bd079ca033fe8650ff86219089172e81290e3
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,65 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.19.0] - 2026-11-28
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- **`action :name, transaction: false` runs one action without the endpoint's
|
|
14
|
+
transaction (#481).** For an action that waits on something slow that is not
|
|
15
|
+
the database (a remote HTTP API), which until now held a transaction and its
|
|
16
|
+
connection open for the whole wait. `transaction:` defaults to `true`, takes
|
|
17
|
+
only `true` or `false` (anything else raises `ArgumentError` at class load) and
|
|
18
|
+
is inherited with the action; every other action keeps the transaction. Only
|
|
19
|
+
the endpoint's own wrapper is skipped: an overridden `transaction_wrapper` is
|
|
20
|
+
not called for the action, and a transaction opened further out is untouched.
|
|
21
|
+
`verify_authorized` still raises after an action that never authorized, but
|
|
22
|
+
the endpoint has nothing to roll back, so its writes stay (unless a
|
|
23
|
+
transaction opened outside the endpoint rolls them back): authorize before any
|
|
24
|
+
side effect. `around_action`, `reply.defer` and `reply.pending` behave as before;
|
|
25
|
+
`run_reactive` honours the flag; the action inventory (`phlex_reactive:actions`,
|
|
26
|
+
`find`, the MCP tools) shows it as `(no endpoint transaction)` /
|
|
27
|
+
`transaction: false`.
|
|
28
|
+
`Component::ActionDefinition` gains a `transaction` member that defaults to
|
|
29
|
+
`true`, so existing `ActionDefinition.new(name:, params:, schema:)` callers
|
|
30
|
+
keep working.
|
|
31
|
+
|
|
32
|
+
### Fixed
|
|
33
|
+
|
|
34
|
+
- **`run_reactive` scopes an authorization mark to the action (#481).** A
|
|
35
|
+
`mark_authorized!` (or an intercepted authorization call) inside an action
|
|
36
|
+
driven by the test helper stayed set on the thread, so a later example could
|
|
37
|
+
see a stale mark. The driver now opens the same tracking window the endpoint
|
|
38
|
+
does; it still does not run `verify_authorized` itself.
|
|
39
|
+
|
|
40
|
+
## [0.18.0] - 2026-11-16
|
|
41
|
+
|
|
42
|
+
### Added
|
|
43
|
+
|
|
44
|
+
- **`reactive_subscribe` catches up on a broadcast sent before its source
|
|
45
|
+
connected (#478, pgbus).** On a durable stream the root carries the stream's
|
|
46
|
+
watermark at render (`data-reactive-subscribe-since`, read as
|
|
47
|
+
`pgbus_stream_from` reads it: one `MAX(msg_id)` query per render of a durable
|
|
48
|
+
subscribing root), and the minted `<pgbus-stream-source>` opens with it as
|
|
49
|
+
`since-id`. A broadcast sent between the render and the connection now
|
|
50
|
+
arrives. Roots sharing a source take the lowest watermark, and a source minted
|
|
51
|
+
again after a page render opens from its roots' watermarks then: it never
|
|
52
|
+
skips a frame, but it may replay one a root already shows, so a `js:` or
|
|
53
|
+
append frame can apply twice. A `data-turbo-permanent` subscribing root hits
|
|
54
|
+
this on every Drive visit (its source is minted again each time, from the
|
|
55
|
+
root's first render). Each time a
|
|
56
|
+
root's source connects, the root gets `reactive:subscribed`
|
|
57
|
+
(`detail: { connectionId, reconnect }`, not bubbling), so
|
|
58
|
+
`on(:refresh, event: "reactive:subscribed")` refetches what an ephemeral
|
|
59
|
+
stream cannot replay.
|
|
60
|
+
|
|
61
|
+
### Fixed
|
|
62
|
+
|
|
63
|
+
- **A `reactive_subscribe` stream durable by configuration no longer replays
|
|
64
|
+
frames older than the render (#478).** The source opened with no `since`, which pgbus reads as
|
|
65
|
+
0, so it replayed every retained frame: a `js:` broadcast sent before the
|
|
66
|
+
page rendered ran again on load.
|
|
67
|
+
|
|
9
68
|
## [0.17.0] - 2026-11-16
|
|
10
69
|
|
|
11
70
|
### Added
|
|
@@ -4630,7 +4689,9 @@ room for dogfood fixes.
|
|
|
4630
4689
|
scaffolds a reactive component (and an RSpec spec when the app uses RSpec),
|
|
4631
4690
|
state-backed by default or record-backed with `--record`.
|
|
4632
4691
|
|
|
4633
|
-
[Unreleased]: https://github.com/mhenrixon/phlex-reactive/compare/v0.
|
|
4692
|
+
[Unreleased]: https://github.com/mhenrixon/phlex-reactive/compare/v0.19.0...HEAD
|
|
4693
|
+
[0.19.0]: https://github.com/mhenrixon/phlex-reactive/compare/v0.18.0...v0.19.0
|
|
4694
|
+
[0.18.0]: https://github.com/mhenrixon/phlex-reactive/compare/v0.17.0...v0.18.0
|
|
4634
4695
|
[0.17.0]: https://github.com/mhenrixon/phlex-reactive/compare/v0.16.14...v0.17.0
|
|
4635
4696
|
[0.16.14]: https://github.com/mhenrixon/phlex-reactive/compare/v0.16.13...v0.16.14
|
|
4636
4697
|
[0.16.13]: https://github.com/mhenrixon/phlex-reactive/compare/v0.16.12...v0.16.13
|
data/README.md
CHANGED
|
@@ -670,6 +670,7 @@ Use in controllers: `render turbo_stream: Counter.replace(counter)`.
|
|
|
670
670
|
| `reactive_record :name` | Record-backed identity (GlobalID). State = the DB. Also defaults `#id` to `dom_id(record)`. |
|
|
671
671
|
| `reactive_state :a, :b` | Signed instance-var identity. Standalone, or combined with `reactive_record` to sign transient UI state alongside the row. |
|
|
672
672
|
| `action :name, params: { x: :integer }` | Declare a client-invokable action + its param schema. **Default-deny.** |
|
|
673
|
+
| `action :name, transaction: false` | Run this one action **without the endpoint's transaction** (default `true`), for an action that waits on something slow that is not the database (a remote API). The endpoint rolls nothing back: `verify_authorized` still raises after it, but writes it already made stay (unless a transaction opened outside the endpoint rolls them back), so authorize before any side effect. See [Running without the endpoint transaction](#running-without-the-endpoint-transaction-transaction-false). |
|
|
673
674
|
| `mark_authorized!` | Inside an action: satisfy the `verify_authorized` guard after a bespoke check the interceptor can't see (a hand-rolled policy). Call it only after your check passes. |
|
|
674
675
|
| `skip_verify_authorized [ :a, :b ]` | Opt a component (bare) or specific actions out of the default-ON `verify_authorized` guard — for a genuinely public component (a counter, a client-only filter). |
|
|
675
676
|
| `reactive_root(**overrides)` | Spread onto the root element: emits the component `id` **and** `reactive_attrs` together, so the controller root always carries `#id`. Preferred over `id:` + `reactive_attrs`. `**overrides` (`class:`/`data:`) deep-merge. `compute: :name` binds a client-side compute **at the root** — descriptors plus the `input->reactive#recompute` delegation, so no field needs its own wiring; `nil` collapses to no binding. See [Client-side computes](#client-side-computes-reactive_compute--reactive_text). |
|
|
@@ -2410,7 +2411,33 @@ Imports::Status.broadcast_self(import, js: Phlex::Reactive::JS.new.add_class("#b
|
|
|
2410
2411
|
- **The actor.** An action on a subscribing root sends that root's own
|
|
2411
2412
|
connection id, so `exclude: reactive_connection_id` skips the actor on that
|
|
2412
2413
|
stream, but not on another page-level stream.
|
|
2413
|
-
- **
|
|
2414
|
+
- **Catching up (#478).** On a durable stream (pgbus's
|
|
2415
|
+
`streams_durable_patterns`) the root also carries the stream's watermark at
|
|
2416
|
+
render (`data-reactive-subscribe-since`, read as `pgbus_stream_from` reads it,
|
|
2417
|
+
one query per render), and the source replays from it: a broadcast sent
|
|
2418
|
+
between the render and the connection still arrives, and nothing older does.
|
|
2419
|
+
A source minted again after a page render (a Drive render, a morph that
|
|
2420
|
+
dropped it, every Drive visit of a `data-turbo-permanent` root) opens from
|
|
2421
|
+
its roots' watermarks then: it never skips a frame, but it may replay one a
|
|
2422
|
+
root already shows, so a `js:` or append frame can apply twice.
|
|
2423
|
+
An ephemeral stream (pgbus's default) has nothing to replay. Each time a
|
|
2424
|
+
root's source connects, the root gets `reactive:subscribed`
|
|
2425
|
+
(`detail: { connectionId, reconnect }`, not bubbling), so a component that
|
|
2426
|
+
must not miss a change refetches on it:
|
|
2427
|
+
|
|
2428
|
+
```ruby
|
|
2429
|
+
action :refresh
|
|
2430
|
+
def refresh
|
|
2431
|
+
authorize! @import, :show? # verify_authorized is on by default
|
|
2432
|
+
reply.morph
|
|
2433
|
+
end
|
|
2434
|
+
|
|
2435
|
+
def view_template
|
|
2436
|
+
div(**mix(reactive_root, on(:refresh, event: "reactive:subscribed"))) { @import.state }
|
|
2437
|
+
end
|
|
2438
|
+
```
|
|
2439
|
+
|
|
2440
|
+
A root that joins a source already connected gets no event.
|
|
2414
2441
|
- **Refused at render**: a stream name over pgbus's stream-name budget (a
|
|
2415
2442
|
UUID-keyed record always is: use `key: -> { [Pgbus.stream_key(@record), :status] }`),
|
|
2416
2443
|
a nil record or key part, and any subscribing root on Action Cable, where you
|
|
@@ -3236,6 +3263,49 @@ interpolated from the row's live field values (`confirm: "Delete '%{name}'?"`
|
|
|
3236
3263
|
`Delete 'Widget'?`) — see [Draft rows for a new
|
|
3237
3264
|
parent](#draft-rows-for-a-new-parent-reactive_nested_).
|
|
3238
3265
|
|
|
3266
|
+
### Running without the endpoint transaction (`transaction: false`)
|
|
3267
|
+
|
|
3268
|
+
The endpoint runs every action inside a database transaction, so a broadcast
|
|
3269
|
+
or enqueue deferred to commit never fires for a rolled-back change. An action
|
|
3270
|
+
that waits on something slow that is not the database, such as a call to a
|
|
3271
|
+
remote HTTP API, would hold that transaction (and its database connection)
|
|
3272
|
+
open for the whole wait. Declare that one action `transaction: false`:
|
|
3273
|
+
|
|
3274
|
+
```ruby
|
|
3275
|
+
action :refresh, transaction: false
|
|
3276
|
+
action :rename, params: { title: :string } # unchanged: in a transaction
|
|
3277
|
+
action :sync, params: { force: :boolean }, transaction: false
|
|
3278
|
+
|
|
3279
|
+
def refresh
|
|
3280
|
+
authorize! @widget, :update? # before any side effect
|
|
3281
|
+
data = RemoteService.fetch(@widget) # no transaction open while it waits
|
|
3282
|
+
@widget.update!(synced_at: Time.current, data:)
|
|
3283
|
+
end
|
|
3284
|
+
```
|
|
3285
|
+
|
|
3286
|
+
- `transaction:` defaults to `true`, takes only `true` or `false` (anything else
|
|
3287
|
+
raises `ArgumentError` at class load) and is inherited with the action. Every
|
|
3288
|
+
other action, on the same component and on the same page, keeps the transaction.
|
|
3289
|
+
- Only the endpoint's own wrapper is skipped (an overridden `transaction_wrapper`
|
|
3290
|
+
is not called for the action). A transaction opened further out, by a
|
|
3291
|
+
transactional test or an `around_action` wrapper, is not affected, and the
|
|
3292
|
+
action may open its own, shorter transactions around its writes.
|
|
3293
|
+
- **The endpoint rolls nothing back.** `verify_authorized` still raises
|
|
3294
|
+
`AuthorizationNotVerified` after an action that never authorized, and the
|
|
3295
|
+
request still fails with no reply, but the writes it already made stay
|
|
3296
|
+
(unless a transaction opened outside the endpoint rolls them back). An
|
|
3297
|
+
error the action raises likewise rolls back only what its own transactions
|
|
3298
|
+
cover. **Authorize before any side effect.**
|
|
3299
|
+
- Commit-deferred work (a `durable:` broadcast, an enqueue under
|
|
3300
|
+
`enqueue_after_transaction_commit`) fires at the action's own commit, or at
|
|
3301
|
+
once when it has none open. "Never fires for a rolled-back change" is then the
|
|
3302
|
+
action's job: wrap the write and its broadcast or enqueue in one transaction.
|
|
3303
|
+
- `Phlex::Reactive.around_action` wrappers, `reply.defer` and `reply.pending`
|
|
3304
|
+
behave as for any action; `run_reactive` skips the transaction for the same
|
|
3305
|
+
actions the endpoint does. The text inventory (`phlex_reactive:actions`) marks
|
|
3306
|
+
them `(no endpoint transaction)`, and `FORMAT=json` and the MCP tools report
|
|
3307
|
+
`transaction: false` for them.
|
|
3308
|
+
|
|
3239
3309
|
### `reply` — controlling the action's reply
|
|
3240
3310
|
|
|
3241
3311
|
By default an action re-renders its component in place. To do more, **return**
|
|
@@ -4042,7 +4112,8 @@ The endpoint runs your action inside a transaction and renders the reply
|
|
|
4042
4112
|
after an enqueue renders from a database the job has not touched yet, and is
|
|
4043
4113
|
*guaranteed* to draw the pre-job world — rows still present, buttons still live,
|
|
4044
4114
|
counts unchanged — sitting right beside the "Queued 177 transfers" flash the
|
|
4045
|
-
same reply emitted:
|
|
4115
|
+
same reply emitted. (An action declared `transaction: false` is no way out: its
|
|
4116
|
+
enqueue can publish at once, so the morph races the job instead.)
|
|
4046
4117
|
|
|
4047
4118
|
```ruby
|
|
4048
4119
|
def restore_all
|
|
@@ -4373,7 +4444,8 @@ Phlex::Reactive.authorization_errors = [Pundit::NotAuthorizedError]
|
|
|
4373
4444
|
|
|
4374
4445
|
# verify_authorized (ON by default): an action that authorizes NOTHING raises
|
|
4375
4446
|
# AuthorizationNotVerified inside the transaction (the mutation rolls back —
|
|
4376
|
-
# fail-closed
|
|
4447
|
+
# fail-closed; an action declared transaction: false has no endpoint transaction,
|
|
4448
|
+
# so the endpoint rolls nothing back). Satisfy it by calling one of authorization_methods, calling
|
|
4377
4449
|
# mark_authorized! after a bespoke check, or declaring skip_verify_authorized on
|
|
4378
4450
|
# a genuinely public component/action. Set the method names to match your library:
|
|
4379
4451
|
Phlex::Reactive.authorization_methods = %i[authorize! authorize allowed_to?]
|
|
@@ -4819,7 +4891,8 @@ end
|
|
|
4819
4891
|
**`run_reactive` — the no-HTTP action driver.** It runs the action through the
|
|
4820
4892
|
SAME contract the endpoint enforces — default-deny, the signed identity
|
|
4821
4893
|
round-trip (a record-backed component's row is re-found), schema coercion, the
|
|
4822
|
-
transaction wrapper
|
|
4894
|
+
transaction wrapper (skipped for a `transaction: false` action, as at the
|
|
4895
|
+
endpoint) — with no HTTP, and returns a `Result`. So a unit test can't
|
|
4823
4896
|
pass on a component that would fail a real click:
|
|
4824
4897
|
|
|
4825
4898
|
```ruby
|
|
@@ -333,7 +333,11 @@ module Phlex
|
|
|
333
333
|
|
|
334
334
|
# Run the action inside a transaction so transactional broadcasts (pgbus
|
|
335
335
|
# broadcasts_to ... durable:) defer to after_commit and never fire for a
|
|
336
|
-
# rolled-back change. Override to add per-request instrumentation.
|
|
336
|
+
# rolled-back change. Override to add per-request instrumentation. An
|
|
337
|
+
# action declared `transaction: false` (issue #481) skips the endpoint's
|
|
338
|
+
# transaction_wrapper, and with it this guarantee: its broadcasts defer to
|
|
339
|
+
# its own transactions' commits, or fire at once, and nothing the endpoint
|
|
340
|
+
# opened rolls back. The connection id and around_action below are the same.
|
|
337
341
|
#
|
|
338
342
|
# The actor's SSE connection id (sent as X-Pgbus-Connection) is exposed
|
|
339
343
|
# for the duration of the action via Phlex::Reactive.current_connection_id,
|
|
@@ -360,32 +364,42 @@ module Phlex
|
|
|
360
364
|
end
|
|
361
365
|
|
|
362
366
|
# The action inside the around_action stack, the transaction and the
|
|
363
|
-
# verify_authorized window (see run_action).
|
|
367
|
+
# verify_authorized window (see run_action). An action declared
|
|
368
|
+
# `transaction: false` (issue #481) runs the same window without
|
|
369
|
+
# transaction_wrapper, so an overridden wrapper is not called for it.
|
|
364
370
|
def run_action_wrapped(component, action_def, coerced)
|
|
365
371
|
with_around_actions(component, action_def, coerced) do
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
# yield's-worth of overhead; verify! is a no-op when the feature is
|
|
371
|
-
# off / the action is skipped / something marked. The interceptor
|
|
372
|
-
# (create_action's instrument!) marks the window on a real
|
|
373
|
-
# authorization call.
|
|
374
|
-
Phlex::Reactive::Authorization.with_tracking do
|
|
375
|
-
result = if coerced.any?
|
|
376
|
-
component.public_send(action_def.name, **coerced)
|
|
377
|
-
else
|
|
378
|
-
component.public_send(action_def.name)
|
|
379
|
-
end
|
|
380
|
-
Phlex::Reactive::Authorization.verify!(component.class, action_def)
|
|
381
|
-
result
|
|
382
|
-
end
|
|
372
|
+
if action_def.transaction
|
|
373
|
+
transaction_wrapper { run_verified_action(component, action_def, coerced) }
|
|
374
|
+
else
|
|
375
|
+
run_verified_action(component, action_def, coerced)
|
|
383
376
|
end
|
|
384
377
|
end
|
|
385
378
|
end
|
|
386
379
|
|
|
380
|
+
# verify_authorized (issue #168): open the tracking window, run the action,
|
|
381
|
+
# then verify. Inside the transaction an unverified action rolls its
|
|
382
|
+
# mutation back (fail-closed); for a `transaction: false` action the
|
|
383
|
+
# endpoint has nothing to roll back, so only the request fails. with_tracking is
|
|
384
|
+
# a bare yield's-worth of overhead; verify! is a no-op when the feature is
|
|
385
|
+
# off / the action is skipped / something marked. The interceptor
|
|
386
|
+
# (create_action's instrument!) marks the window on a real authorization
|
|
387
|
+
# call.
|
|
388
|
+
def run_verified_action(component, action_def, coerced)
|
|
389
|
+
Phlex::Reactive::Authorization.with_tracking do
|
|
390
|
+
result = if coerced.any?
|
|
391
|
+
component.public_send(action_def.name, **coerced)
|
|
392
|
+
else
|
|
393
|
+
component.public_send(action_def.name)
|
|
394
|
+
end
|
|
395
|
+
Phlex::Reactive::Authorization.verify!(component.class, action_def)
|
|
396
|
+
result
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
|
|
387
400
|
# Fold the registered around_action wrappers around `block`, innermost being
|
|
388
|
-
# the
|
|
401
|
+
# the action (in its transaction unless declared `transaction: false`).
|
|
402
|
+
# The empty-stack fast path is a bare `yield` —
|
|
389
403
|
# the default request gains only one Array#empty? check. Each wrapper is
|
|
390
404
|
# called with the frozen ActionContext and the continuation as its block, so
|
|
391
405
|
# a well-behaved wrapper returns action.call's value and the action's
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
// The markup is the Ruby side's (reactive_subscribe):
|
|
6
6
|
//
|
|
7
7
|
// data-reactive-subscribe="<signed src>" the stream's signed SSE src
|
|
8
|
+
// data-reactive-subscribe-since="<id>" a durable stream's watermark at
|
|
9
|
+
// render (issue #478)
|
|
8
10
|
//
|
|
9
11
|
// While the root is on the page, the module keeps a <pgbus-stream-source> for
|
|
10
12
|
// that src in a hidden box at the end of <body> (marked
|
|
@@ -20,12 +22,27 @@
|
|
|
20
22
|
// the roots stay connected, the module mints a fresh source for every stream
|
|
21
23
|
// a live root still needs.
|
|
22
24
|
//
|
|
25
|
+
// Catching up (issue #478). A source opens with since-id set to the lowest
|
|
26
|
+
// watermark of the roots on its src, so pgbus replays a durable broadcast
|
|
27
|
+
// sent between the render and the connection. A source minted again (after a
|
|
28
|
+
// page render dropped it) opens from what its roots render now, their
|
|
29
|
+
// watermarks: it may replay a frame a root already shows, never skip one.
|
|
30
|
+
// Each time
|
|
31
|
+
// a source connects (its pgbus:connected), every root on it gets
|
|
32
|
+
// reactive:subscribed (detail { connectionId, reconnect }; reconnect is false
|
|
33
|
+
// on the stream's first connection), not bubbling, so a component can refetch
|
|
34
|
+
// what an ephemeral stream could not replay. A root that joins a source
|
|
35
|
+
// already connected gets no event: the source is live, and a root that comes
|
|
36
|
+
// back under the same id (a replace) must not refresh again and again.
|
|
37
|
+
//
|
|
23
38
|
// This module never imports the core.
|
|
24
39
|
|
|
25
40
|
const ATTR = "data-reactive-subscribe"
|
|
41
|
+
const SINCE = "data-reactive-subscribe-since"
|
|
26
42
|
const MARK = "data-reactive-subscribe-source"
|
|
27
43
|
|
|
28
|
-
// src -> { source, ids }: one source per stream,
|
|
44
|
+
// src -> { source, ids, connected }: one source per stream, the root ids on
|
|
45
|
+
// it, and whether it ever connected.
|
|
29
46
|
const streams = new Map()
|
|
30
47
|
// root id -> { id, root, src, removing, observer }: kept across a replace.
|
|
31
48
|
// The id is the one the root had at connect: a root whose id changes while
|
|
@@ -89,8 +106,9 @@ function rejoin() {
|
|
|
89
106
|
|
|
90
107
|
function join(src, id) {
|
|
91
108
|
let stream = streams.get(src)
|
|
92
|
-
if (!stream
|
|
109
|
+
if (!stream) streams.set(src, (stream = { source: null, ids: new Set(), connected: false }))
|
|
93
110
|
stream.ids.add(id)
|
|
111
|
+
if (!stream.source?.isConnected) stream.source = mint(src, stream)
|
|
94
112
|
}
|
|
95
113
|
|
|
96
114
|
function leave(src, id) {
|
|
@@ -102,7 +120,7 @@ function leave(src, id) {
|
|
|
102
120
|
stream.source.remove()
|
|
103
121
|
}
|
|
104
122
|
|
|
105
|
-
function mint(src) {
|
|
123
|
+
function mint(src, stream) {
|
|
106
124
|
// pgbus's client is an async module script: mint the element anyway (it
|
|
107
125
|
// upgrades when pgbus defines it), and call a still-missing client a
|
|
108
126
|
// misconfiguration only after a grace period.
|
|
@@ -120,11 +138,38 @@ function mint(src) {
|
|
|
120
138
|
source.setAttribute("src", src)
|
|
121
139
|
source.setAttribute(MARK, "")
|
|
122
140
|
source.hidden = true
|
|
141
|
+
// Before the append: pgbus reads since-id once, when the element connects.
|
|
142
|
+
const since = lowestWatermark(src)
|
|
143
|
+
if (since != null) source.setAttribute("since-id", String(since))
|
|
144
|
+
source.addEventListener("pgbus:connected", (event) => {
|
|
145
|
+
if (stream.source !== source) return
|
|
146
|
+
const reconnect = stream.connected
|
|
147
|
+
stream.connected = true
|
|
148
|
+
const connectionId = event.detail?.connectionId ?? null
|
|
149
|
+
for (const id of stream.ids) {
|
|
150
|
+
const entry = roots.get(id)
|
|
151
|
+
if (!entry || entry.removing || entry.src !== src || !entry.root.isConnected) continue
|
|
152
|
+
entry.root.dispatchEvent(new CustomEvent("reactive:subscribed", { detail: { connectionId, reconnect } }))
|
|
153
|
+
}
|
|
154
|
+
})
|
|
123
155
|
minted.add(source)
|
|
124
156
|
box().appendChild(source)
|
|
125
157
|
return source
|
|
126
158
|
}
|
|
127
159
|
|
|
160
|
+
// Where a durable stream's source replays from: the lowest watermark of the
|
|
161
|
+
// roots on its src (two roots rendered apart share the source). null for an
|
|
162
|
+
// ephemeral stream (no root carries a watermark).
|
|
163
|
+
function lowestWatermark(src) {
|
|
164
|
+
let since = null
|
|
165
|
+
for (const root of document.querySelectorAll(`[${SINCE}]`)) {
|
|
166
|
+
if (root.getAttribute(ATTR) !== src) continue
|
|
167
|
+
const value = Number(root.getAttribute(SINCE))
|
|
168
|
+
if (Number.isFinite(value) && value >= 0 && (since == null || value < since)) since = value
|
|
169
|
+
}
|
|
170
|
+
return since
|
|
171
|
+
}
|
|
172
|
+
|
|
128
173
|
// The minted sources live in one hidden <reactive-stream-sources> at the end
|
|
129
174
|
// of <body>, data-turbo-permanent: a page refresh by morph (Turbo 8) keeps
|
|
130
175
|
// it, sources and connections included. A tag no server page renders, so
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var i=new Map,s=new Map,a=new WeakMap,b=new WeakSet,u=!1;function w(t){if(!u)u=!0,document.addEventListener("turbo:morph",d),document.addEventListener("turbo:render",d);let n=t.element,e=s.get(n.id);if(!e)s.set(n.id,e={id:n.id,src:null});a.set(n,e),e.observer?.disconnect(),e.root=n,e.removing=!1;for(let o of document.querySelectorAll("pgbus-stream-source[data-reactive-subscribe-source]"))if(!b.has(o))o.remove();c(e,n.getAttribute("data-reactive-subscribe")),e.observer=new MutationObserver(()=>c(e,n.getAttribute("data-reactive-subscribe"))),e.observer.observe(n,{attributes:!0,attributeFilter:["data-reactive-subscribe"]})}function C(t){let n=t.element,e=a.get(n);if(!e||e.root!==n)return;e.observer.disconnect(),e.removing=!0,queueMicrotask(()=>{if(!e.removing)return;s.delete(e.id),c(e,null)})}function c(t,n){let e=t.src;if(t.src=n||null,t.src)f(t.src,t.id);if(e&&e!==t.src)A(e,t.id)}function d(){for(let t of s.values())if(t.src&&!t.removing&&t.root.isConnected)f(t.src,t.id)}function f(t,n){let e=i.get(t);if(!e)i.set(t,e={source:null,ids:new Set,connected:!1});if(e.ids.add(n),!e.source?.isConnected)e.source=h(t,e)}function A(t,n){let e=i.get(t);if(!e)return;if(e.ids.delete(n),e.ids.size)return;i.delete(t),e.source.remove()}function h(t,n){if(!l())setTimeout(()=>{if(!l())console.error('[phlex-reactive] reactive_subscribe needs the pgbus client (<pgbus-stream-source>) on this page: import "pgbus/stream_source_element"')},5000);let e=document.createElement("pgbus-stream-source");e.setAttribute("src",t),e.setAttribute("data-reactive-subscribe-source",""),e.hidden=!0;let o=T(t);if(o!=null)e.setAttribute("since-id",String(o));return e.addEventListener("pgbus:connected",(m)=>{if(n.source!==e)return;let v=n.connected;n.connected=!0;let g=m.detail?.connectionId??null;for(let p of n.ids){let r=s.get(p);if(!r||r.removing||r.src!==t||!r.root.isConnected)continue;r.root.dispatchEvent(new CustomEvent("reactive:subscribed",{detail:{connectionId:g,reconnect:v}}))}}),b.add(e),E().appendChild(e),e}function T(t){let n=null;for(let e of document.querySelectorAll("[data-reactive-subscribe-since]")){if(e.getAttribute("data-reactive-subscribe")!==t)continue;let o=Number(e.getAttribute("data-reactive-subscribe-since"));if(Number.isFinite(o)&&o>=0&&(n==null||o<n))n=o}return n}function E(){let t=[...document.body.children].find((n)=>n.localName==="reactive-stream-sources");if(!t)t=document.createElement("reactive-stream-sources"),t.hidden=!0,t.setAttribute("data-turbo-permanent",""),document.body.appendChild(t);return t}var l=()=>!!globalThis.customElements?.get?.("pgbus-stream-source");export{w as connect,C as disconnect};
|
|
2
2
|
|
|
3
|
-
//# debugId=
|
|
3
|
+
//# debugId=87CA872C63C935B364756E2164756E21
|
|
4
4
|
//# sourceMappingURL=subscribe.min.js.map
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["features/subscribe.js"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"// phlex/reactive/features/subscribe — a root subscribed to its own broadcast\n// stream (issue #474). One of the feature modules the core imports on demand\n// (issue #275); in the default client it is part of the one file.\n//\n// The markup is the Ruby side's (reactive_subscribe):\n//\n// data-reactive-subscribe=\"<signed src>\" the stream's signed SSE src\n//\n// While the root is on the page, the module keeps a <pgbus-stream-source> for\n// that src in a hidden box at the end of <body> (marked\n// data-reactive-subscribe-source); pgbus's element\n// applies what arrives as Turbo Stream actions. Two roots with the same src\n// share ONE source (an append lands once, not once per root): it closes when\n// the last of them goes. A replace of the root disconnects and reconnects the\n// controller: the source is released only a microtask after a disconnect, so\n// the same root id coming back keeps the connection. A change of the src (a\n// morph that re-keys the root) moves the root to the new stream; a morph that\n// keeps it changes nothing. A page refresh by morph (Turbo 8) keeps the\n// source, connection and all (see box). Should a render drop it anyway while\n// the roots stay connected, the module mints a fresh source for every stream\n// a live root still needs.\n//\n// This module never imports the core.\n\nconst ATTR = \"data-reactive-subscribe\"\nconst MARK = \"data-reactive-subscribe-source\"\n\n// src -> { source, ids }: one source per stream,
|
|
5
|
+
"// phlex/reactive/features/subscribe — a root subscribed to its own broadcast\n// stream (issue #474). One of the feature modules the core imports on demand\n// (issue #275); in the default client it is part of the one file.\n//\n// The markup is the Ruby side's (reactive_subscribe):\n//\n// data-reactive-subscribe=\"<signed src>\" the stream's signed SSE src\n// data-reactive-subscribe-since=\"<id>\" a durable stream's watermark at\n// render (issue #478)\n//\n// While the root is on the page, the module keeps a <pgbus-stream-source> for\n// that src in a hidden box at the end of <body> (marked\n// data-reactive-subscribe-source); pgbus's element\n// applies what arrives as Turbo Stream actions. Two roots with the same src\n// share ONE source (an append lands once, not once per root): it closes when\n// the last of them goes. A replace of the root disconnects and reconnects the\n// controller: the source is released only a microtask after a disconnect, so\n// the same root id coming back keeps the connection. A change of the src (a\n// morph that re-keys the root) moves the root to the new stream; a morph that\n// keeps it changes nothing. A page refresh by morph (Turbo 8) keeps the\n// source, connection and all (see box). Should a render drop it anyway while\n// the roots stay connected, the module mints a fresh source for every stream\n// a live root still needs.\n//\n// Catching up (issue #478). A source opens with since-id set to the lowest\n// watermark of the roots on its src, so pgbus replays a durable broadcast\n// sent between the render and the connection. A source minted again (after a\n// page render dropped it) opens from what its roots render now, their\n// watermarks: it may replay a frame a root already shows, never skip one.\n// Each time\n// a source connects (its pgbus:connected), every root on it gets\n// reactive:subscribed (detail { connectionId, reconnect }; reconnect is false\n// on the stream's first connection), not bubbling, so a component can refetch\n// what an ephemeral stream could not replay. A root that joins a source\n// already connected gets no event: the source is live, and a root that comes\n// back under the same id (a replace) must not refresh again and again.\n//\n// This module never imports the core.\n\nconst ATTR = \"data-reactive-subscribe\"\nconst SINCE = \"data-reactive-subscribe-since\"\nconst MARK = \"data-reactive-subscribe-source\"\n\n// src -> { source, ids, connected }: one source per stream, the root ids on\n// it, and whether it ever connected.\nconst streams = new Map()\n// root id -> { id, root, src, removing, observer }: kept across a replace.\n// The id is the one the root had at connect: a root whose id changes while\n// connected still releases what it holds (byRoot finds its entry).\nconst roots = new Map()\nconst byRoot = new WeakMap()\n// The sources this module minted (a Turbo cache restore brings back clones).\nconst minted = new WeakSet()\nlet listening = false\n\nexport function connect(controller) {\n if (!listening) {\n listening = true\n document.addEventListener(\"turbo:morph\", rejoin)\n document.addEventListener(\"turbo:render\", rejoin)\n }\n const root = controller.element\n let entry = roots.get(root.id)\n if (!entry) roots.set(root.id, (entry = { id: root.id, src: null }))\n byRoot.set(root, entry)\n entry.observer?.disconnect()\n entry.root = root\n entry.removing = false\n for (const clone of document.querySelectorAll(`pgbus-stream-source[${MARK}]`)) {\n if (!minted.has(clone)) clone.remove()\n }\n follow(entry, root.getAttribute(ATTR))\n entry.observer = new MutationObserver(() => follow(entry, root.getAttribute(ATTR)))\n entry.observer.observe(root, { attributes: true, attributeFilter: [ATTR] })\n}\n\nexport function disconnect(controller) {\n const root = controller.element\n const entry = byRoot.get(root)\n if (!entry || entry.root !== root) return\n entry.observer.disconnect()\n entry.removing = true\n queueMicrotask(() => {\n if (!entry.removing) return\n roots.delete(entry.id)\n follow(entry, null)\n })\n}\n\n// Move the root to `src` (null: no stream): join the new one first, so a\n// shared source is never closed and reopened, then leave the old.\nfunction follow(entry, src) {\n const previous = entry.src\n entry.src = src || null\n if (entry.src) join(entry.src, entry.id)\n if (previous && previous !== entry.src) leave(previous, entry.id)\n}\n\n// After a page render, re-mint each source the page lost while a root on its\n// stream is still on the page (a root that left releases it on its own).\nfunction rejoin() {\n for (const entry of roots.values()) {\n if (entry.src && !entry.removing && entry.root.isConnected) join(entry.src, entry.id)\n }\n}\n\nfunction join(src, id) {\n let stream = streams.get(src)\n if (!stream) streams.set(src, (stream = { source: null, ids: new Set(), connected: false }))\n stream.ids.add(id)\n if (!stream.source?.isConnected) stream.source = mint(src, stream)\n}\n\nfunction leave(src, id) {\n const stream = streams.get(src)\n if (!stream) return\n stream.ids.delete(id)\n if (stream.ids.size) return\n streams.delete(src)\n stream.source.remove()\n}\n\nfunction mint(src, stream) {\n // pgbus's client is an async module script: mint the element anyway (it\n // upgrades when pgbus defines it), and call a still-missing client a\n // misconfiguration only after a grace period.\n if (!defined()) {\n setTimeout(() => {\n if (!defined()) {\n console.error(\n \"[phlex-reactive] reactive_subscribe needs the pgbus client (<pgbus-stream-source>) on this page: \" +\n 'import \"pgbus/stream_source_element\"',\n )\n }\n }, 5000)\n }\n const source = document.createElement(\"pgbus-stream-source\")\n source.setAttribute(\"src\", src)\n source.setAttribute(MARK, \"\")\n source.hidden = true\n // Before the append: pgbus reads since-id once, when the element connects.\n const since = lowestWatermark(src)\n if (since != null) source.setAttribute(\"since-id\", String(since))\n source.addEventListener(\"pgbus:connected\", (event) => {\n if (stream.source !== source) return\n const reconnect = stream.connected\n stream.connected = true\n const connectionId = event.detail?.connectionId ?? null\n for (const id of stream.ids) {\n const entry = roots.get(id)\n if (!entry || entry.removing || entry.src !== src || !entry.root.isConnected) continue\n entry.root.dispatchEvent(new CustomEvent(\"reactive:subscribed\", { detail: { connectionId, reconnect } }))\n }\n })\n minted.add(source)\n box().appendChild(source)\n return source\n}\n\n// Where a durable stream's source replays from: the lowest watermark of the\n// roots on its src (two roots rendered apart share the source). null for an\n// ephemeral stream (no root carries a watermark).\nfunction lowestWatermark(src) {\n let since = null\n for (const root of document.querySelectorAll(`[${SINCE}]`)) {\n if (root.getAttribute(ATTR) !== src) continue\n const value = Number(root.getAttribute(SINCE))\n if (Number.isFinite(value) && value >= 0 && (since == null || value < since)) since = value\n }\n return since\n}\n\n// The minted sources live in one hidden <reactive-stream-sources> at the end\n// of <body>, data-turbo-permanent: a page refresh by morph (Turbo 8) keeps\n// it, sources and connections included. A tag no server page renders, so\n// Idiomorph never pairs it with (and drops) a node the refreshed page adds.\nfunction box() {\n let el = [...document.body.children].find((child) => child.localName === \"reactive-stream-sources\")\n if (!el) {\n el = document.createElement(\"reactive-stream-sources\")\n el.hidden = true\n el.setAttribute(\"data-turbo-permanent\", \"\")\n document.body.appendChild(el)\n }\n return el\n}\n\nconst defined = () => !!globalThis.customElements?.get?.(\"pgbus-stream-source\")\n"
|
|
6
6
|
],
|
|
7
|
-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