live_cable 0.4.0 → 0.5.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 +368 -196
- data/README.md +87 -54
- data/app/assets/javascript/controllers/live_controller.js +10 -0
- data/app/assets/javascript/loading.js +3 -3
- data/app/assets/javascript/subscriptions.js +137 -60
- data/app/helpers/live_cable_helper.rb +18 -4
- data/lib/live_cable/arel_predications.rb +15 -0
- data/lib/live_cable/component/broadcasting.rb +13 -5
- data/lib/live_cable/component/dispatching.rb +2 -1
- data/lib/live_cable/component/events.rb +2 -1
- data/lib/live_cable/component/rendering.rb +25 -4
- data/lib/live_cable/connection/broadcasting.rb +42 -10
- data/lib/live_cable/connection/messaging.rb +18 -3
- data/lib/live_cable/connection.rb +5 -5
- data/lib/live_cable/defaults_signer.rb +62 -2
- data/lib/live_cable/delegation/hash.rb +5 -0
- data/lib/live_cable/delegation/model.rb +6 -1
- data/lib/live_cable/delegator.rb +23 -15
- data/lib/live_cable/engine.rb +5 -0
- data/lib/live_cable/model_observer.rb +46 -1
- data/lib/live_cable/observer_tracking.rb +10 -1
- data/lib/live_cable/predicate_builder.rb +19 -1
- data/lib/live_cable/record_ids.rb +37 -0
- data/lib/live_cable/render_context.rb +66 -14
- data/lib/live_cable/rendering/controller_renderer.rb +15 -11
- data/lib/live_cable/rendering/dependency_visitor.rb +35 -2
- data/lib/live_cable/rendering/handler.rb +1 -1
- data/lib/live_cable/rendering/partial.rb +16 -12
- data/lib/live_cable/rendering/partial_renderer.rb +30 -65
- data/lib/live_cable/rendering/renderer.rb +19 -3
- data/lib/live_cable/rendering/view_buffer.rb +3 -2
- data/lib/live_cable/testing/render_state.rb +1 -1
- data/lib/live_cable/testing/test_component.rb +2 -2
- data/lib/live_cable/testing.rb +4 -2
- data/lib/live_cable/version.rb +1 -1
- metadata +11 -9
|
@@ -12,6 +12,7 @@ module LiveCable
|
|
|
12
12
|
return unless data['messages'].present?
|
|
13
13
|
|
|
14
14
|
@reply_to = component
|
|
15
|
+
@carried = Set.new
|
|
15
16
|
|
|
16
17
|
data['messages'].each do |message|
|
|
17
18
|
action(component, message)
|
|
@@ -22,16 +23,30 @@ module LiveCable
|
|
|
22
23
|
# Guarantee exactly one reply per message batch so the client can
|
|
23
24
|
# clear its loading state: the component's own render, an _error,
|
|
24
25
|
# or this
|
|
25
|
-
component.broadcast_ack if take_reply(component)
|
|
26
|
+
component.broadcast_ack(rendered: @carried.include?(component.live_id)) if take_reply(component)
|
|
26
27
|
ensure
|
|
27
28
|
@reply_to = nil
|
|
29
|
+
@carried = nil
|
|
28
30
|
end
|
|
29
31
|
end
|
|
30
32
|
|
|
33
|
+
# Notes the children a frame sent during a message batch rendered inline
|
|
34
|
+
#
|
|
35
|
+
# @param children [Hash{String => Hash}, nil] the frame's c
|
|
36
|
+
def carried(children)
|
|
37
|
+
@carried&.merge(children.keys) if children
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @return [Boolean] whether the component sent the message batch being
|
|
41
|
+
# processed and no frame has answered it yet
|
|
42
|
+
def awaiting_reply?(component)
|
|
43
|
+
@reply_to.equal?(component)
|
|
44
|
+
end
|
|
45
|
+
|
|
31
46
|
# @return [Boolean] true, once, for the frame that answers the message
|
|
32
|
-
# batch the component sent
|
|
47
|
+
# batch the component sent; never while one of its messages still runs
|
|
33
48
|
def take_reply(component)
|
|
34
|
-
return false unless component && @reply_to.equal?(component)
|
|
49
|
+
return false unless component && @reply_to.equal?(component) && !component.current_dispatch
|
|
35
50
|
|
|
36
51
|
@reply_to = nil
|
|
37
52
|
true
|
|
@@ -37,16 +37,16 @@ module LiveCable
|
|
|
37
37
|
@monitor.synchronize(&)
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
# A fresh view
|
|
41
|
-
#
|
|
40
|
+
# A fresh controller and view per socket render, on a request and response
|
|
41
|
+
# kept until a code reload replaces ApplicationController.
|
|
42
42
|
#
|
|
43
43
|
# @return [ActionView::Base]
|
|
44
44
|
def view_context
|
|
45
|
-
unless
|
|
46
|
-
@
|
|
45
|
+
unless ApplicationController.equal?(@renderer&.controller)
|
|
46
|
+
@renderer = Rendering::ControllerRenderer.from_defaults(ApplicationController)
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
-
@
|
|
49
|
+
@renderer.build_controller.view_context
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
private
|
|
@@ -17,6 +17,7 @@ module LiveCable
|
|
|
17
17
|
# and is treated as no defaults at all.
|
|
18
18
|
module DefaultsSigner
|
|
19
19
|
PURPOSE = 'live_cable/defaults'
|
|
20
|
+
JSON_SAFE = [String, Symbol, Numeric, TrueClass, FalseClass, NilClass, Date, Time].freeze
|
|
20
21
|
|
|
21
22
|
module_function
|
|
22
23
|
|
|
@@ -37,10 +38,15 @@ module LiveCable
|
|
|
37
38
|
# development and test, warns about any default that changes on the way.
|
|
38
39
|
#
|
|
39
40
|
# @param defaults [Hash]
|
|
40
|
-
# @param component_class [Class] Named in
|
|
41
|
+
# @param component_class [Class] Named in warnings and errors
|
|
41
42
|
# @return [Hash] String-keyed, JSON-native defaults
|
|
43
|
+
# @raise [ArgumentError] if a default is or holds anything but strings,
|
|
44
|
+
# symbols, numbers, booleans, nil, times, dates and permitted params,
|
|
45
|
+
# whose JSON form could put a record's attributes or an object's state
|
|
46
|
+
# in the page
|
|
42
47
|
def round_trip(defaults, component_class)
|
|
43
48
|
defaults ||= {}
|
|
49
|
+
reject_unsafe(defaults, component_class)
|
|
44
50
|
json = JSON.parse(JSON.generate(defaults.as_json))
|
|
45
51
|
|
|
46
52
|
warn_about_changed(defaults, json, component_class) if Rails.env.local?
|
|
@@ -63,6 +69,60 @@ module LiveCable
|
|
|
63
69
|
{}
|
|
64
70
|
end
|
|
65
71
|
|
|
72
|
+
def reject_unsafe(defaults, component_class)
|
|
73
|
+
defaults.each do |key, value|
|
|
74
|
+
value = undelegated(value)
|
|
75
|
+
unsafe = unsafe_part(value) or next
|
|
76
|
+
|
|
77
|
+
raise ArgumentError, "#{component_class.name} default :#{key} (#{value.class}) #{unsafe_reason(unsafe)}"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def unsafe_reason(unsafe)
|
|
82
|
+
if record?(unsafe)
|
|
83
|
+
'is or holds an ActiveRecord record, which would be signed into the page as JSON with every one of its ' \
|
|
84
|
+
'attributes. Pass its id and load the record in the component.'
|
|
85
|
+
elsif params?(unsafe)
|
|
86
|
+
"is or holds ActionController::Parameters that aren't permitted. Permit the keys it may take, " \
|
|
87
|
+
'such as params.permit(:status).'
|
|
88
|
+
else
|
|
89
|
+
"is or holds an object (#{unsafe.class}) that isn't JSON-native, which would be signed into the page as " \
|
|
90
|
+
'its JSON form, often every one of its instance variables. Pass strings, numbers, booleans, nil, times, ' \
|
|
91
|
+
'or arrays and hashes of those, and load anything else in the component.'
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The first thing in value that JSON can't carry as itself, or nil.
|
|
96
|
+
# is_a? rather than case, which TimeWithZone and Duration don't answer.
|
|
97
|
+
def unsafe_part(value)
|
|
98
|
+
value = undelegated(value)
|
|
99
|
+
|
|
100
|
+
if JSON_SAFE.any? { |type| value.is_a?(type) }
|
|
101
|
+
nil
|
|
102
|
+
elsif params?(value)
|
|
103
|
+
value.permitted? ? unsafe_part(value.to_h) : value
|
|
104
|
+
elsif value.is_a?(Hash)
|
|
105
|
+
value.each_value.lazy.filter_map { |item| unsafe_part(item) }.first
|
|
106
|
+
elsif value.is_a?(Array)
|
|
107
|
+
value.lazy.filter_map { |item| unsafe_part(item) }.first
|
|
108
|
+
else
|
|
109
|
+
value
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def params?(value)
|
|
114
|
+
defined?(ActionController::Parameters) && value.is_a?(ActionController::Parameters)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def record?(value)
|
|
118
|
+
defined?(ActiveRecord) && (value.is_a?(ActiveRecord::Base) || value.is_a?(ActiveRecord::Relation))
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def undelegated(value)
|
|
122
|
+
value = value.__getobj__ while value.is_a?(::Delegator)
|
|
123
|
+
value
|
|
124
|
+
end
|
|
125
|
+
|
|
66
126
|
def warn_about_changed(defaults, json, component_class)
|
|
67
127
|
defaults.each do |key, value|
|
|
68
128
|
next if json[key.to_s] == value
|
|
@@ -70,7 +130,7 @@ module LiveCable
|
|
|
70
130
|
LiveCable.warn_once(
|
|
71
131
|
"[LiveCable] #{component_class.name} default :#{key} (#{value.class}) is not JSON-native, so it " \
|
|
72
132
|
'reaches the component as its JSON form. Pass strings, numbers, booleans, nil, or arrays and ' \
|
|
73
|
-
'string-keyed hashes of those
|
|
133
|
+
'string-keyed hashes of those.'
|
|
74
134
|
)
|
|
75
135
|
end
|
|
76
136
|
end
|
|
@@ -8,13 +8,18 @@ module LiveCable
|
|
|
8
8
|
decorate_mutator :assign_attributes
|
|
9
9
|
decorate_mutator :update
|
|
10
10
|
|
|
11
|
+
def initialize_clone(other, **)
|
|
12
|
+
super
|
|
13
|
+
@live_cable_observers = other.live_cable_observers
|
|
14
|
+
end
|
|
15
|
+
|
|
11
16
|
# Setters, toggle!, reload and the like reach the record through here
|
|
12
17
|
def method_missing(...) # rubocop:disable Style/MissingRespondToMissing -- SimpleDelegator defines it
|
|
13
18
|
record = __getobj__
|
|
14
19
|
writes = record.live_cable_writes
|
|
15
20
|
|
|
16
21
|
begin
|
|
17
|
-
super
|
|
22
|
+
record.live_cable_forwarding { super }
|
|
18
23
|
ensure
|
|
19
24
|
notify_live_cable_observers unless record.live_cable_writes == writes
|
|
20
25
|
end
|
data/lib/live_cable/delegator.rb
CHANGED
|
@@ -79,21 +79,34 @@ module LiveCable
|
|
|
79
79
|
# wraps, copying an Array or Hash only when something inside it changes.
|
|
80
80
|
#
|
|
81
81
|
# @param value [Object]
|
|
82
|
+
# @yieldparam delegator [Delegator] each Delegator it replaces
|
|
82
83
|
# @return [Object]
|
|
83
|
-
def self.unwrap(value, unwrapped =
|
|
84
|
-
|
|
84
|
+
def self.unwrap(value, unwrapped = nil, &on_strip)
|
|
85
|
+
while value.is_a?(Delegator)
|
|
86
|
+
on_strip&.call(value)
|
|
87
|
+
value = value.__getobj__
|
|
88
|
+
end
|
|
89
|
+
return value unless value.is_a?(::Array) || value.is_a?(::Hash)
|
|
90
|
+
|
|
91
|
+
unwrapped ||= {}.compare_by_identity
|
|
85
92
|
return unwrapped[value] if unwrapped.key?(value)
|
|
86
93
|
|
|
87
94
|
unwrapped[value] = value
|
|
88
95
|
copy = nil
|
|
89
|
-
replace = lambda do |key, child|
|
|
90
|
-
plain = unwrap(child, unwrapped)
|
|
91
|
-
(copy ||= value.dup)[key] = plain unless plain.equal?(child)
|
|
92
|
-
end
|
|
93
96
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
+
if value.is_a?(::Array)
|
|
98
|
+
value.each_with_index do |child, index|
|
|
99
|
+
plain = unwrap(child, unwrapped, &on_strip)
|
|
100
|
+
(copy ||= value.dup)[index] = plain unless plain.equal?(child)
|
|
101
|
+
end
|
|
102
|
+
elsif value.each_key.any?(Delegator)
|
|
103
|
+
copy = value.dup.clear
|
|
104
|
+
value.each_pair { |key, child| copy[unwrap(key, unwrapped, &on_strip)] = unwrap(child, unwrapped, &on_strip) }
|
|
105
|
+
else
|
|
106
|
+
value.each_pair do |key, child|
|
|
107
|
+
plain = unwrap(child, unwrapped, &on_strip)
|
|
108
|
+
(copy ||= value.dup)[key] = plain unless plain.equal?(child)
|
|
109
|
+
end
|
|
97
110
|
end
|
|
98
111
|
|
|
99
112
|
unwrapped[value] = copy || value
|
|
@@ -144,12 +157,7 @@ module LiveCable
|
|
|
144
157
|
|
|
145
158
|
return value unless self.class.supported?(value)
|
|
146
159
|
|
|
147
|
-
|
|
148
|
-
self.class.new(value).tap do |delegator|
|
|
149
|
-
live_cable_observers.each do |variable, observers|
|
|
150
|
-
observers.each { |observer| delegator.add_live_cable_observer(observer, variable) }
|
|
151
|
-
end
|
|
152
|
-
end
|
|
160
|
+
self.class.new(value).tap { |delegator| share_live_cable_observers_with(delegator) }
|
|
153
161
|
end
|
|
154
162
|
end
|
|
155
163
|
end
|
data/lib/live_cable/engine.rb
CHANGED
|
@@ -37,6 +37,11 @@ module LiveCable
|
|
|
37
37
|
include ModelObserver
|
|
38
38
|
|
|
39
39
|
ActiveRecord::PredicateBuilder.prepend(PredicateBuilder)
|
|
40
|
+
Arel::Predications.prepend(ArelPredications)
|
|
41
|
+
ActiveRecord::FinderMethods.prepend(RecordIds::FinderMethods)
|
|
42
|
+
ActiveRecord::Relation.prepend(RecordIds::Relation)
|
|
43
|
+
ActiveRecord::Core::ClassMethods.prepend(RecordIds::CoreClassMethods)
|
|
44
|
+
ActiveRecord::Persistence::ClassMethods.prepend(RecordIds::PersistenceClassMethods)
|
|
40
45
|
end
|
|
41
46
|
end
|
|
42
47
|
end
|
|
@@ -9,6 +9,17 @@ module LiveCable
|
|
|
9
9
|
end
|
|
10
10
|
alias eql? ==
|
|
11
11
|
|
|
12
|
+
def initialize_copy(other)
|
|
13
|
+
super
|
|
14
|
+
@live_cable_forwarding = nil
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# A clone shares the original's attributes, so writes through it change the original
|
|
18
|
+
def initialize_clone(other, **)
|
|
19
|
+
super
|
|
20
|
+
@live_cable_observers = other.live_cable_observers
|
|
21
|
+
end
|
|
22
|
+
|
|
12
23
|
def _write_attribute(...)
|
|
13
24
|
notify_live_cable_observers
|
|
14
25
|
|
|
@@ -25,8 +36,14 @@ module LiveCable
|
|
|
25
36
|
super.tap { notify_live_cable_observers }
|
|
26
37
|
end
|
|
27
38
|
|
|
39
|
+
# Unchanged reloads stay quiet, so reloading to check a record before writing
|
|
40
|
+
# doesn't re-render
|
|
28
41
|
def reload(...)
|
|
29
|
-
super
|
|
42
|
+
return super unless live_cable_watched?
|
|
43
|
+
|
|
44
|
+
before = @attributes
|
|
45
|
+
|
|
46
|
+
super.tap { notify_live_cable_observers if live_cable_reload_changed?(before) }
|
|
30
47
|
end
|
|
31
48
|
|
|
32
49
|
# Catches in-place changes to serialized and JSON attributes once saved
|
|
@@ -39,8 +56,36 @@ module LiveCable
|
|
|
39
56
|
@live_cable_writes || 0
|
|
40
57
|
end
|
|
41
58
|
|
|
59
|
+
# @api private
|
|
60
|
+
def live_cable_forwarding
|
|
61
|
+
@live_cable_forwarding = live_cable_forwarding_depth + 1
|
|
62
|
+
|
|
63
|
+
yield
|
|
64
|
+
ensure
|
|
65
|
+
@live_cable_forwarding -= 1
|
|
66
|
+
end
|
|
67
|
+
|
|
42
68
|
private
|
|
43
69
|
|
|
70
|
+
def live_cable_forwarding_depth
|
|
71
|
+
@live_cable_forwarding || 0
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def live_cable_watched?
|
|
75
|
+
live_cable_forwarding_depth.positive? || @live_cable_observers&.each_value&.any?(&:any?)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Compares the database's values first, so columns nobody read stay unloaded
|
|
79
|
+
def live_cable_reload_changed?(before)
|
|
80
|
+
before.keys != @attributes.keys || before.keys.any? do |name|
|
|
81
|
+
old = before[name]
|
|
82
|
+
fresh = @attributes[name]
|
|
83
|
+
raw_changed = old.value_before_type_cast != fresh.value_before_type_cast
|
|
84
|
+
|
|
85
|
+
(raw_changed || old.changed_in_place?) && old.value != fresh.value
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
44
89
|
def notify_live_cable_observers
|
|
45
90
|
@live_cable_writes = live_cable_writes + 1
|
|
46
91
|
|
|
@@ -60,6 +60,13 @@ module LiveCable
|
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
+
# @api private
|
|
64
|
+
# @param other [ObserverTracking]
|
|
65
|
+
# @return [Boolean] whether both notify the same observers for the same variables
|
|
66
|
+
def same_live_cable_observers?(other)
|
|
67
|
+
live_cable_observers == other.live_cable_observers
|
|
68
|
+
end
|
|
69
|
+
|
|
63
70
|
def initialize_dup(other)
|
|
64
71
|
super
|
|
65
72
|
@live_cable_observers = {}
|
|
@@ -70,7 +77,7 @@ module LiveCable
|
|
|
70
77
|
@live_cable_observers = {}
|
|
71
78
|
end
|
|
72
79
|
|
|
73
|
-
|
|
80
|
+
protected
|
|
74
81
|
|
|
75
82
|
# Get the hash of all observers, keyed by variable name.
|
|
76
83
|
#
|
|
@@ -79,6 +86,8 @@ module LiveCable
|
|
|
79
86
|
@live_cable_observers ||= {}
|
|
80
87
|
end
|
|
81
88
|
|
|
89
|
+
private
|
|
90
|
+
|
|
82
91
|
# Get the list of observers for a specific variable.
|
|
83
92
|
#
|
|
84
93
|
# @param variable [Symbol] The variable name
|
|
@@ -4,10 +4,28 @@ module LiveCable
|
|
|
4
4
|
# Lets where(column: value) see the Array or Hash inside a reactive value
|
|
5
5
|
# instead of treating the wrapper as a single scalar.
|
|
6
6
|
module PredicateBuilder
|
|
7
|
+
# An object rather than a lambda, so relations stay marshalable.
|
|
8
|
+
class DelegatorHandler
|
|
9
|
+
def initialize(predicate_builder)
|
|
10
|
+
@predicate_builder = predicate_builder
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call(attribute, value)
|
|
14
|
+
@predicate_builder.build(attribute, value.__getobj__)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
7
18
|
def initialize(...)
|
|
8
19
|
super
|
|
9
20
|
|
|
10
|
-
register_handler(Delegator,
|
|
21
|
+
register_handler(Delegator, DelegatorHandler.new(self))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
protected
|
|
25
|
+
|
|
26
|
+
# A polymorphic association reads its _type from the record's class
|
|
27
|
+
def expand_from_hash(attributes, &)
|
|
28
|
+
super(Delegator.unwrap(attributes), &)
|
|
11
29
|
end
|
|
12
30
|
end
|
|
13
31
|
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LiveCable
|
|
4
|
+
# ActiveRecord tells a list of ids from one id with is_a?(Array), which a
|
|
5
|
+
# reactive Array isn't, so it found, destroyed or updated a single record.
|
|
6
|
+
module RecordIds
|
|
7
|
+
module FinderMethods
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def find_with_ids(*ids)
|
|
11
|
+
super(*Delegator.unwrap(ids))
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
module Relation
|
|
16
|
+
def destroy(id)
|
|
17
|
+
super(Delegator.unwrap(id))
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
module CoreClassMethods
|
|
22
|
+
def find(*ids, &)
|
|
23
|
+
super(*Delegator.unwrap(ids), &)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
module PersistenceClassMethods
|
|
28
|
+
def update(id_or_attributes, *)
|
|
29
|
+
super(Delegator.unwrap(id_or_attributes), *)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def update!(id_or_attributes, *)
|
|
33
|
+
super(Delegator.unwrap(id_or_attributes), *)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -40,13 +40,18 @@ module LiveCable
|
|
|
40
40
|
|
|
41
41
|
# @return [Array<LiveCable::Component>]
|
|
42
42
|
def owned_children
|
|
43
|
-
children_by_part.values.flatten.uniq
|
|
43
|
+
children_by_part.values.flatten.uniq(&:object_id)
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
def root?
|
|
47
47
|
root.nil?
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
# @return [LiveCable::Component] the component whose frame carries this render
|
|
51
|
+
def root_component
|
|
52
|
+
root? ? component : root.component
|
|
53
|
+
end
|
|
54
|
+
|
|
50
55
|
def clear
|
|
51
56
|
@children = nil
|
|
52
57
|
@component = nil
|
|
@@ -83,15 +88,23 @@ module LiveCable
|
|
|
83
88
|
# @return [Object] the value the template sees
|
|
84
89
|
def unwrap(delegator)
|
|
85
90
|
value = delegator.__getobj__
|
|
91
|
+
locations = (tracked_values[value] ||= [])
|
|
86
92
|
|
|
87
|
-
|
|
88
|
-
|
|
93
|
+
# `todos.first` builds a new wrapper on every call; one per set of observers is enough
|
|
94
|
+
unless locations.any? { |root, _path| root.same_live_cable_observers?(delegator) }
|
|
95
|
+
locations << [delegator, []]
|
|
89
96
|
unindexed << delegator
|
|
90
97
|
end
|
|
91
98
|
|
|
92
99
|
value
|
|
93
100
|
end
|
|
94
101
|
|
|
102
|
+
# @param value [Array, Hash] a plain collection a component method returned
|
|
103
|
+
# @return [Array, Hash] the collection with no Delegator left in it
|
|
104
|
+
def unwrap_nested(value)
|
|
105
|
+
unwrapped_nested[value] ||= Delegator.unwrap(value) { |delegator| unwrap(delegator) }
|
|
106
|
+
end
|
|
107
|
+
|
|
95
108
|
# Maps a prop back to tracked values, so a child's changes still mark this
|
|
96
109
|
# component's variables, even through a collection built in the template.
|
|
97
110
|
def track(value)
|
|
@@ -101,23 +114,39 @@ module LiveCable
|
|
|
101
114
|
index_nested(delegator, delegator.__getobj__, [])
|
|
102
115
|
end
|
|
103
116
|
|
|
104
|
-
|
|
105
|
-
|
|
117
|
+
locations = tracked_values[value]
|
|
118
|
+
|
|
119
|
+
# A child given a variable's own value shares its wrapper, so changes on either side re-render both
|
|
120
|
+
stored, *aliases = stored_wrappers[value]
|
|
121
|
+
if stored
|
|
122
|
+
(aliases + locations.to_a.map(&:first)).each { |source| source.share_live_cable_observers_with(stored) }
|
|
123
|
+
return stored
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
if locations&.one?
|
|
127
|
+
delegator, path = locations.first
|
|
128
|
+
return path.reduce(delegator) { |nested, key| nested[key] }
|
|
129
|
+
end
|
|
106
130
|
|
|
107
|
-
sources = sources_within(value)
|
|
131
|
+
sources = locations ? locations.map(&:first) : sources_within(value).keys
|
|
108
132
|
return value if sources.empty?
|
|
109
133
|
|
|
110
134
|
Delegator.new(value).tap do |wrapped|
|
|
111
|
-
sources.
|
|
135
|
+
sources.each { |source| source.share_live_cable_observers_with(wrapped) }
|
|
112
136
|
end
|
|
113
137
|
end
|
|
114
138
|
|
|
139
|
+
# A finished context is kept until the next render; drop what it unwrapped.
|
|
140
|
+
def forget_tracked_values
|
|
141
|
+
@tracked_values = @unindexed = @unwrapped_nested = @stored_wrappers = nil
|
|
142
|
+
end
|
|
143
|
+
|
|
115
144
|
private
|
|
116
145
|
|
|
117
146
|
# @return [LiveCable::RenderContext, nil]
|
|
118
147
|
attr_reader :root
|
|
119
148
|
|
|
120
|
-
# @return [Hash{Object => Array(LiveCable::Delegator, Array)}] by identity
|
|
149
|
+
# @return [Hash{Object => Array<Array(LiveCable::Delegator, Array)>}] by identity
|
|
121
150
|
def tracked_values
|
|
122
151
|
@tracked_values ||= {}.compare_by_identity
|
|
123
152
|
end
|
|
@@ -127,12 +156,30 @@ module LiveCable
|
|
|
127
156
|
@unindexed ||= []
|
|
128
157
|
end
|
|
129
158
|
|
|
130
|
-
|
|
159
|
+
# @return [Hash{Object => Array<LiveCable::Delegator>}] by identity, each variable's value to its wrappers
|
|
160
|
+
def stored_wrappers
|
|
161
|
+
@stored_wrappers ||= begin
|
|
162
|
+
variables = component.all_reactive_variables | component.shared_variables
|
|
163
|
+
|
|
164
|
+
variables.each_with_object({}.compare_by_identity) do |variable, wrappers|
|
|
165
|
+
value = component.public_send(variable)
|
|
166
|
+
(wrappers[value.__getobj__] ||= []) << value if value.is_a?(Delegator)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# @return [Hash{Object => Object}] by identity
|
|
172
|
+
def unwrapped_nested
|
|
173
|
+
@unwrapped_nested ||= {}.compare_by_identity
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def index_nested(delegator, value, path, seen = {}.compare_by_identity)
|
|
131
177
|
nested_entries(value).each do |key, child|
|
|
132
|
-
next if !Delegator.supported?(child) ||
|
|
178
|
+
next if !Delegator.supported?(child) || seen.key?(child)
|
|
133
179
|
|
|
134
|
-
|
|
135
|
-
|
|
180
|
+
seen[child] = true
|
|
181
|
+
(tracked_values[child] ||= []) << [delegator, path + [key]]
|
|
182
|
+
index_nested(delegator, child, path + [key], seen)
|
|
136
183
|
end
|
|
137
184
|
end
|
|
138
185
|
|
|
@@ -142,8 +189,13 @@ module LiveCable
|
|
|
142
189
|
next if !Delegator.supported?(child) || seen.key?(child)
|
|
143
190
|
|
|
144
191
|
seen[child] = true
|
|
145
|
-
|
|
146
|
-
|
|
192
|
+
locations = tracked_values[child]
|
|
193
|
+
|
|
194
|
+
if locations
|
|
195
|
+
locations.each { |location| sources[location.first] = true }
|
|
196
|
+
else
|
|
197
|
+
sources_within(child, sources, seen)
|
|
198
|
+
end
|
|
147
199
|
end
|
|
148
200
|
|
|
149
201
|
sources
|
|
@@ -2,23 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
module LiveCable
|
|
4
4
|
module Rendering
|
|
5
|
-
# Builds
|
|
6
|
-
#
|
|
7
|
-
# render instead of paying for a controller, request and response each time.
|
|
5
|
+
# Builds controllers as ActionController::Renderer#render does, but on one
|
|
6
|
+
# request and response it keeps, so a connection builds those only once.
|
|
8
7
|
class ControllerRenderer < ActionController::Renderer
|
|
9
8
|
# @param controller_class [Class]
|
|
10
|
-
# @return [
|
|
11
|
-
def self.
|
|
12
|
-
self.for(controller_class, nil, controller_class.renderer.defaults)
|
|
9
|
+
# @return [ControllerRenderer]
|
|
10
|
+
def self.from_defaults(controller_class)
|
|
11
|
+
self.for(controller_class, nil, controller_class.renderer.defaults)
|
|
13
12
|
end
|
|
14
13
|
|
|
15
|
-
def
|
|
16
|
-
|
|
17
|
-
request
|
|
14
|
+
def initialize(...)
|
|
15
|
+
super
|
|
16
|
+
@request = ActionDispatch::Request.new(env_for_request)
|
|
17
|
+
@request.routes = controller._routes
|
|
18
|
+
@response = controller.make_response!(@request)
|
|
19
|
+
end
|
|
18
20
|
|
|
21
|
+
# @return [ActionController::Base]
|
|
22
|
+
def build_controller
|
|
19
23
|
controller.new.tap do |instance|
|
|
20
|
-
instance.set_request!(request)
|
|
21
|
-
instance.set_response!(
|
|
24
|
+
instance.set_request!(@request)
|
|
25
|
+
instance.set_response!(@response)
|
|
22
26
|
end
|
|
23
27
|
end
|
|
24
28
|
end
|