phlex-reactive 0.16.14 → 0.17.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 +89 -22
- data/README.md +46 -0
- data/app/javascript/phlex/reactive/core.js +1 -0
- data/app/javascript/phlex/reactive/core.min.js +6 -6
- data/app/javascript/phlex/reactive/core.min.js.map +4 -4
- data/app/javascript/phlex/reactive/features/presence.js +37 -3
- data/app/javascript/phlex/reactive/features/presence.min.js +2 -2
- data/app/javascript/phlex/reactive/features/presence.min.js.map +3 -3
- data/app/javascript/phlex/reactive/features/subscribe.js +143 -0
- data/app/javascript/phlex/reactive/features/subscribe.min.js +4 -0
- data/app/javascript/phlex/reactive/features/subscribe.min.js.map +10 -0
- data/app/javascript/phlex/reactive/reactive_controller.js +2 -0
- data/app/javascript/phlex/reactive/reactive_controller.min.js +4 -4
- data/app/javascript/phlex/reactive/reactive_controller.min.js.map +7 -6
- data/app/javascript/phlex/reactive/runtime.js +25 -1
- data/lib/phlex/reactive/component/dsl.rb +3 -0
- data/lib/phlex/reactive/component/helpers.rb +8 -0
- data/lib/phlex/reactive/component/presence_declaration.rb +2 -1
- data/lib/phlex/reactive/component/registry.rb +5 -2
- data/lib/phlex/reactive/component/subscribe_declaration.rb +132 -0
- data/lib/phlex/reactive/component.rb +1 -0
- data/lib/phlex/reactive/doctor.rb +51 -4
- data/lib/phlex/reactive/engine.rb +3 -3
- data/lib/phlex/reactive/subscribe.rb +125 -0
- data/lib/phlex/reactive/version.rb +1 -1
- metadata +6 -1
|
@@ -38,6 +38,7 @@ import * as deferFeature from "phlex/reactive/features/defer"
|
|
|
38
38
|
import * as sortableFeature from "phlex/reactive/features/sortable"
|
|
39
39
|
import * as dragFeature from "phlex/reactive/features/drag"
|
|
40
40
|
import * as presenceFeature from "phlex/reactive/features/presence"
|
|
41
|
+
import * as subscribeFeature from "phlex/reactive/features/subscribe"
|
|
41
42
|
import * as countdownFeature from "phlex/reactive/features/countdown"
|
|
42
43
|
|
|
43
44
|
// phlex/reactive/runtime — the reactive controller WITHOUT its feature
|
|
@@ -2414,6 +2415,11 @@ function presenceNeeded(root) {
|
|
|
2414
2415
|
return root.getAttribute?.("data-reactive-presence") != null
|
|
2415
2416
|
}
|
|
2416
2417
|
|
|
2418
|
+
// Subscribe (issue #474): the root subscribes to its own broadcast stream.
|
|
2419
|
+
function subscribeNeeded(root) {
|
|
2420
|
+
return root.getAttribute?.("data-reactive-subscribe") != null
|
|
2421
|
+
}
|
|
2422
|
+
|
|
2417
2423
|
// Countdowns (issue #400): the root is, or holds, a reactive_countdown.
|
|
2418
2424
|
function countdownNeeded(root) {
|
|
2419
2425
|
return Boolean(root.matches?.("[data-reactive-countdown]") || root.querySelector?.("[data-reactive-countdown]"))
|
|
@@ -2436,6 +2442,9 @@ const PRODUCTION_FEATURES = [
|
|
|
2436
2442
|
// Its announce is a request of its own; nothing it does at connect is read
|
|
2437
2443
|
// by another request: it never gates.
|
|
2438
2444
|
["presence", [presenceNeeded, null]],
|
|
2445
|
+
// Its source is a connection of its own; nothing it does at connect is read
|
|
2446
|
+
// by a request: it never gates.
|
|
2447
|
+
["subscribe", [subscribeNeeded, null]],
|
|
2439
2448
|
// A countdown writes only its own text and visibility: it never gates.
|
|
2440
2449
|
["countdown", [countdownNeeded, null]],
|
|
2441
2450
|
[
|
|
@@ -2601,7 +2610,7 @@ function featureTimeoutMs() {
|
|
|
2601
2610
|
export function reactiveFeatureNames() {
|
|
2602
2611
|
return __SPLIT__
|
|
2603
2612
|
? [...FEATURES.keys()]
|
|
2604
|
-
: ["persist", "defer", "form", "bindings", "compute", "sortable", "drag", "presence", "countdown", "effects", "hints", "devtools"]
|
|
2613
|
+
: ["persist", "defer", "form", "bindings", "compute", "sortable", "drag", "presence", "subscribe", "countdown", "effects", "hints", "devtools"]
|
|
2605
2614
|
}
|
|
2606
2615
|
|
|
2607
2616
|
export function __setReactiveFeatureForTest(name, needs, load, waiting, gates) {
|
|
@@ -2626,6 +2635,7 @@ export function __loadReactiveFeatureForTest(name) {
|
|
|
2626
2635
|
sortable: sortableFeature,
|
|
2627
2636
|
drag: dragFeature,
|
|
2628
2637
|
presence: presenceFeature,
|
|
2638
|
+
subscribe: subscribeFeature,
|
|
2629
2639
|
countdown: countdownFeature,
|
|
2630
2640
|
effects: effectsFeature,
|
|
2631
2641
|
hints: hintsFeature,
|
|
@@ -2690,6 +2700,7 @@ const SORTABLE = 32
|
|
|
2690
2700
|
const PRESENCE = 64
|
|
2691
2701
|
const COUNTDOWN = 128
|
|
2692
2702
|
const DRAG = 256
|
|
2703
|
+
const SUBSCRIBE = 512
|
|
2693
2704
|
|
|
2694
2705
|
// The default entry's features are part of this file: install them now, as
|
|
2695
2706
|
// the opt-in entry does when one arrives.
|
|
@@ -3022,6 +3033,7 @@ export default class extends Controller {
|
|
|
3022
3033
|
this.#connectFeatureNow(SORTABLE, "sortable", sortableNeeded, (core) => sortableFeature.connect(this, core))
|
|
3023
3034
|
this.#connectFeatureNow(DRAG, "drag", dragNeeded, (core) => dragFeature.connect(this, core))
|
|
3024
3035
|
this.#connectFeatureNow(PRESENCE, "presence", presenceNeeded, (core) => presenceFeature.connect(this, core))
|
|
3036
|
+
this.#connectFeatureNow(SUBSCRIBE, "subscribe", subscribeNeeded, () => subscribeFeature.connect(this))
|
|
3025
3037
|
this.#connectFeatureNow(COUNTDOWN, "countdown", countdownNeeded, (core) => countdownFeature.connect(this, core))
|
|
3026
3038
|
}
|
|
3027
3039
|
}
|
|
@@ -3062,6 +3074,7 @@ export default class extends Controller {
|
|
|
3062
3074
|
if (on & SORTABLE) this.#disconnectFeatureNow("sortable", () => sortableFeature.disconnect(this, core))
|
|
3063
3075
|
if (on & DRAG) this.#disconnectFeatureNow("drag", () => dragFeature.disconnect(this, core))
|
|
3064
3076
|
if (on & PRESENCE) this.#disconnectFeatureNow("presence", () => presenceFeature.disconnect(this, core))
|
|
3077
|
+
if (on & SUBSCRIBE) this.#disconnectFeatureNow("subscribe", () => subscribeFeature.disconnect(this))
|
|
3065
3078
|
if (on & COUNTDOWN) this.#disconnectFeatureNow("countdown", () => countdownFeature.disconnect(this, core))
|
|
3066
3079
|
}
|
|
3067
3080
|
}
|
|
@@ -5243,7 +5256,18 @@ export default class extends Controller {
|
|
|
5243
5256
|
// when not present (e.g. no pgbus, or no active subscription) — the header
|
|
5244
5257
|
// is then simply omitted.
|
|
5245
5258
|
#connectionId() {
|
|
5259
|
+
// A subscribing root (issue #474) sends its own stream's connection, so
|
|
5260
|
+
// exclude: reactive_connection_id on a broadcast to that stream skips the
|
|
5261
|
+
// actor — whatever source comes first on the page.
|
|
5262
|
+
const src = this.element.getAttribute?.("data-reactive-subscribe")
|
|
5263
|
+
let own
|
|
5264
|
+
if (src) {
|
|
5265
|
+
for (const source of document.querySelectorAll("pgbus-stream-source[data-reactive-subscribe-source][connection-id]")) {
|
|
5266
|
+
if (source.getAttribute("src") === src) own = source.getAttribute("connection-id")
|
|
5267
|
+
}
|
|
5268
|
+
}
|
|
5246
5269
|
return (
|
|
5270
|
+
own ||
|
|
5247
5271
|
document.querySelector("pgbus-stream-source[connection-id]")?.getAttribute("connection-id") ||
|
|
5248
5272
|
document.querySelector('meta[name="pgbus-connection-id"]')?.content ||
|
|
5249
5273
|
null
|
|
@@ -372,6 +372,9 @@ module Phlex
|
|
|
372
372
|
# A ClientBindings-only class has no presence declaration to refuse.
|
|
373
373
|
refuse_dormant_presence! if dormant && respond_to?(:reactive_presence_declaration) &&
|
|
374
374
|
reactive_presence_declaration
|
|
375
|
+
# Issue #474: nor subscribe to its stream.
|
|
376
|
+
refuse_dormant_subscribe! if dormant && respond_to?(:reactive_subscribe_declaration) &&
|
|
377
|
+
reactive_subscribe_declaration
|
|
375
378
|
|
|
376
379
|
Phlex::Reactive::Dormant.reject_visible_lazy!(self) if dormant
|
|
377
380
|
|
|
@@ -111,6 +111,10 @@ module Phlex
|
|
|
111
111
|
# The lazy SHELL (not the real render) of an on: :visible component
|
|
112
112
|
# can never wake — refuse it rather than ship a dead placeholder.
|
|
113
113
|
Phlex::Reactive::Dormant.reject_visible_lazy!(self.class) unless Phlex::Reactive::Defer.real_render?
|
|
114
|
+
# Nor a per-render dormant root that subscribes (issue #474) or
|
|
115
|
+
# is a presence roster: it never connects, so it never would.
|
|
116
|
+
self.class.send(:refuse_dormant_subscribe!) if respond_to?(:reactive_subscribe_data)
|
|
117
|
+
self.class.send(:refuse_dormant_presence!) if respond_to?(:reactive_presence_data)
|
|
114
118
|
{ reactive_dormant: "reactive" }
|
|
115
119
|
else
|
|
116
120
|
{ controller: "reactive" }
|
|
@@ -155,6 +159,10 @@ module Phlex
|
|
|
155
159
|
# stream, from which the client mints its subscription. Only a class
|
|
156
160
|
# that declares reactive_presence has the method.
|
|
157
161
|
data.merge!(reactive_presence_data) if respond_to?(:reactive_presence_data)
|
|
162
|
+
# Subscribe (issue #474): the signed src of the component's own
|
|
163
|
+
# broadcast stream. Only a class that declares reactive_subscribe has
|
|
164
|
+
# the method.
|
|
165
|
+
data.merge!(reactive_subscribe_data) if respond_to?(:reactive_subscribe_data)
|
|
158
166
|
{ data: }
|
|
159
167
|
end
|
|
160
168
|
|
|
@@ -109,7 +109,8 @@ module Phlex
|
|
|
109
109
|
def refuse_dormant_presence!
|
|
110
110
|
raise ArgumentError,
|
|
111
111
|
"#{name}: reactive_presence cannot be dormant — a dormant root never connects, so its tab " \
|
|
112
|
-
"would never join the stream. Drop reactive_dormant on this
|
|
112
|
+
"would never join the stream. Drop reactive_dormant (or reactive_root(dormant: true)) on this " \
|
|
113
|
+
"component."
|
|
113
114
|
end
|
|
114
115
|
end
|
|
115
116
|
|
|
@@ -68,7 +68,9 @@ module Phlex
|
|
|
68
68
|
skip_all: :@reactive_own_skip_all,
|
|
69
69
|
skip_actions: :@reactive_own_skip_actions,
|
|
70
70
|
# Presence roster — reactive_presence (issue #358).
|
|
71
|
-
presence: :@reactive_own_presence
|
|
71
|
+
presence: :@reactive_own_presence,
|
|
72
|
+
# Own broadcast stream — reactive_subscribe (issue #474).
|
|
73
|
+
subscribe: :@reactive_own_subscribe
|
|
72
74
|
}.freeze
|
|
73
75
|
|
|
74
76
|
# The identity-side hot-path memos swept by bump! (see the contract
|
|
@@ -92,7 +94,8 @@ module Phlex
|
|
|
92
94
|
effects: :reactive_effects_config,
|
|
93
95
|
skip_all: :reactive_skip_all_authorization?,
|
|
94
96
|
skip_actions: :reactive_skip_authorization_actions,
|
|
95
|
-
presence: :reactive_presence_declaration
|
|
97
|
+
presence: :reactive_presence_declaration,
|
|
98
|
+
subscribe: :reactive_subscribe_declaration
|
|
96
99
|
}.freeze
|
|
97
100
|
|
|
98
101
|
# The per-class memos the render path reads (Identity's token memos,
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phlex
|
|
4
|
+
module Reactive
|
|
5
|
+
module Component
|
|
6
|
+
# The reactive_subscribe macro (issue #474). Not named `Subscribe`: this
|
|
7
|
+
# module is in every reactive component's constant lookup, where a bare
|
|
8
|
+
# noun would shadow a host app's kit component of that name (the #233
|
|
9
|
+
# rule). The behaviour it adds lives in Phlex::Reactive::Subscribe.
|
|
10
|
+
module SubscribeDeclaration
|
|
11
|
+
extend ActiveSupport::Concern
|
|
12
|
+
|
|
13
|
+
# A subscribe name is the stream name's last part: one identifier, no
|
|
14
|
+
# colon (the stream-name separator), no spaces.
|
|
15
|
+
SUBSCRIBE_NAME = /\A[a-z][a-z0-9_]*\z/
|
|
16
|
+
|
|
17
|
+
class_methods do
|
|
18
|
+
# Subscribe every rendered root of this component to a broadcast
|
|
19
|
+
# stream, for as long as the root is on the page:
|
|
20
|
+
#
|
|
21
|
+
# reactive_record :item
|
|
22
|
+
# reactive_subscribe :widget # [item, :widget]
|
|
23
|
+
# reactive_subscribe key: -> { [@item.list, :items] } # parts, run on the component
|
|
24
|
+
# reactive_subscribe key: [:lobby, :feed] # parts, fixed
|
|
25
|
+
#
|
|
26
|
+
# A name or key:, not both. One stream per component (a subclass may
|
|
27
|
+
# declare its own). broadcast_self broadcasts to it.
|
|
28
|
+
def reactive_subscribe(name = nil, key: nil)
|
|
29
|
+
declaration = build_subscribe_declaration(name, key)
|
|
30
|
+
refuse_dormant_subscribe! if reactive_dormant?
|
|
31
|
+
Registry.write_scalar(self, :subscribe, declaration)
|
|
32
|
+
include Phlex::Reactive::Subscribe::ComponentMethods
|
|
33
|
+
|
|
34
|
+
declaration
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The Phlex::Reactive::Subscribe::Declaration, or nil.
|
|
38
|
+
def reactive_subscribe_declaration
|
|
39
|
+
Registry.resolve_scalar(self, :subscribe, :reactive_subscribe_declaration)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# broadcast_to on the declared stream, so the broadcaster need not
|
|
43
|
+
# repeat the key:
|
|
44
|
+
#
|
|
45
|
+
# Widget.broadcast_self(replace: item) # the key from the Widget built for item
|
|
46
|
+
# Widget.broadcast_self(item, js: js.add_class(...)) # js: has no component: name its subject
|
|
47
|
+
#
|
|
48
|
+
# The key resolves on a component of this class: `subject` (a record,
|
|
49
|
+
# an init-kwargs Hash or a built component), else the verb's payload
|
|
50
|
+
# (built once: the same component is what gets rendered). Every other
|
|
51
|
+
# keyword is broadcast_to's, except each: (the stream is the
|
|
52
|
+
# declared one).
|
|
53
|
+
def broadcast_self(subject = nil, **opts)
|
|
54
|
+
unless reactive_subscribe_declaration
|
|
55
|
+
raise ArgumentError, "#{name}.broadcast_self needs a reactive_subscribe declaration"
|
|
56
|
+
end
|
|
57
|
+
if opts.key?(:each)
|
|
58
|
+
raise ArgumentError, "#{name}.broadcast_self broadcasts to the declared stream: it takes no each: " \
|
|
59
|
+
"(use broadcast_to(each: ...) to fan out)"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Exactly one verb, or broadcast_to's own error before a component
|
|
63
|
+
# is built for nothing.
|
|
64
|
+
verb, = send(:extract_broadcast_verb, opts.slice(*Phlex::Reactive::Streamable::BROADCAST_VERBS.keys))
|
|
65
|
+
# No subject: the verb's payload is it — built here, and handed on
|
|
66
|
+
# built, so broadcast_to does not build it a second time.
|
|
67
|
+
if subject.nil? && verb != :js
|
|
68
|
+
component = subscribe_component(opts[verb])
|
|
69
|
+
opts = opts.merge(verb => component)
|
|
70
|
+
else
|
|
71
|
+
component = subscribe_component(subject, javascript: verb == :js && subject.nil?)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
broadcast_to(*component.reactive_subscribe_parts, **opts)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
def subscribe_component(subject, javascript: false)
|
|
80
|
+
if javascript
|
|
81
|
+
raise ArgumentError,
|
|
82
|
+
"#{name}.broadcast_self(js: ...) has no component to read the stream key from: pass its " \
|
|
83
|
+
"subject first, e.g. broadcast_self(record, js: ...)"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
component = subject.is_a?(::Phlex::SGML) ? subject : send(:coerce_broadcast_payload, subject)
|
|
87
|
+
return component if component.is_a?(self)
|
|
88
|
+
|
|
89
|
+
raise ArgumentError,
|
|
90
|
+
"#{name}.broadcast_self reads the stream key from a #{name}, got a #{component.class.name}: " \
|
|
91
|
+
"pass the subject first, e.g. broadcast_self(record, replace: other_component)"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def build_subscribe_declaration(name, key)
|
|
95
|
+
if name.nil? == key.nil?
|
|
96
|
+
raise ArgumentError, "#{self.name}: reactive_subscribe takes a stream name or key:, exactly one"
|
|
97
|
+
end
|
|
98
|
+
return Phlex::Reactive::Subscribe::Declaration.new(name: nil, key: subscribe_key(key)) if key
|
|
99
|
+
|
|
100
|
+
name = name.to_s
|
|
101
|
+
unless SUBSCRIBE_NAME.match?(name)
|
|
102
|
+
raise ArgumentError,
|
|
103
|
+
"#{self.name}: reactive_subscribe name must be a lowercase identifier (it ends the stream " \
|
|
104
|
+
"name), got #{name.inspect}"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
Phlex::Reactive::Subscribe::Declaration.new(name: name.to_sym, key: nil)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def subscribe_key(key)
|
|
111
|
+
return key if key.respond_to?(:call)
|
|
112
|
+
return key.dup.freeze if key.is_a?(Array) && !key.empty?
|
|
113
|
+
|
|
114
|
+
raise ArgumentError,
|
|
115
|
+
"#{name}: reactive_subscribe key: must be the stream's parts (a non-empty Array) or a " \
|
|
116
|
+
"callable that returns them, got #{key.inspect}"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def refuse_dormant_subscribe!
|
|
120
|
+
raise ArgumentError,
|
|
121
|
+
"#{name}: reactive_subscribe cannot be dormant — a dormant root never connects, so it would " \
|
|
122
|
+
"never subscribe. Drop reactive_dormant (or reactive_root(dormant: true)) on this component."
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# ActiveSupport::Concern's bookkeeping on this module, sealed now that
|
|
127
|
+
# the body has loaded (zoolutions/phlex-reactive#364).
|
|
128
|
+
Phlex::Reactive::Shareable.seal_concern(self)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
@@ -85,6 +85,7 @@ module Phlex
|
|
|
85
85
|
id_check(components),
|
|
86
86
|
authorization_check(components),
|
|
87
87
|
dormant_check(components),
|
|
88
|
+
subscribe_check(components),
|
|
88
89
|
*early_pin_check(components)
|
|
89
90
|
].compact
|
|
90
91
|
end
|
|
@@ -286,6 +287,32 @@ module Phlex
|
|
|
286
287
|
"your entrypoint, before any controller loads:\n import \"phlex/reactive/early\"")
|
|
287
288
|
end
|
|
288
289
|
|
|
290
|
+
# Subscribing components (issue #474): reactive_subscribe needs pgbus with
|
|
291
|
+
# reactive Streams (a hard failure: the root raises at render without it)
|
|
292
|
+
# and pgbus's client element on the page — nothing else imports
|
|
293
|
+
# pgbus/stream_source_element when no view renders pgbus_stream_from. The
|
|
294
|
+
# client half is ADVISORY: the import may live in a file not scanned. No
|
|
295
|
+
# line at all when nothing subscribes.
|
|
296
|
+
def subscribe_check(components, capable: Phlex::Reactive::Subscribe.capable?, client: imports_pgbus_client?)
|
|
297
|
+
subscribing = components.select do
|
|
298
|
+
it.respond_to?(:reactive_subscribe_declaration) && it.reactive_subscribe_declaration
|
|
299
|
+
end
|
|
300
|
+
return if subscribing.empty?
|
|
301
|
+
|
|
302
|
+
label = "#{subscribing.size} subscribing #{subscribing.one? ? "component" : "components"} " \
|
|
303
|
+
"(#{subscribing.map(&:name).join(", ")})"
|
|
304
|
+
unless capable
|
|
305
|
+
return Check.new(:fail, "#{label}, but pgbus with reactive Streams is not loaded", name: :subscribe,
|
|
306
|
+
fix: "reactive_subscribe needs pgbus (its roots raise at render without it). Add pgbus, or drop the " \
|
|
307
|
+
"declaration and render turbo_stream_from with the same key next to the component.")
|
|
308
|
+
end
|
|
309
|
+
return Check.new(:ok, "#{label}; pgbus's client is imported", name: :subscribe) if client
|
|
310
|
+
|
|
311
|
+
Check.new(:unknown, "#{label}, but no import of pgbus/stream_source_element was found", name: :subscribe,
|
|
312
|
+
fix: "A subscribing root mints a <pgbus-stream-source>, which connects only once pgbus's client " \
|
|
313
|
+
"element is defined. Add to your entrypoint:\n import \"pgbus/stream_source_element\"")
|
|
314
|
+
end
|
|
315
|
+
|
|
289
316
|
# Every import map the app registers must pin phlex/reactive/early when a
|
|
290
317
|
# dormant root exists (issue #307): a map without the pin cannot resolve
|
|
291
318
|
# the specifier, so the import fails on the pages that map serves. One
|
|
@@ -334,18 +361,28 @@ module Phlex
|
|
|
334
361
|
app/javascript/application.js
|
|
335
362
|
].freeze
|
|
336
363
|
EARLY_IMPORT = %r{import\s+["']phlex/reactive/early["']}
|
|
364
|
+
# pgbus's client element, imported (statically or dynamically), or a
|
|
365
|
+
# helper that emits the import (issue #474). A bare mention (an import
|
|
366
|
+
# map pin) is not an import.
|
|
367
|
+
PGBUS_CLIENT = %r{\bimport\b[^;\n]*?["']pgbus/stream_source_element["']|\b(?:pgbus|turbo)_stream_from\b}
|
|
337
368
|
# Closed comments, which may span lines: HTML, JS block and ERB.
|
|
338
369
|
BLOCK_COMMENT = %r{<!--.*?-->|/\*.*?\*/|<%#.*?%>}m
|
|
339
370
|
# What precedes the import on its line when it is commented out: a //
|
|
340
|
-
# (not the one in a URL
|
|
341
|
-
|
|
371
|
+
# (not the one in a URL: after a scheme's colon, or a protocol-relative
|
|
372
|
+
# one opening a quoted value), an unclosed /*, or a block-comment's
|
|
373
|
+
# leading *.
|
|
374
|
+
COMMENT_BEFORE = %r{(?<![:"'=])//|/\*|\A\s*\*}
|
|
342
375
|
|
|
343
376
|
# Is there an `import "phlex/reactive/early"` in `source` that actually
|
|
344
377
|
# runs? Closed comments are dropped first, then each line is read, so a
|
|
345
378
|
# commented-out import doesn't count.
|
|
346
|
-
def self.imports_early_source?(source)
|
|
379
|
+
def self.imports_early_source?(source) = imports_source?(source, EARLY_IMPORT)
|
|
380
|
+
|
|
381
|
+
# Is there a match for `pattern` in `source` that is not commented out?
|
|
382
|
+
# Closed comments are dropped first, then each line is read.
|
|
383
|
+
def self.imports_source?(source, pattern)
|
|
347
384
|
source.gsub(BLOCK_COMMENT, "").each_line.any? do
|
|
348
|
-
(match =
|
|
385
|
+
(match = pattern.match(it)) && !match.pre_match.match?(COMMENT_BEFORE)
|
|
349
386
|
end
|
|
350
387
|
end
|
|
351
388
|
|
|
@@ -502,6 +539,16 @@ module Phlex
|
|
|
502
539
|
" (#{early})"
|
|
503
540
|
end
|
|
504
541
|
|
|
542
|
+
# Does any app JavaScript, view or component import pgbus's client
|
|
543
|
+
# element (or render a helper that emits that import)?
|
|
544
|
+
def imports_pgbus_client?
|
|
545
|
+
(stimulus_registration_files | early_import_candidates).any? do
|
|
546
|
+
Doctor.imports_source?(early_source(it), PGBUS_CLIENT)
|
|
547
|
+
rescue StandardError
|
|
548
|
+
false
|
|
549
|
+
end
|
|
550
|
+
end
|
|
551
|
+
|
|
505
552
|
def dormant_components(components)
|
|
506
553
|
components.select { it.respond_to?(:reactive_dormant?) && it.reactive_dormant? }
|
|
507
554
|
end
|
|
@@ -21,11 +21,11 @@ module Phlex
|
|
|
21
21
|
# default entry name the same features —
|
|
22
22
|
# spec/phlex/engine_client_pin_spec.rb fails when they disagree.
|
|
23
23
|
CLIENT_FEATURES = %w[
|
|
24
|
-
persist defer form bindings compute sortable drag presence countdown effects hints devtools
|
|
24
|
+
persist defer form bindings compute sortable drag presence subscribe countdown effects hints devtools
|
|
25
25
|
].freeze
|
|
26
|
-
# (Gzipped, bun zlib level 9, KB = 1,024 B: the default file
|
|
26
|
+
# (Gzipped, bun zlib level 9, KB = 1,024 B: the default file 42.4 KB; the core 19.6 KB;
|
|
27
27
|
# persist 3.2, defer 2.9, form 1.2, bindings 8.0, compute 2.0, sortable
|
|
28
|
-
# 3.1, drag 3.5, presence 1.
|
|
28
|
+
# 3.1, drag 3.5, presence 1.5, subscribe 0.9, countdown 0.9, effects 1.7, hints 0.9, devtools 1.5 KB. spec/javascript/bundle_budget.test.js
|
|
29
29
|
# is the source of truth and holds a ratchet on each.)
|
|
30
30
|
|
|
31
31
|
# Mount POST /reactive/actions -> Phlex::Reactive::ActionsController#create
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phlex
|
|
4
|
+
module Reactive
|
|
5
|
+
# A component that subscribes to its own broadcast stream (issue #474).
|
|
6
|
+
#
|
|
7
|
+
# class Widget < ApplicationComponent
|
|
8
|
+
# include Phlex::Reactive::Component
|
|
9
|
+
#
|
|
10
|
+
# reactive_record :item
|
|
11
|
+
# reactive_subscribe :widget # the stream [item, :widget]
|
|
12
|
+
#
|
|
13
|
+
# def view_template = div(**reactive_root) { @item.state }
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# # A job, later:
|
|
17
|
+
# Widget.broadcast_self(replace: item) # = Widget.broadcast_to(item, :widget, replace: item)
|
|
18
|
+
#
|
|
19
|
+
# Rendering puts the stream's signed SSE src on the root
|
|
20
|
+
# (data-reactive-subscribe); the client's subscribe feature opens one
|
|
21
|
+
# <pgbus-stream-source> for it while the root is on the page and closes it
|
|
22
|
+
# when the root goes. The key lives in one place, the declaration, for the
|
|
23
|
+
# subscriber and the broadcaster.
|
|
24
|
+
#
|
|
25
|
+
# pgbus only: the src is a pgbus signed stream name, the one
|
|
26
|
+
# pgbus_stream_from (and turbo_stream_from under pgbus) renders. Without
|
|
27
|
+
# pgbus (Action Cable) a subscribing root raises ConfigurationError at
|
|
28
|
+
# render, naming turbo_stream_from as the alternative. There is no replay:
|
|
29
|
+
# a broadcast sent before the source connects is missed (pgbus streams are
|
|
30
|
+
# ephemeral by default), as with any live subscription.
|
|
31
|
+
module Subscribe
|
|
32
|
+
# What reactive_subscribe declared: the stream name's last part (nil
|
|
33
|
+
# with key:), and key: — the stream's parts as an Array, or a callable
|
|
34
|
+
# the component instance_execs to get them (nil with a name).
|
|
35
|
+
Declaration = Data.define(:name, :key)
|
|
36
|
+
|
|
37
|
+
class << self
|
|
38
|
+
# Streams-capable pgbus that signs stream names: what the src needs.
|
|
39
|
+
# Signatures only, no query.
|
|
40
|
+
def capable?
|
|
41
|
+
Phlex::Reactive.pgbus_streams? && defined?(::Pgbus::Streams::SignedName) == "constant" &&
|
|
42
|
+
::Pgbus::Streams::SignedName.respond_to?(:sign)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Raise unless pgbus can carry the subscription — at render of a
|
|
46
|
+
# subscribing root, naming the alternative.
|
|
47
|
+
def require_capable!(klass)
|
|
48
|
+
return if capable?
|
|
49
|
+
|
|
50
|
+
raise Phlex::Reactive::ConfigurationError,
|
|
51
|
+
"#{klass.name} declares reactive_subscribe, which needs pgbus with reactive Streams (a signed " \
|
|
52
|
+
"<pgbus-stream-source> per root). On Action Cable, render turbo_stream_from with the same key " \
|
|
53
|
+
"next to the component instead."
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The stream name broadcast_to(*parts) publishes to — Turbo's and
|
|
57
|
+
# pgbus's name_from — refused when it overflows pgbus's queue-name
|
|
58
|
+
# budget (pgbus would raise on the broadcast too, and the subscription
|
|
59
|
+
# could never match it).
|
|
60
|
+
def stream_name(klass, parts)
|
|
61
|
+
name = ::Pgbus::Streams::Stream.name_from(parts)
|
|
62
|
+
budget = ::Pgbus.stream_name_budget if ::Pgbus.respond_to?(:stream_name_budget)
|
|
63
|
+
return name if budget.nil? || name.length <= budget
|
|
64
|
+
|
|
65
|
+
raise Phlex::Reactive::ConfigurationError,
|
|
66
|
+
"#{klass.name}: the reactive_subscribe stream #{name.inspect} is #{name.length} chars, over " \
|
|
67
|
+
"pgbus's stream-name budget of #{budget}. Declare shorter parts with key: (for a record, " \
|
|
68
|
+
"Pgbus.stream_key(record) is a fixed-length key) and broadcast with broadcast_self."
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The signed SSE URL a <pgbus-stream-source> subscribes with.
|
|
72
|
+
def source_src(stream_name) = Phlex::Reactive::Defer.stream_src(stream_name)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# What reactive_subscribe adds to a component's instances. Included only
|
|
76
|
+
# by a class that declares it.
|
|
77
|
+
module ComponentMethods
|
|
78
|
+
# The stream's parts: [record, name], or key: (an Array, or a callable
|
|
79
|
+
# run against this component).
|
|
80
|
+
def reactive_subscribe_parts
|
|
81
|
+
declaration = self.class.reactive_subscribe_declaration
|
|
82
|
+
key = declaration.key
|
|
83
|
+
parts =
|
|
84
|
+
if key.respond_to?(:call) then instance_exec(&key)
|
|
85
|
+
elsif key then key
|
|
86
|
+
else [reactive_subscribe_record, declaration.name]
|
|
87
|
+
end
|
|
88
|
+
parts = Array(parts)
|
|
89
|
+
return parts unless parts.empty? || parts.any?(&:nil?)
|
|
90
|
+
|
|
91
|
+
# A nil part would put every such root on one shared stream.
|
|
92
|
+
raise ArgumentError,
|
|
93
|
+
"#{self.class.name}: reactive_subscribe key: gave #{parts.inspect}; the stream's parts must be " \
|
|
94
|
+
"non-empty and none of them nil"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# The root's subscription marker (reactive_attrs): the signed src the
|
|
98
|
+
# client mints its <pgbus-stream-source> from.
|
|
99
|
+
def reactive_subscribe_data
|
|
100
|
+
Subscribe.require_capable!(self.class)
|
|
101
|
+
name = Subscribe.stream_name(self.class, reactive_subscribe_parts)
|
|
102
|
+
{ reactive_subscribe: Subscribe.source_src(name) }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
def reactive_subscribe_record
|
|
108
|
+
record_key = self.class.reactive_record_key
|
|
109
|
+
unless record_key
|
|
110
|
+
raise ArgumentError,
|
|
111
|
+
"#{self.class.name}: reactive_subscribe :#{self.class.reactive_subscribe_declaration.name} " \
|
|
112
|
+
"needs reactive_record (the stream is the record's), or declare key: (the stream's parts)"
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
record = instance_variable_get(:"@#{record_key}")
|
|
116
|
+
# A nil record would subscribe every such root to one shared ":name"
|
|
117
|
+
# stream.
|
|
118
|
+
return record unless record.nil?
|
|
119
|
+
|
|
120
|
+
raise ArgumentError, "#{self.class.name}: reactive_subscribe needs @#{record_key} (it is nil)"
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: phlex-reactive
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.17.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mikael Henriksson
|
|
@@ -170,6 +170,9 @@ files:
|
|
|
170
170
|
- app/javascript/phlex/reactive/features/sortable.js
|
|
171
171
|
- app/javascript/phlex/reactive/features/sortable.min.js
|
|
172
172
|
- app/javascript/phlex/reactive/features/sortable.min.js.map
|
|
173
|
+
- app/javascript/phlex/reactive/features/subscribe.js
|
|
174
|
+
- app/javascript/phlex/reactive/features/subscribe.min.js
|
|
175
|
+
- app/javascript/phlex/reactive/features/subscribe.min.js.map
|
|
173
176
|
- app/javascript/phlex/reactive/inspect.js
|
|
174
177
|
- app/javascript/phlex/reactive/inspect.min.js
|
|
175
178
|
- app/javascript/phlex/reactive/inspect.min.js.map
|
|
@@ -217,6 +220,7 @@ files:
|
|
|
217
220
|
- lib/phlex/reactive/component/shared_field.rb
|
|
218
221
|
- lib/phlex/reactive/component/show_options.rb
|
|
219
222
|
- lib/phlex/reactive/component/sortable.rb
|
|
223
|
+
- lib/phlex/reactive/component/subscribe_declaration.rb
|
|
220
224
|
- lib/phlex/reactive/defer.rb
|
|
221
225
|
- lib/phlex/reactive/deferred_render_job.rb
|
|
222
226
|
- lib/phlex/reactive/doctor.rb
|
|
@@ -259,6 +263,7 @@ files:
|
|
|
259
263
|
- lib/phlex/reactive/store.rb
|
|
260
264
|
- lib/phlex/reactive/stream.rb
|
|
261
265
|
- lib/phlex/reactive/streamable.rb
|
|
266
|
+
- lib/phlex/reactive/subscribe.rb
|
|
262
267
|
- lib/phlex/reactive/test_helpers.rb
|
|
263
268
|
- lib/phlex/reactive/test_helpers/matchers.rb
|
|
264
269
|
- lib/phlex/reactive/test_helpers/system.rb
|