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
|
@@ -7,37 +7,41 @@ module LiveCable
|
|
|
7
7
|
# @return [LiveCable::Component]
|
|
8
8
|
attr_reader :component
|
|
9
9
|
|
|
10
|
-
def initialize(component, parts, view_context)
|
|
10
|
+
def initialize(component, parts, view_context, virtual_path = nil)
|
|
11
11
|
@component = component
|
|
12
12
|
@parts = parts
|
|
13
13
|
@view_context = view_context
|
|
14
|
+
@virtual_path = virtual_path
|
|
15
|
+
bind_shadowed_names
|
|
14
16
|
end
|
|
15
17
|
|
|
16
18
|
def render_changes(changes = :all)
|
|
17
19
|
# No changes, different from :all (:all means render all)
|
|
18
20
|
# Return an array of nils
|
|
19
21
|
if changes == []
|
|
20
|
-
return Array.new(parts.size, nil)
|
|
22
|
+
return Array.new(@parts.size, nil)
|
|
21
23
|
end
|
|
22
24
|
|
|
23
25
|
@locals = {}
|
|
24
26
|
@dirty_locals = Set.new # Track which locals were recomputed this render
|
|
25
27
|
@method_deps_cache = {} # Cache method dependency expansion for this render cycle
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
@view_context.with_live_cable_virtual_path(@virtual_path) do
|
|
29
|
+
@parts.each_with_index.with_object([]) do |item, acc|
|
|
30
|
+
part, index = item
|
|
31
|
+
result = @view_context.render_part(index) { __send__("render_part_#{index}", changes) }
|
|
32
|
+
acc << result unless part[0] == :code
|
|
33
|
+
end
|
|
30
34
|
end
|
|
31
35
|
end
|
|
32
36
|
|
|
33
37
|
# Pass through renders to the view context, otherwise method_missing will pass
|
|
34
38
|
# to the component instead since it also has a render method
|
|
35
|
-
def render(
|
|
36
|
-
view_context.render(
|
|
39
|
+
def render(...)
|
|
40
|
+
@view_context.render(...)
|
|
37
41
|
end
|
|
38
42
|
|
|
39
43
|
def mark_locals_dirty(locals)
|
|
40
|
-
locals.each { |local| dirty_locals << local }
|
|
44
|
+
locals.each { |local| @dirty_locals << local }
|
|
41
45
|
end
|
|
42
46
|
|
|
43
47
|
# Expand component.method_name calls to their transitive dependencies
|
|
@@ -51,7 +55,7 @@ module LiveCable
|
|
|
51
55
|
cache_key = method_names.sort
|
|
52
56
|
|
|
53
57
|
# Return cached result if available
|
|
54
|
-
return method_deps_cache[cache_key] if method_deps_cache.key?(cache_key)
|
|
58
|
+
return @method_deps_cache[cache_key] if @method_deps_cache.key?(cache_key)
|
|
55
59
|
|
|
56
60
|
# Get the analyzer for the component class
|
|
57
61
|
analyzer = component.class.method_dependencies_analyzer
|
|
@@ -64,7 +68,7 @@ module LiveCable
|
|
|
64
68
|
end
|
|
65
69
|
|
|
66
70
|
# Cache and return the result
|
|
67
|
-
method_deps_cache[cache_key] = expanded.uniq
|
|
71
|
+
@method_deps_cache[cache_key] = expanded.uniq
|
|
68
72
|
end
|
|
69
73
|
|
|
70
74
|
# Check if this part should be skipped based on its dependencies
|
|
@@ -80,6 +84,8 @@ module LiveCable
|
|
|
80
84
|
# Always render on template switch (all dynamic parts must render)
|
|
81
85
|
return false if changes == :dynamic
|
|
82
86
|
|
|
87
|
+
return false if component_method_calls.any? && !component.class.method_dependencies_analyzer.analyzable?
|
|
88
|
+
|
|
83
89
|
# Expand component.method_name calls to their transitive dependencies
|
|
84
90
|
expanded_deps = expand_component_method_dependencies(component_method_calls)
|
|
85
91
|
all_component_deps = component_dependencies | expanded_deps
|
|
@@ -88,57 +94,68 @@ module LiveCable
|
|
|
88
94
|
return false if changes.intersect?(all_component_deps)
|
|
89
95
|
|
|
90
96
|
# Render if any local dependencies are dirty
|
|
91
|
-
return false if dirty_locals.intersect?(local_dependencies)
|
|
97
|
+
return false if @dirty_locals.intersect?(local_dependencies)
|
|
92
98
|
|
|
93
99
|
# Otherwise, skip rendering this part
|
|
94
100
|
true
|
|
95
101
|
end
|
|
96
102
|
|
|
97
103
|
def method_missing(method, ...)
|
|
98
|
-
if locals.key?(method)
|
|
99
|
-
return locals[method]
|
|
100
|
-
end
|
|
101
|
-
|
|
102
104
|
if component.respond_to?(method)
|
|
103
|
-
return
|
|
105
|
+
return call_component(method, ...)
|
|
104
106
|
end
|
|
105
107
|
|
|
106
|
-
if view_context.respond_to?(method)
|
|
107
|
-
return view_context.public_send(method, ...)
|
|
108
|
+
if @view_context.respond_to?(method)
|
|
109
|
+
return @view_context.public_send(method, ...)
|
|
108
110
|
end
|
|
109
111
|
|
|
110
112
|
super
|
|
111
113
|
end
|
|
112
114
|
|
|
113
115
|
def respond_to_missing?(method, _include_private = false)
|
|
114
|
-
|
|
116
|
+
component.respond_to?(method) || @view_context.respond_to?(method)
|
|
115
117
|
end
|
|
116
118
|
|
|
117
119
|
private
|
|
118
120
|
|
|
119
|
-
# @return [Array]
|
|
120
|
-
attr_reader :parts
|
|
121
|
-
|
|
122
|
-
# @return [ActionView::Base]
|
|
123
|
-
attr_reader :view_context
|
|
124
|
-
|
|
125
|
-
# @return [Hash]
|
|
126
|
-
attr_reader :locals
|
|
127
|
-
|
|
128
|
-
# @return [Set]
|
|
129
|
-
attr_reader :dirty_locals
|
|
130
|
-
|
|
131
|
-
# @return [Hash]
|
|
132
|
-
attr_reader :method_deps_cache
|
|
133
|
-
|
|
134
121
|
def with_buffer(&)
|
|
135
|
-
@output_buffer =
|
|
122
|
+
@output_buffer = ViewBuffer.new(@view_context)
|
|
136
123
|
|
|
137
|
-
view_context.with_output_buffer(
|
|
124
|
+
@view_context.with_output_buffer(ActionView::OutputBuffer.new, &).to_s
|
|
138
125
|
end
|
|
139
126
|
|
|
140
127
|
def store_local(name, value)
|
|
141
|
-
locals[name] = value
|
|
128
|
+
@locals[name] = value
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def call_component(name, ...)
|
|
132
|
+
@view_context.live_cable_unwrap(component.public_send(name, ...))
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Kernel and Object names never reach method_missing. Bind the ones the component
|
|
136
|
+
# or view context define themselves; a wrapper falling back to Kernel breaks lambda.
|
|
137
|
+
def bind_shadowed_names
|
|
138
|
+
names = self.class.metadata.compact.flat_map { |part| part[:component_method_calls] || [] }.uniq
|
|
139
|
+
names = names.select { |name| Object.method_defined?(name) || Object.private_method_defined?(name) }
|
|
140
|
+
|
|
141
|
+
# The renderer's respond_to? already checks the component and the view context.
|
|
142
|
+
(names - [:respond_to?]).each do |name|
|
|
143
|
+
target = [component, @view_context].find do |object|
|
|
144
|
+
if Object.private_method_defined?(name)
|
|
145
|
+
object.respond_to?(name)
|
|
146
|
+
else
|
|
147
|
+
object.class.public_method_defined?(name) &&
|
|
148
|
+
object.class.instance_method(name).owner != Object.instance_method(name).owner
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
next unless target
|
|
152
|
+
|
|
153
|
+
define_singleton_method(name) do |*args, **kwargs, &block|
|
|
154
|
+
next call_component(name, *args, **kwargs, &block) if target.equal?(component)
|
|
155
|
+
|
|
156
|
+
target.public_send(name, *args, **kwargs, &block)
|
|
157
|
+
end
|
|
158
|
+
end
|
|
142
159
|
end
|
|
143
160
|
end
|
|
144
161
|
end
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'prism'
|
|
4
|
+
|
|
3
5
|
module LiveCable
|
|
4
6
|
module Rendering
|
|
5
7
|
class Renderer < ::Herb::Engine
|
|
6
8
|
# rubocop:disable Lint/MissingSuper
|
|
7
|
-
def initialize
|
|
9
|
+
def initialize(identifier = nil, virtual_path = nil)
|
|
10
|
+
@identifier = identifier
|
|
11
|
+
@virtual_path = virtual_path
|
|
8
12
|
@newline_pending = 0
|
|
9
13
|
@parts = []
|
|
10
14
|
@src = +''
|
|
@@ -20,11 +24,18 @@ module LiveCable
|
|
|
20
24
|
|
|
21
25
|
def src
|
|
22
26
|
metadata = build_metadata
|
|
23
|
-
|
|
27
|
+
args = [parts, metadata, identifier, virtual_path].map(&:inspect).join(', ')
|
|
28
|
+
"::LiveCable::Rendering::Partial.new(#{args})"
|
|
24
29
|
end
|
|
25
30
|
|
|
26
31
|
private
|
|
27
32
|
|
|
33
|
+
# @return [String, nil]
|
|
34
|
+
attr_reader :identifier
|
|
35
|
+
|
|
36
|
+
# @return [String, nil]
|
|
37
|
+
attr_reader :virtual_path
|
|
38
|
+
|
|
28
39
|
# @return [Integer]
|
|
29
40
|
attr_reader :newline_pending
|
|
30
41
|
|
|
@@ -40,25 +51,24 @@ module LiveCable
|
|
|
40
51
|
def build_metadata
|
|
41
52
|
accumulated_locals = [] # Track locals defined in previous parts
|
|
42
53
|
|
|
43
|
-
parts.map do |type, code|
|
|
54
|
+
metadata = parts.map do |type, code|
|
|
44
55
|
next nil if type == :static || code.nil? || code.empty?
|
|
45
56
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
local_check_code = +''
|
|
49
|
-
|
|
50
|
-
locals_defined_here.each do |local|
|
|
51
|
-
local_check_code << "store_local(:#{local}, #{local}) if defined?(#{local})\n"
|
|
52
|
-
end
|
|
57
|
+
# Earlier parts' locals are in scope, as they would be in one ERB method body
|
|
58
|
+
parsed = Prism.parse(code, scopes: [accumulated_locals]).value
|
|
53
59
|
|
|
54
60
|
visitor = DependencyVisitor.new
|
|
55
61
|
visitor.visit(parsed)
|
|
56
62
|
|
|
57
|
-
|
|
58
|
-
|
|
63
|
+
local_check_code = +''
|
|
64
|
+
|
|
65
|
+
visitor.local_writes.each do |local|
|
|
66
|
+
local_check_code << "store_local(:#{local}, #{local}) if defined?(#{local})\n"
|
|
67
|
+
end
|
|
59
68
|
|
|
60
|
-
#
|
|
61
|
-
local_dependencies = visitor.local_reads & accumulated_locals
|
|
69
|
+
# A write that doesn't run, such as one in a false `if`, keeps the earlier value
|
|
70
|
+
local_dependencies = (visitor.local_reads | visitor.local_writes) & accumulated_locals
|
|
71
|
+
exposed_local_reads = visitor.exposed_local_reads & accumulated_locals
|
|
62
72
|
|
|
63
73
|
# Track component.method_name calls separately for runtime expansion
|
|
64
74
|
component_method_calls = visitor.component_method_calls.to_a
|
|
@@ -69,13 +79,23 @@ module LiveCable
|
|
|
69
79
|
{
|
|
70
80
|
type:,
|
|
71
81
|
code:,
|
|
72
|
-
component_dependencies:
|
|
82
|
+
component_dependencies: visitor.variable_calls - [:component],
|
|
73
83
|
component_method_calls:,
|
|
74
84
|
local_dependencies:,
|
|
85
|
+
exposed_local_reads:,
|
|
75
86
|
defines_locals: visitor.local_writes,
|
|
76
87
|
local_check_code:,
|
|
77
88
|
}
|
|
78
89
|
end
|
|
90
|
+
|
|
91
|
+
read_later = []
|
|
92
|
+
metadata.reverse_each do |part|
|
|
93
|
+
next unless part
|
|
94
|
+
|
|
95
|
+
part[:feeds_later_parts] = part[:defines_locals].intersect?(read_later)
|
|
96
|
+
read_later |= part[:exposed_local_reads]
|
|
97
|
+
end
|
|
98
|
+
metadata
|
|
79
99
|
end
|
|
80
100
|
|
|
81
101
|
def finish_method(type)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LiveCable
|
|
4
|
+
module Rendering
|
|
5
|
+
# A template's buffer: stands in for whichever buffer the view is writing to at
|
|
6
|
+
# that moment, so a block lands wherever its caller yields it, as in plain ERB.
|
|
7
|
+
class ViewBuffer
|
|
8
|
+
delegate :safe_append=, :append=, :safe_expr_append=, to: :output_buffer
|
|
9
|
+
delegate_missing_to :output_buffer
|
|
10
|
+
|
|
11
|
+
def initialize(view_context)
|
|
12
|
+
@view_context = view_context
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def output_buffer
|
|
18
|
+
@view_context.output_buffer
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -6,14 +6,16 @@ module LiveCable
|
|
|
6
6
|
# mirroring what the JavaScript client does: the first refresh carries
|
|
7
7
|
# all parts, subsequent refreshes carry only the changed parts (nil
|
|
8
8
|
# means unchanged), and child components arrive as separate results
|
|
9
|
-
# referenced by <LiveCable child-live-id="..."> placeholders.
|
|
9
|
+
# referenced by <LiveCable child-live-id="..."> placeholders. Results for
|
|
10
|
+
# children at every depth arrive in the root's c, so the whole tree
|
|
11
|
+
# shares one set of child states.
|
|
10
12
|
class RenderState
|
|
11
13
|
CHILD_PLACEHOLDER = %r{<LiveCable child-live-id="(?<live_id>[^"]+)"></LiveCable>}
|
|
12
14
|
|
|
13
|
-
def initialize
|
|
15
|
+
def initialize(children = Hash.new { |hash, key| hash[key] = RenderState.new(hash) })
|
|
14
16
|
@parts_by_template = {}
|
|
15
17
|
@last_template = nil
|
|
16
|
-
@children =
|
|
18
|
+
@children = children
|
|
17
19
|
end
|
|
18
20
|
|
|
19
21
|
# @param refresh [Hash] A _refresh payload ({ h:, p:, c: })
|
|
@@ -44,7 +46,7 @@ module LiveCable
|
|
|
44
46
|
return '' unless parts
|
|
45
47
|
|
|
46
48
|
parts.join.gsub(CHILD_PLACEHOLDER) do
|
|
47
|
-
@children[Regexp.last_match[:live_id]].html
|
|
49
|
+
@children[CGI.unescapeHTML(Regexp.last_match[:live_id])].html
|
|
48
50
|
end
|
|
49
51
|
end
|
|
50
52
|
end
|
|
@@ -12,17 +12,23 @@ module LiveCable
|
|
|
12
12
|
# @return [Array<Hash>] Payloads sent by components through this channel
|
|
13
13
|
attr_reader :transmissions
|
|
14
14
|
|
|
15
|
+
# @return [Array<Hash>] Every _refresh payload sent, including any
|
|
16
|
+
# since cleared from transmissions
|
|
17
|
+
attr_reader :refreshes
|
|
18
|
+
|
|
15
19
|
# @return [LiveCable::Testing::TestCableConnection]
|
|
16
20
|
attr_reader :connection
|
|
17
21
|
|
|
18
22
|
def initialize(identifiers = {})
|
|
19
23
|
@streams = {}
|
|
20
24
|
@transmissions = []
|
|
25
|
+
@refreshes = []
|
|
21
26
|
@connection = TestCableConnection.new(identifiers)
|
|
22
27
|
end
|
|
23
28
|
|
|
24
29
|
def broadcast(data)
|
|
25
30
|
@transmissions << data
|
|
31
|
+
@refreshes << data[:_refresh] if data.key?(:_refresh)
|
|
26
32
|
end
|
|
27
33
|
|
|
28
34
|
def stream_from(name, coder: nil, &block)
|
|
@@ -77,14 +77,14 @@ module LiveCable
|
|
|
77
77
|
end
|
|
78
78
|
|
|
79
79
|
# Forget previously captured broadcasts. Useful after mounting, to
|
|
80
|
-
# assert on the effects of a single action.
|
|
80
|
+
# assert on the effects of a single action. Doesn't affect rendered.
|
|
81
81
|
def clear_broadcasts
|
|
82
82
|
channel.transmissions.clear
|
|
83
83
|
end
|
|
84
84
|
|
|
85
85
|
# All events the component has dispatched via dispatch_event, in
|
|
86
|
-
# order, whether they rode along with a render or
|
|
87
|
-
# their own.
|
|
86
|
+
# order, whether they rode along with a render or an _ack, or were
|
|
87
|
+
# broadcast on their own.
|
|
88
88
|
#
|
|
89
89
|
# @return [Array<Hash>] Event hashes ({ name:, detail:, window: })
|
|
90
90
|
def dispatched_events
|
|
@@ -98,8 +98,8 @@ module LiveCable
|
|
|
98
98
|
def rendered_html
|
|
99
99
|
state = RenderState.new
|
|
100
100
|
|
|
101
|
-
|
|
102
|
-
state.apply(
|
|
101
|
+
channel.refreshes.each do |refresh|
|
|
102
|
+
state.apply(refresh)
|
|
103
103
|
end
|
|
104
104
|
|
|
105
105
|
state.html
|
data/lib/live_cable/testing.rb
CHANGED
|
@@ -38,8 +38,10 @@ module LiveCable
|
|
|
38
38
|
# between components
|
|
39
39
|
# @param identifiers [Hash] ActionCable connection identifiers made
|
|
40
40
|
# available to the component (e.g. current_user: user)
|
|
41
|
-
# @param raise_errors [Boolean] Raise errors from
|
|
42
|
-
#
|
|
41
|
+
# @param raise_errors [Boolean] Raise errors from mounting (connect
|
|
42
|
+
# callbacks and the first render), actions, rendering and stream
|
|
43
|
+
# callbacks instead of broadcasting an _error like production does
|
|
44
|
+
# (default true)
|
|
43
45
|
# @param defaults [Hash] Default values for reactive variables
|
|
44
46
|
# @return [LiveCable::Testing::TestComponent]
|
|
45
47
|
def live_mount(component, id: 'test', connection: nil, identifiers: {}, raise_errors: true, **defaults)
|
|
@@ -54,11 +56,15 @@ module LiveCable
|
|
|
54
56
|
|
|
55
57
|
test_component = TestComponent.new(instance, connection, TestChannel.new(identifiers))
|
|
56
58
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
begin
|
|
60
|
+
connection.add_component(instance)
|
|
61
|
+
instance.defaults = defaults
|
|
62
|
+
instance.apply_defaults
|
|
63
|
+
instance.connect(test_component.channel)
|
|
64
|
+
instance.broadcast_render(subscribed: true)
|
|
65
|
+
rescue StandardError => error
|
|
66
|
+
connection.handle_error(instance, error, channel: test_component.channel)
|
|
67
|
+
end
|
|
62
68
|
|
|
63
69
|
test_component
|
|
64
70
|
end
|
data/lib/live_cable/version.rb
CHANGED
data/lib/live_cable.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'logger'
|
|
3
4
|
require 'zeitwerk'
|
|
4
5
|
require 'herb'
|
|
5
6
|
|
|
@@ -19,18 +20,46 @@ require_relative '../app/helpers/live_cable_helper'
|
|
|
19
20
|
require_relative 'live'
|
|
20
21
|
|
|
21
22
|
module LiveCable
|
|
23
|
+
@warn_mutex = Mutex.new
|
|
24
|
+
@warned_messages = Set.new
|
|
25
|
+
|
|
26
|
+
# Log a warning through the Rails logger at most once per process for a
|
|
27
|
+
# given message. Used for performance and configuration hints (e.g. a
|
|
28
|
+
# component rendered without a .live.erb template) that would otherwise
|
|
29
|
+
# repeat on every render and flood the log.
|
|
30
|
+
def self.warn_once(message)
|
|
31
|
+
@warn_mutex.synchronize do
|
|
32
|
+
return unless @warned_messages.add?(message)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
logger.warn(message)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @return [Logger]
|
|
39
|
+
def self.logger
|
|
40
|
+
if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
|
41
|
+
Rails.logger
|
|
42
|
+
else
|
|
43
|
+
@fallback_logger ||= Logger.new($stderr)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
22
47
|
def self.instance_from_string(string, id)
|
|
23
48
|
klass = Live
|
|
24
49
|
klass_string = string.camelize
|
|
25
50
|
|
|
26
51
|
begin
|
|
27
52
|
klass_string.split('::').each do |part|
|
|
28
|
-
|
|
53
|
+
# inherit: false so component names that collide with a top-level
|
|
54
|
+
# constant (e.g. "string" -> ::String) don't resolve through Object's
|
|
55
|
+
# ancestry and slip past this guard, only to blow up later with a
|
|
56
|
+
# confusing NameError instead of this friendly message.
|
|
57
|
+
unless klass.const_defined?(part, false)
|
|
29
58
|
raise LiveCable::Error,
|
|
30
59
|
"Component Live::#{klass_string} not found, make sure it is located in the Live:: module"
|
|
31
60
|
end
|
|
32
61
|
|
|
33
|
-
klass = klass.const_get(part)
|
|
62
|
+
klass = klass.const_get(part, false)
|
|
34
63
|
end
|
|
35
64
|
rescue NameError
|
|
36
65
|
raise LiveCable::Error, "Invalid component name \"#{string}\" - Live::#{klass_string} not found"
|
|
@@ -38,7 +67,7 @@ module LiveCable
|
|
|
38
67
|
|
|
39
68
|
klass = "Live::#{klass_string}".safe_constantize
|
|
40
69
|
|
|
41
|
-
unless klass < LiveCable::Component
|
|
70
|
+
unless klass && klass < LiveCable::Component
|
|
42
71
|
raise LiveCable::Error, 'Components must extend LiveCable::Component'
|
|
43
72
|
end
|
|
44
73
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: live_cable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Craig Blanchette
|
|
@@ -15,56 +15,56 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: '
|
|
18
|
+
version: '8.0'
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: '
|
|
25
|
+
version: '8.0'
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: actionview
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
29
29
|
requirements:
|
|
30
30
|
- - ">="
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '
|
|
32
|
+
version: '8.0'
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: '
|
|
39
|
+
version: '8.0'
|
|
40
40
|
- !ruby/object:Gem::Dependency
|
|
41
41
|
name: activemodel
|
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|
|
43
43
|
requirements:
|
|
44
44
|
- - ">="
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
|
-
version: '
|
|
46
|
+
version: '8.0'
|
|
47
47
|
type: :runtime
|
|
48
48
|
prerelease: false
|
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
50
50
|
requirements:
|
|
51
51
|
- - ">="
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: '
|
|
53
|
+
version: '8.0'
|
|
54
54
|
- !ruby/object:Gem::Dependency
|
|
55
55
|
name: activesupport
|
|
56
56
|
requirement: !ruby/object:Gem::Requirement
|
|
57
57
|
requirements:
|
|
58
58
|
- - ">="
|
|
59
59
|
- !ruby/object:Gem::Version
|
|
60
|
-
version: '
|
|
60
|
+
version: '8.0'
|
|
61
61
|
type: :runtime
|
|
62
62
|
prerelease: false
|
|
63
63
|
version_requirements: !ruby/object:Gem::Requirement
|
|
64
64
|
requirements:
|
|
65
65
|
- - ">="
|
|
66
66
|
- !ruby/object:Gem::Version
|
|
67
|
-
version: '
|
|
67
|
+
version: '8.0'
|
|
68
68
|
- !ruby/object:Gem::Dependency
|
|
69
69
|
name: herb
|
|
70
70
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -134,8 +134,10 @@ files:
|
|
|
134
134
|
- lib/live.rb
|
|
135
135
|
- lib/live_cable.rb
|
|
136
136
|
- lib/live_cable/action_cable_connection.rb
|
|
137
|
+
- lib/live_cable/arel_predications.rb
|
|
137
138
|
- lib/live_cable/component.rb
|
|
138
139
|
- lib/live_cable/component/broadcasting.rb
|
|
140
|
+
- lib/live_cable/component/dispatching.rb
|
|
139
141
|
- lib/live_cable/component/events.rb
|
|
140
142
|
- lib/live_cable/component/identification.rb
|
|
141
143
|
- lib/live_cable/component/lifecycle.rb
|
|
@@ -151,18 +153,25 @@ files:
|
|
|
151
153
|
- lib/live_cable/connection/messaging.rb
|
|
152
154
|
- lib/live_cable/connection/state_management.rb
|
|
153
155
|
- lib/live_cable/container.rb
|
|
156
|
+
- lib/live_cable/defaults_signer.rb
|
|
154
157
|
- lib/live_cable/delegation/array.rb
|
|
155
158
|
- lib/live_cable/delegation/hash.rb
|
|
156
159
|
- lib/live_cable/delegation/methods.rb
|
|
157
160
|
- lib/live_cable/delegation/model.rb
|
|
158
161
|
- lib/live_cable/delegator.rb
|
|
162
|
+
- lib/live_cable/dispatch.rb
|
|
159
163
|
- lib/live_cable/engine.rb
|
|
160
164
|
- lib/live_cable/error.rb
|
|
165
|
+
- lib/live_cable/forbidden.rb
|
|
166
|
+
- lib/live_cable/identity_digest.rb
|
|
161
167
|
- lib/live_cable/model_observer.rb
|
|
162
168
|
- lib/live_cable/observer.rb
|
|
163
169
|
- lib/live_cable/observer_tracking.rb
|
|
170
|
+
- lib/live_cable/predicate_builder.rb
|
|
171
|
+
- lib/live_cable/record_ids.rb
|
|
164
172
|
- lib/live_cable/render_context.rb
|
|
165
173
|
- lib/live_cable/rendering/compiler.rb
|
|
174
|
+
- lib/live_cable/rendering/controller_renderer.rb
|
|
166
175
|
- lib/live_cable/rendering/dependency_visitor.rb
|
|
167
176
|
- lib/live_cable/rendering/handler.rb
|
|
168
177
|
- lib/live_cable/rendering/method_analyzer.rb
|
|
@@ -172,6 +181,7 @@ files:
|
|
|
172
181
|
- lib/live_cable/rendering/partial_renderer.rb
|
|
173
182
|
- lib/live_cable/rendering/render_result.rb
|
|
174
183
|
- lib/live_cable/rendering/renderer.rb
|
|
184
|
+
- lib/live_cable/rendering/view_buffer.rb
|
|
175
185
|
- lib/live_cable/testing.rb
|
|
176
186
|
- lib/live_cable/testing/render_state.rb
|
|
177
187
|
- lib/live_cable/testing/test_cable_connection.rb
|