live_cable 0.3.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 +739 -0
- data/README.md +101 -56
- data/app/assets/javascript/controllers/live_controller.js +61 -7
- data/app/assets/javascript/live_cable.js +2 -0
- data/app/assets/javascript/loading.js +61 -9
- data/app/assets/javascript/subscriptions.js +389 -42
- data/app/channels/live_channel.rb +51 -29
- data/app/helpers/live_cable_helper.rb +45 -2
- data/lib/live_cable/action_cable_connection.rb +4 -1
- data/lib/live_cable/arel_predications.rb +15 -0
- data/lib/live_cable/component/broadcasting.rb +47 -8
- data/lib/live_cable/component/dispatching.rb +37 -0
- data/lib/live_cable/component/events.rb +2 -1
- data/lib/live_cable/component/lifecycle.rb +1 -1
- data/lib/live_cable/component/reactive_variables.rb +33 -5
- data/lib/live_cable/component/rendering.rb +54 -17
- data/lib/live_cable/component/streaming.rb +35 -4
- data/lib/live_cable/component.rb +4 -3
- data/lib/live_cable/connection/broadcasting.rb +56 -12
- data/lib/live_cable/connection/component_management.rb +12 -8
- data/lib/live_cable/connection/error_handling.rb +28 -7
- data/lib/live_cable/connection/messaging.rb +54 -51
- data/lib/live_cable/connection/state_management.rb +13 -16
- data/lib/live_cable/connection.rb +33 -0
- data/lib/live_cable/container.rb +17 -11
- data/lib/live_cable/defaults_signer.rb +155 -0
- data/lib/live_cable/delegation/array.rb +27 -1
- data/lib/live_cable/delegation/hash.rb +60 -1
- data/lib/live_cable/delegation/methods.rb +25 -1
- data/lib/live_cable/delegation/model.rb +17 -0
- data/lib/live_cable/delegator.rb +68 -12
- data/lib/live_cable/dispatch.rb +54 -0
- data/lib/live_cable/engine.rb +8 -1
- data/lib/live_cable/forbidden.rb +7 -0
- data/lib/live_cable/identity_digest.rb +27 -0
- data/lib/live_cable/model_observer.rb +82 -0
- data/lib/live_cable/observer_tracking.rb +20 -2
- data/lib/live_cable/predicate_builder.rb +31 -0
- data/lib/live_cable/record_ids.rb +37 -0
- data/lib/live_cable/render_context.rb +134 -8
- data/lib/live_cable/rendering/controller_renderer.rb +30 -0
- data/lib/live_cable/rendering/dependency_visitor.rb +101 -24
- data/lib/live_cable/rendering/handler.rb +1 -1
- data/lib/live_cable/rendering/method_analyzer.rb +18 -4
- data/lib/live_cable/rendering/method_collector.rb +13 -5
- data/lib/live_cable/rendering/partial.rb +28 -12
- data/lib/live_cable/rendering/partial_renderer.rb +55 -38
- data/lib/live_cable/rendering/render_result.rb +5 -0
- data/lib/live_cable/rendering/renderer.rb +35 -15
- data/lib/live_cable/rendering/view_buffer.rb +22 -0
- data/lib/live_cable/testing/render_state.rb +6 -4
- data/lib/live_cable/testing/test_channel.rb +6 -0
- data/lib/live_cable/testing/test_component.rb +5 -5
- data/lib/live_cable/testing.rb +13 -7
- data/lib/live_cable/version.rb +1 -1
- data/lib/live_cable.rb +32 -3
- metadata +19 -9
|
@@ -4,16 +4,19 @@ module LiveCable
|
|
|
4
4
|
module Delegation
|
|
5
5
|
module Array
|
|
6
6
|
extend Methods
|
|
7
|
-
extend Enumerable
|
|
8
7
|
|
|
9
8
|
GETTER_METHODS = %i[
|
|
10
9
|
[]
|
|
10
|
+
at
|
|
11
11
|
chunk
|
|
12
12
|
collect
|
|
13
13
|
compact
|
|
14
14
|
cycle
|
|
15
|
+
detect
|
|
16
|
+
dig
|
|
15
17
|
drop
|
|
16
18
|
drop_while
|
|
19
|
+
fetch
|
|
17
20
|
filter
|
|
18
21
|
find
|
|
19
22
|
find_all
|
|
@@ -24,9 +27,14 @@ module LiveCable
|
|
|
24
27
|
group_by
|
|
25
28
|
last
|
|
26
29
|
map
|
|
30
|
+
max
|
|
31
|
+
max_by
|
|
32
|
+
min
|
|
33
|
+
min_by
|
|
27
34
|
reject
|
|
28
35
|
reverse
|
|
29
36
|
rotate
|
|
37
|
+
sample
|
|
30
38
|
select
|
|
31
39
|
shuffle
|
|
32
40
|
slice
|
|
@@ -39,21 +47,36 @@ module LiveCable
|
|
|
39
47
|
zip
|
|
40
48
|
].freeze
|
|
41
49
|
|
|
50
|
+
ITERATOR_METHODS = %i[
|
|
51
|
+
each_cons
|
|
52
|
+
each_slice
|
|
53
|
+
each_with_index
|
|
54
|
+
each_with_object
|
|
55
|
+
reverse_each
|
|
56
|
+
].freeze
|
|
57
|
+
|
|
42
58
|
MUTATIVE_METHODS = %i[
|
|
43
59
|
[]=
|
|
44
60
|
<<
|
|
61
|
+
append
|
|
45
62
|
clear
|
|
63
|
+
collect!
|
|
46
64
|
compact!
|
|
65
|
+
compact_blank!
|
|
47
66
|
concat
|
|
48
67
|
delete
|
|
49
68
|
delete_at
|
|
50
69
|
delete_if
|
|
70
|
+
extract!
|
|
71
|
+
extract_options!
|
|
51
72
|
fill
|
|
73
|
+
filter!
|
|
52
74
|
flatten!
|
|
53
75
|
insert
|
|
54
76
|
keep_if
|
|
55
77
|
map!
|
|
56
78
|
pop
|
|
79
|
+
prepend
|
|
57
80
|
push
|
|
58
81
|
reject!
|
|
59
82
|
replace
|
|
@@ -70,9 +93,12 @@ module LiveCable
|
|
|
70
93
|
].freeze
|
|
71
94
|
|
|
72
95
|
decorate_getters GETTER_METHODS
|
|
96
|
+
decorate_iterators ITERATOR_METHODS
|
|
73
97
|
decorate_mutators MUTATIVE_METHODS
|
|
74
98
|
|
|
75
99
|
def each(&)
|
|
100
|
+
return to_enum(:each) { size } unless block_given?
|
|
101
|
+
|
|
76
102
|
__getobj__.each do |v|
|
|
77
103
|
yield create_delegator(v)
|
|
78
104
|
end
|
|
@@ -5,34 +5,93 @@ module LiveCable
|
|
|
5
5
|
module Hash
|
|
6
6
|
extend Methods
|
|
7
7
|
|
|
8
|
+
GETTER_METHODS = %i[
|
|
9
|
+
[]
|
|
10
|
+
dig
|
|
11
|
+
fetch
|
|
12
|
+
].freeze
|
|
13
|
+
|
|
14
|
+
COLLECTION_GETTER_METHODS = %i[
|
|
15
|
+
fetch_values
|
|
16
|
+
values
|
|
17
|
+
values_at
|
|
18
|
+
].freeze
|
|
19
|
+
|
|
20
|
+
ITERATOR_METHODS = %i[
|
|
21
|
+
detect
|
|
22
|
+
each_cons
|
|
23
|
+
each_slice
|
|
24
|
+
each_with_index
|
|
25
|
+
each_with_object
|
|
26
|
+
find
|
|
27
|
+
max_by
|
|
28
|
+
min_by
|
|
29
|
+
reverse_each
|
|
30
|
+
].freeze
|
|
31
|
+
|
|
8
32
|
MUTATIVE_METHODS = %i[
|
|
9
33
|
[]=
|
|
10
34
|
clear
|
|
11
35
|
compact!
|
|
36
|
+
compact_blank!
|
|
37
|
+
compare_by_identity
|
|
12
38
|
deep_merge!
|
|
13
39
|
deep_stringify_keys!
|
|
40
|
+
deep_symbolize_keys!
|
|
14
41
|
deep_transform_keys!
|
|
15
42
|
deep_transform_values!
|
|
43
|
+
default=
|
|
44
|
+
default_proc=
|
|
16
45
|
delete
|
|
17
46
|
delete_if
|
|
18
47
|
except!
|
|
19
48
|
extract!
|
|
49
|
+
filter!
|
|
20
50
|
keep_if
|
|
21
51
|
merge!
|
|
22
52
|
rehash
|
|
23
53
|
reject!
|
|
54
|
+
replace
|
|
24
55
|
reverse_merge!
|
|
56
|
+
reverse_update
|
|
25
57
|
select!
|
|
26
58
|
shift
|
|
59
|
+
slice!
|
|
60
|
+
store
|
|
27
61
|
stringify_keys!
|
|
28
62
|
symbolize_keys!
|
|
63
|
+
to_options!
|
|
29
64
|
transform_keys!
|
|
30
65
|
transform_values!
|
|
31
66
|
update
|
|
67
|
+
with_defaults!
|
|
32
68
|
].freeze
|
|
33
69
|
|
|
34
|
-
|
|
70
|
+
decorate_getters GETTER_METHODS
|
|
71
|
+
decorate_collection_getters COLLECTION_GETTER_METHODS
|
|
72
|
+
decorate_iterators ITERATOR_METHODS
|
|
35
73
|
decorate_mutators MUTATIVE_METHODS
|
|
74
|
+
|
|
75
|
+
# Like Hash#each, yields the key and value separately only to blocks that
|
|
76
|
+
# take more than one argument and a [key, value] pair to the rest.
|
|
77
|
+
def each_pair(&block)
|
|
78
|
+
return to_enum(:each_pair) { size } unless block
|
|
79
|
+
|
|
80
|
+
split = block.arity > 1
|
|
81
|
+
|
|
82
|
+
__getobj__.each_pair do |key, value|
|
|
83
|
+
value = create_delegator(value)
|
|
84
|
+
|
|
85
|
+
split ? yield(key, value) : yield([key, value])
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
alias each each_pair
|
|
89
|
+
|
|
90
|
+
def each_value
|
|
91
|
+
return to_enum(:each_value) { size } unless block_given?
|
|
92
|
+
|
|
93
|
+
__getobj__.each_value { |value| yield create_delegator(value) }
|
|
94
|
+
end
|
|
36
95
|
end
|
|
37
96
|
end
|
|
38
97
|
end
|
|
@@ -19,6 +19,30 @@ module LiveCable
|
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
+
# A new collection is only wrapped when it holds something trackable, so
|
|
23
|
+
# a plain list of scalars stays a real Array.
|
|
24
|
+
def decorate_collection_getters(methods)
|
|
25
|
+
methods.each do |method|
|
|
26
|
+
define_method(method) do |*pos, **kwargs, &block|
|
|
27
|
+
result = __getobj__.method(method).call(*pos, **kwargs, &block)
|
|
28
|
+
|
|
29
|
+
result.any? { |value| Delegator.supported?(value) } ? create_delegator(result) : result
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Runs Enumerable's implementation against the delegator, so elements
|
|
35
|
+
# arrive through its wrapping #each.
|
|
36
|
+
def decorate_iterators(methods)
|
|
37
|
+
methods.each do |method|
|
|
38
|
+
iterator = ::Enumerable.instance_method(method)
|
|
39
|
+
|
|
40
|
+
define_method(method) do |*args, &block|
|
|
41
|
+
iterator.bind_call(self, *args, &block)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
22
46
|
def decorate_mutators(methods)
|
|
23
47
|
methods.each do |method|
|
|
24
48
|
decorate_mutator(method)
|
|
@@ -29,7 +53,7 @@ module LiveCable
|
|
|
29
53
|
define_method(method) do |*pos, **kwargs, &block|
|
|
30
54
|
notify_live_cable_observers
|
|
31
55
|
|
|
32
|
-
__getobj__.method(method).call(*pos, **kwargs, &block)
|
|
56
|
+
__getobj__.method(method).call(*Delegator.unwrap(pos), **Delegator.unwrap(kwargs), &block)
|
|
33
57
|
end
|
|
34
58
|
end
|
|
35
59
|
end
|
|
@@ -7,6 +7,23 @@ module LiveCable
|
|
|
7
7
|
|
|
8
8
|
decorate_mutator :assign_attributes
|
|
9
9
|
decorate_mutator :update
|
|
10
|
+
|
|
11
|
+
def initialize_clone(other, **)
|
|
12
|
+
super
|
|
13
|
+
@live_cable_observers = other.live_cable_observers
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Setters, toggle!, reload and the like reach the record through here
|
|
17
|
+
def method_missing(...) # rubocop:disable Style/MissingRespondToMissing -- SimpleDelegator defines it
|
|
18
|
+
record = __getobj__
|
|
19
|
+
writes = record.live_cable_writes
|
|
20
|
+
|
|
21
|
+
begin
|
|
22
|
+
record.live_cable_forwarding { super }
|
|
23
|
+
ensure
|
|
24
|
+
notify_live_cable_observers unless record.live_cable_writes == writes
|
|
25
|
+
end
|
|
26
|
+
end
|
|
10
27
|
end
|
|
11
28
|
end
|
|
12
29
|
end
|
data/lib/live_cable/delegator.rb
CHANGED
|
@@ -48,12 +48,14 @@ module LiveCable
|
|
|
48
48
|
def initialize(value)
|
|
49
49
|
super
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
extend_delegation_modules
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# dup doesn't copy the singleton class, so the copy needs its modules again
|
|
55
|
+
def initialize_dup(other)
|
|
56
|
+
super
|
|
57
|
+
|
|
58
|
+
extend_delegation_modules
|
|
57
59
|
end
|
|
58
60
|
|
|
59
61
|
# Factory method to create a Delegator only if the value's type is supported.
|
|
@@ -73,6 +75,43 @@ module LiveCable
|
|
|
73
75
|
value
|
|
74
76
|
end
|
|
75
77
|
|
|
78
|
+
# Replaces every Delegator in value, value included, with the object it
|
|
79
|
+
# wraps, copying an Array or Hash only when something inside it changes.
|
|
80
|
+
#
|
|
81
|
+
# @param value [Object]
|
|
82
|
+
# @yieldparam delegator [Delegator] each Delegator it replaces
|
|
83
|
+
# @return [Object]
|
|
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
|
|
92
|
+
return unwrapped[value] if unwrapped.key?(value)
|
|
93
|
+
|
|
94
|
+
unwrapped[value] = value
|
|
95
|
+
copy = nil
|
|
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
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
unwrapped[value] = copy || value
|
|
113
|
+
end
|
|
114
|
+
|
|
76
115
|
# Check if a value's type can be wrapped in a Delegator.
|
|
77
116
|
#
|
|
78
117
|
# @param value [Object] The value to check
|
|
@@ -81,8 +120,27 @@ module LiveCable
|
|
|
81
120
|
Delegation::SUPPORTED.keys.any? { |c| value.is_a?(c) }
|
|
82
121
|
end
|
|
83
122
|
|
|
123
|
+
# SimpleDelegator compares its target with `other` as given, and
|
|
124
|
+
# ActiveRecord's == rejects anything that isn't an instance of the model.
|
|
125
|
+
def ==(other)
|
|
126
|
+
equal?(other) || __getobj__ == (other.is_a?(Delegator) ? other.__getobj__ : other)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def eql?(other)
|
|
130
|
+
equal?(other) || __getobj__.eql?(other.is_a?(Delegator) ? other.__getobj__ : other)
|
|
131
|
+
end
|
|
132
|
+
|
|
84
133
|
private
|
|
85
134
|
|
|
135
|
+
# Extend with the appropriate delegation module based on the value's type
|
|
136
|
+
def extend_delegation_modules
|
|
137
|
+
Delegation::SUPPORTED.each do |klass, delegator|
|
|
138
|
+
if __getobj__.is_a?(klass)
|
|
139
|
+
extend delegator
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
86
144
|
# Create a new Delegator for nested values (e.g., nested arrays/hashes).
|
|
87
145
|
# Propagates all observers from the parent delegator to the child.
|
|
88
146
|
#
|
|
@@ -94,14 +152,12 @@ module LiveCable
|
|
|
94
152
|
# inner = outer[0] # Returns a Delegator wrapping ['inner']
|
|
95
153
|
# inner << 'new' # Notifies same observers as outer
|
|
96
154
|
def create_delegator(value)
|
|
155
|
+
# A Delegator stored inside raw data carries some other variable's observers
|
|
156
|
+
value = value.__getobj__ while value.is_a?(Delegator)
|
|
157
|
+
|
|
97
158
|
return value unless self.class.supported?(value)
|
|
98
159
|
|
|
99
|
-
|
|
100
|
-
self.class.new(value).tap do |delegator|
|
|
101
|
-
live_cable_observers.each do |variable, observers|
|
|
102
|
-
observers.each { |observer| delegator.add_live_cable_observer(observer, variable) }
|
|
103
|
-
end
|
|
104
|
-
end
|
|
160
|
+
self.class.new(value).tap { |delegator| share_live_cable_observers_with(delegator) }
|
|
105
161
|
end
|
|
106
162
|
end
|
|
107
163
|
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LiveCable
|
|
4
|
+
# A client message on its way to a component: an action call or a
|
|
5
|
+
# live-reactive write. Exposed as Component#current_dispatch while the
|
|
6
|
+
# component's before_dispatch callbacks, and the action or write, run.
|
|
7
|
+
class Dispatch
|
|
8
|
+
# @return [Symbol] :action or :reactive
|
|
9
|
+
attr_reader :kind
|
|
10
|
+
|
|
11
|
+
# @return [Symbol] The action or reactive variable name
|
|
12
|
+
attr_reader :name
|
|
13
|
+
|
|
14
|
+
# @return [Object] The value a reactive write assigns; nil for an action
|
|
15
|
+
attr_reader :value
|
|
16
|
+
|
|
17
|
+
# @param data [Hash] One message from the client's batch
|
|
18
|
+
# @return [LiveCable::Dispatch]
|
|
19
|
+
def self.from_message(data)
|
|
20
|
+
if data['_action'].to_s == '_reactive'
|
|
21
|
+
new(:reactive, data['name'], value: data['value'])
|
|
22
|
+
else
|
|
23
|
+
new(:action, data['_action'], query: data['params'])
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def initialize(kind, name, value: nil, query: nil)
|
|
28
|
+
@kind = kind
|
|
29
|
+
@name = name.to_s.to_sym
|
|
30
|
+
@value = value
|
|
31
|
+
@query = query
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def action?
|
|
35
|
+
kind == :action
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def reactive?
|
|
39
|
+
kind == :reactive
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @return [ActionController::Parameters, nil] The action's params; nil for
|
|
43
|
+
# a reactive write
|
|
44
|
+
def params
|
|
45
|
+
return unless action?
|
|
46
|
+
|
|
47
|
+
@params ||= ActionController::Parameters.new(
|
|
48
|
+
ActionDispatch::ParamBuilder.from_pairs(
|
|
49
|
+
ActionDispatch::QueryParser.each_pair(@query || '')
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/live_cable/engine.rb
CHANGED
|
@@ -9,7 +9,7 @@ module LiveCable
|
|
|
9
9
|
if live_component_dir.directory?
|
|
10
10
|
Rails.autoloaders.main.push_dir(live_component_dir, namespace: Live)
|
|
11
11
|
else
|
|
12
|
-
|
|
12
|
+
LiveCable.warn_once("[LiveCable] #{live_component_dir} does not exist; no components will be autoloaded.")
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
# Add LiveCable to importmap (skip when using jsbundling/npm)
|
|
@@ -35,6 +35,13 @@ module LiveCable
|
|
|
35
35
|
initializer 'live_cable.active_record' do
|
|
36
36
|
ActiveSupport.on_load :active_record do
|
|
37
37
|
include ModelObserver
|
|
38
|
+
|
|
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)
|
|
38
45
|
end
|
|
39
46
|
end
|
|
40
47
|
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LiveCable
|
|
4
|
+
# Digests the values that identify who a page was rendered for, so the
|
|
5
|
+
# client can tell when they change between pages without learning them.
|
|
6
|
+
#
|
|
7
|
+
# Keyed with a key derived from secret_key_base: a plain hash of a user id
|
|
8
|
+
# could be matched by hashing guessed ids.
|
|
9
|
+
module IdentityDigest
|
|
10
|
+
PURPOSE = 'live_cable/identity'
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
# @param values [Array<Object>] Records are identified by their GlobalID,
|
|
15
|
+
# as ActionCable identifies a connection; anything else by its to_s
|
|
16
|
+
# @return [String] A hex digest
|
|
17
|
+
def digest(*values)
|
|
18
|
+
payload = values.map { |value| value.respond_to?(:to_gid_param) ? value.to_gid_param : value.to_s }
|
|
19
|
+
|
|
20
|
+
OpenSSL::HMAC.hexdigest('SHA256', key, JSON.generate(payload))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def key
|
|
24
|
+
Rails.application.key_generator.generate_key(PURPOSE)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -4,10 +4,92 @@ module LiveCable
|
|
|
4
4
|
module ModelObserver
|
|
5
5
|
include ObserverTracking
|
|
6
6
|
|
|
7
|
+
def ==(other)
|
|
8
|
+
super(other.is_a?(Delegator) ? other.__getobj__ : other)
|
|
9
|
+
end
|
|
10
|
+
alias eql? ==
|
|
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
|
+
|
|
7
23
|
def _write_attribute(...)
|
|
8
24
|
notify_live_cable_observers
|
|
9
25
|
|
|
10
26
|
super
|
|
11
27
|
end
|
|
28
|
+
|
|
29
|
+
def write_attribute(...)
|
|
30
|
+
notify_live_cable_observers
|
|
31
|
+
|
|
32
|
+
super
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def update_columns(...)
|
|
36
|
+
super.tap { notify_live_cable_observers }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Unchanged reloads stay quiet, so reloading to check a record before writing
|
|
40
|
+
# doesn't re-render
|
|
41
|
+
def reload(...)
|
|
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) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Catches in-place changes to serialized and JSON attributes once saved
|
|
50
|
+
def changes_applied(...)
|
|
51
|
+
super.tap { notify_live_cable_observers if saved_changes? }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Lets a Delegator around this record see that a call it forwarded wrote to it
|
|
55
|
+
def live_cable_writes
|
|
56
|
+
@live_cable_writes || 0
|
|
57
|
+
end
|
|
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
|
+
|
|
68
|
+
private
|
|
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
|
+
|
|
89
|
+
def notify_live_cable_observers
|
|
90
|
+
@live_cable_writes = live_cable_writes + 1
|
|
91
|
+
|
|
92
|
+
super
|
|
93
|
+
end
|
|
12
94
|
end
|
|
13
95
|
end
|
|
@@ -51,17 +51,33 @@ module LiveCable
|
|
|
51
51
|
observers.delete(observer)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
# @api private
|
|
55
|
+
# @param other [ObserverTracking] starts notifying this object's observers too
|
|
56
|
+
# @return [void]
|
|
57
|
+
def share_live_cable_observers_with(other)
|
|
58
|
+
live_cable_observers.each do |variable, observers|
|
|
59
|
+
observers.each { |observer| other.add_live_cable_observer(observer, variable) }
|
|
60
|
+
end
|
|
61
|
+
end
|
|
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
|
+
|
|
54
70
|
def initialize_dup(other)
|
|
55
71
|
super
|
|
56
72
|
@live_cable_observers = {}
|
|
57
73
|
end
|
|
58
74
|
|
|
59
|
-
def initialize_clone(other)
|
|
75
|
+
def initialize_clone(other, **)
|
|
60
76
|
super
|
|
61
77
|
@live_cable_observers = {}
|
|
62
78
|
end
|
|
63
79
|
|
|
64
|
-
|
|
80
|
+
protected
|
|
65
81
|
|
|
66
82
|
# Get the hash of all observers, keyed by variable name.
|
|
67
83
|
#
|
|
@@ -70,6 +86,8 @@ module LiveCable
|
|
|
70
86
|
@live_cable_observers ||= {}
|
|
71
87
|
end
|
|
72
88
|
|
|
89
|
+
private
|
|
90
|
+
|
|
73
91
|
# Get the list of observers for a specific variable.
|
|
74
92
|
#
|
|
75
93
|
# @param variable [Symbol] The variable name
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LiveCable
|
|
4
|
+
# Lets where(column: value) see the Array or Hash inside a reactive value
|
|
5
|
+
# instead of treating the wrapper as a single scalar.
|
|
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
|
+
|
|
18
|
+
def initialize(...)
|
|
19
|
+
super
|
|
20
|
+
|
|
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), &)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
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
|