phlex-reactive 0.16.13 → 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 +99 -16
- data/README.md +60 -3
- data/app/javascript/phlex/reactive/core.js +1 -0
- data/app/javascript/phlex/reactive/core.min.js +4 -4
- 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 +37 -2
- data/lib/phlex/reactive/component/dsl.rb +3 -0
- data/lib/phlex/reactive/component/editable.rb +3 -0
- data/lib/phlex/reactive/component/helpers.rb +26 -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/js.rb +51 -0
- data/lib/phlex/reactive/response.rb +4 -1
- data/lib/phlex/reactive/streamable.rb +10 -3
- data/lib/phlex/reactive/subscribe.rb +125 -0
- data/lib/phlex/reactive/version.rb +1 -1
- metadata +6 -1
|
@@ -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
|