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
|
@@ -6,48 +6,70 @@ class LiveChannel < ActionCable::Channel::Base
|
|
|
6
6
|
|
|
7
7
|
before_subscribe :ensure_live_connection
|
|
8
8
|
|
|
9
|
+
# Each entry point below runs as its own job on ActionCable's worker pool,
|
|
10
|
+
# so each holds the connection's lock for the whole of its work - see
|
|
11
|
+
# LiveCable::Connection#synchronize.
|
|
12
|
+
|
|
9
13
|
# @component is assigned as soon as the component is known, so a failure
|
|
10
14
|
# later in here still leaves it for unsubscribed to clean up
|
|
11
15
|
def subscribed
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
16
|
+
live_connection.synchronize do
|
|
17
|
+
live_id = "#{params[:component]}/#{params[:id]}"
|
|
18
|
+
|
|
19
|
+
@component = live_connection.get_component(live_id)
|
|
20
|
+
|
|
21
|
+
# The client replaces a subscription, to send new defaults, by
|
|
22
|
+
# unsubscribing first, but ActionCable may run the two out of order
|
|
23
|
+
if component&.subscribed?
|
|
24
|
+
component.disconnect
|
|
25
|
+
@component = nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
rendered = component.present?
|
|
29
|
+
|
|
30
|
+
unless component
|
|
31
|
+
@component = LiveCable.instance_from_string(params[:component], params[:id])
|
|
32
|
+
live_connection.add_component(component)
|
|
33
|
+
# Defaults round-trip through the client, so verify the signed blob and
|
|
34
|
+
# bind it to this live_id before trusting it - otherwise a tampered value
|
|
35
|
+
# could set non-writable reactive variables at subscribe time.
|
|
36
|
+
component.defaults = LiveCable::DefaultsSigner.verify(params[:defaults], live_id)
|
|
37
|
+
component.apply_defaults
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
component.connect(self)
|
|
41
|
+
|
|
42
|
+
if rendered
|
|
43
|
+
component.broadcast_subscribe
|
|
44
|
+
else
|
|
45
|
+
component.broadcast_render(subscribed: true)
|
|
46
|
+
end
|
|
47
|
+
rescue StandardError => error
|
|
48
|
+
live_connection.handle_error(component, error, channel: self)
|
|
30
49
|
end
|
|
31
|
-
rescue StandardError => error
|
|
32
|
-
live_connection.handle_error(component, error, channel: self)
|
|
33
50
|
end
|
|
34
51
|
|
|
35
|
-
# Every batch must be answered - the client holds its loading state until
|
|
36
|
-
#
|
|
37
|
-
# ActionCable, which would only log it and leave the client hanging.
|
|
52
|
+
# Every batch must be answered - the client holds its loading state until
|
|
53
|
+
# its reply, an _ack or an _error arrives - so nothing raised here may escape
|
|
54
|
+
# to ActionCable, which would only log it and leave the client hanging.
|
|
38
55
|
def receive(data)
|
|
39
|
-
|
|
56
|
+
live_connection.synchronize do
|
|
57
|
+
raise LiveCable::Error, 'No component was built, so this subscription cannot receive messages' unless component
|
|
40
58
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
59
|
+
live_connection.receive(component, data)
|
|
60
|
+
rescue StandardError => error
|
|
61
|
+
live_connection.handle_error(component, error, channel: self)
|
|
62
|
+
end
|
|
44
63
|
end
|
|
45
64
|
|
|
46
65
|
def unsubscribed
|
|
47
66
|
return unless component
|
|
48
67
|
|
|
49
|
-
|
|
50
|
-
|
|
68
|
+
live_connection.synchronize do
|
|
69
|
+
# Already disconnected by a newer subscription for the same component
|
|
70
|
+
component.disconnect if live_connection.get_component(component.live_id).equal?(component)
|
|
71
|
+
@component = nil
|
|
72
|
+
end
|
|
51
73
|
end
|
|
52
74
|
|
|
53
75
|
# Exposes #transmit, which ActionCable keeps private to the channel.
|
|
@@ -6,6 +6,9 @@ module LiveCableHelper
|
|
|
6
6
|
# Add the current component to the parent context before making a new context
|
|
7
7
|
render_context&.add_component(component)
|
|
8
8
|
|
|
9
|
+
# A prerendered root is the one component whose defaults are signed into the page
|
|
10
|
+
component.round_trip_defaults if render_context.nil? && !component.live_connection
|
|
11
|
+
|
|
9
12
|
# If we had a parent with a live connection, we're connected, so apply defaults now, if not
|
|
10
13
|
# then we apply them to the pre-render container
|
|
11
14
|
component.apply_defaults
|
|
@@ -14,9 +17,17 @@ module LiveCableHelper
|
|
|
14
17
|
context_stack.push(context)
|
|
15
18
|
|
|
16
19
|
begin
|
|
17
|
-
value =
|
|
20
|
+
value = nil
|
|
21
|
+
|
|
22
|
+
# A connected root runs its render callbacks in broadcast_render
|
|
23
|
+
if component.live_connection && !context.root?
|
|
24
|
+
component.run_callbacks(:render) { value = yield }
|
|
25
|
+
else
|
|
26
|
+
value = yield
|
|
27
|
+
end
|
|
18
28
|
ensure
|
|
19
29
|
context_stack.pop
|
|
30
|
+
context.forget_tracked_values
|
|
20
31
|
end
|
|
21
32
|
|
|
22
33
|
[value, context]
|
|
@@ -29,6 +40,16 @@ module LiveCableHelper
|
|
|
29
40
|
ctx.render_part(index, &)
|
|
30
41
|
end
|
|
31
42
|
|
|
43
|
+
# Relative t('.key') lookups resolve against @virtual_path, which ActionView
|
|
44
|
+
# only sets while the template's compiled method runs, not while its parts do.
|
|
45
|
+
def with_live_cable_virtual_path(virtual_path)
|
|
46
|
+
previous = @virtual_path
|
|
47
|
+
@virtual_path = virtual_path
|
|
48
|
+
yield
|
|
49
|
+
ensure
|
|
50
|
+
@virtual_path = previous
|
|
51
|
+
end
|
|
52
|
+
|
|
32
53
|
def live(component, id:, **defaults)
|
|
33
54
|
unless component.is_a?(String)
|
|
34
55
|
raise LiveCable::Error, '`live` helper only accepts string component names. Use render(component) directly if ' \
|
|
@@ -40,11 +61,33 @@ module LiveCableHelper
|
|
|
40
61
|
live_id = "#{component}/#{id}"
|
|
41
62
|
|
|
42
63
|
component = render_context&.get_component(live_id) || LiveCable.instance_from_string(component, id)
|
|
43
|
-
|
|
64
|
+
track = render_context && !component.defaults_applied
|
|
65
|
+
component.defaults = track ? defaults.transform_values { |value| render_context.track(value) } : defaults
|
|
44
66
|
|
|
45
67
|
render(component)
|
|
46
68
|
end
|
|
47
69
|
|
|
70
|
+
# A meta tag carrying a digest of who this page was rendered for. When a
|
|
71
|
+
# Turbo visit brings a different one - after a sign-in, sign-out or
|
|
72
|
+
# impersonation - the client reopens its socket, so the new handshake
|
|
73
|
+
# identifies the new session. Pass what your connection's connect
|
|
74
|
+
# identifies by; the values are never sent in the clear.
|
|
75
|
+
#
|
|
76
|
+
# @param values [Array<Object>] e.g. current_user, or nil when signed out
|
|
77
|
+
def live_cable_identity_tag(*values)
|
|
78
|
+
tag.meta(name: 'live-cable-identity', content: LiveCable::IdentityDigest.digest(*values))
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Templates get the plain value inside a reactive one, so Rails helpers and
|
|
82
|
+
# `case` see a real Array or Hash.
|
|
83
|
+
def live_cable_unwrap(value)
|
|
84
|
+
case value
|
|
85
|
+
when LiveCable::Delegator then render_context.unwrap(value)
|
|
86
|
+
when Array, Hash then render_context.live_connection ? render_context.unwrap_nested(value) : value
|
|
87
|
+
else value
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
48
91
|
private
|
|
49
92
|
|
|
50
93
|
# @return [LiveCable::RenderContext, nil]
|
|
@@ -11,9 +11,12 @@ module LiveCable
|
|
|
11
11
|
# there. Keeping LiveCable off the identifiers leaves that mechanism working
|
|
12
12
|
# for the application's own, such as current_user.
|
|
13
13
|
module ActionCableConnection
|
|
14
|
+
LOCK = Mutex.new
|
|
15
|
+
|
|
14
16
|
# @return [LiveCable::Connection]
|
|
15
17
|
def live_connection
|
|
16
|
-
|
|
18
|
+
# A socket's subscribes run as concurrent jobs on ActionCable's worker pool
|
|
19
|
+
@live_connection || LOCK.synchronize { @live_connection ||= LiveCable::Connection.new(request) }
|
|
17
20
|
end
|
|
18
21
|
end
|
|
19
22
|
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LiveCable
|
|
4
|
+
# Arel's in and not_in, which in_order_of also uses, only see an Array as a
|
|
5
|
+
# list of values when it is Enumerable, and a reactive one isn't.
|
|
6
|
+
module ArelPredications
|
|
7
|
+
def in(other)
|
|
8
|
+
super(Delegator.unwrap(other))
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def not_in(other)
|
|
12
|
+
super(Delegator.unwrap(other))
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -12,13 +12,33 @@ module LiveCable
|
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def broadcast_subscribe
|
|
15
|
-
broadcast({ _status: 'subscribed', id: live_id })
|
|
15
|
+
broadcast({ _status: 'subscribed', id: live_id, _subscribed: true })
|
|
16
|
+
|
|
17
|
+
# Deliver any events queued before this component had a channel of its
|
|
18
|
+
# own (e.g. dispatched while it was rendered inline by a parent). The
|
|
19
|
+
# broadcast_render path flushes events itself, so this only matters for
|
|
20
|
+
# an already-rendered component that subscribes without re-rendering.
|
|
21
|
+
broadcast_events
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Sends any queued events on their own, for when there's no render for
|
|
25
|
+
# them to ride along with.
|
|
26
|
+
def broadcast_events
|
|
27
|
+
events = flush_events
|
|
28
|
+
broadcast(_events: events) if events.any?
|
|
16
29
|
end
|
|
17
30
|
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
|
|
21
|
-
|
|
31
|
+
# Answers a message batch when the component's own render didn't -
|
|
32
|
+
# nothing changed, or its render went out inside its parent's. Events
|
|
33
|
+
# the batch queued ride along, so they fire once the loading state ends.
|
|
34
|
+
#
|
|
35
|
+
# @param rendered [Boolean] whether a parent's frame carried its render
|
|
36
|
+
def broadcast_ack(rendered: false)
|
|
37
|
+
data = { _ack: true }
|
|
38
|
+
data[:_rendered] = true if rendered
|
|
39
|
+
events = flush_events
|
|
40
|
+
data[:_events] = events if events.any?
|
|
41
|
+
broadcast(data)
|
|
22
42
|
end
|
|
23
43
|
|
|
24
44
|
def broadcast_destroy
|
|
@@ -26,17 +46,36 @@ module LiveCable
|
|
|
26
46
|
@subscribed = false
|
|
27
47
|
end
|
|
28
48
|
|
|
29
|
-
|
|
49
|
+
# @param subscribed [Boolean] whether this is the render sent when the
|
|
50
|
+
# component subscribes
|
|
51
|
+
# @return [Boolean] false when the render changed nothing on the page, so
|
|
52
|
+
# no _refresh went out
|
|
53
|
+
def broadcast_render(subscribed: false)
|
|
54
|
+
sent = false
|
|
55
|
+
|
|
30
56
|
run_callbacks :render do
|
|
31
|
-
|
|
57
|
+
result = render
|
|
58
|
+
|
|
59
|
+
# Leaves the reply unclaimed, so the message is answered with an _ack
|
|
60
|
+
if !subscribed && result.is_a?(LiveCable::Rendering::RenderResult) && result.blank?
|
|
61
|
+
broadcast_events unless live_connection&.awaiting_reply?(self)
|
|
62
|
+
next
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
events = flush_events
|
|
66
|
+
data = { _refresh: result.as_json, _reply: live_connection&.take_reply(self) }
|
|
67
|
+
data[:_subscribed] = true if subscribed
|
|
68
|
+
live_connection&.carried(data[:_refresh][:c])
|
|
32
69
|
|
|
33
70
|
# Events ride along with the render so the client can fire them
|
|
34
71
|
# after the DOM has been morphed
|
|
35
|
-
events = flush_events
|
|
36
72
|
data[:_events] = events if events.any?
|
|
37
73
|
|
|
38
74
|
broadcast(data)
|
|
75
|
+
sent = true
|
|
39
76
|
end
|
|
77
|
+
|
|
78
|
+
sent
|
|
40
79
|
end
|
|
41
80
|
end
|
|
42
81
|
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LiveCable
|
|
4
|
+
class Component
|
|
5
|
+
module Dispatching
|
|
6
|
+
extend ActiveSupport::Concern
|
|
7
|
+
|
|
8
|
+
included do
|
|
9
|
+
define_model_callbacks :dispatch, only: :before
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# @return [LiveCable::Dispatch, nil] The client message being dispatched
|
|
13
|
+
attr_reader :current_dispatch
|
|
14
|
+
|
|
15
|
+
# Run the before_dispatch callbacks, then the action or reactive write
|
|
16
|
+
# the message asks for, unless a callback threw :abort.
|
|
17
|
+
#
|
|
18
|
+
# @param dispatch [LiveCable::Dispatch] Already checked against the
|
|
19
|
+
# component's actions and writable variables
|
|
20
|
+
def perform_dispatch(dispatch)
|
|
21
|
+
@current_dispatch = dispatch
|
|
22
|
+
|
|
23
|
+
run_callbacks :dispatch do
|
|
24
|
+
if dispatch.reactive?
|
|
25
|
+
public_send("#{dispatch.name}=", dispatch.value)
|
|
26
|
+
else
|
|
27
|
+
# A reactive variable named method replaces Kernel#method
|
|
28
|
+
action = Kernel.instance_method(:method).bind_call(self, dispatch.name)
|
|
29
|
+
action.arity.positive? ? action.call(dispatch.params) : action.call
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
ensure
|
|
33
|
+
@current_dispatch = nil
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -7,7 +7,8 @@ module LiveCable
|
|
|
7
7
|
|
|
8
8
|
# Queue a DOM event to be dispatched on the client. Events are
|
|
9
9
|
# delivered with the next broadcast for this component - attached to
|
|
10
|
-
# the render when state changed,
|
|
10
|
+
# the render when state changed, to the _ack answering its message, or
|
|
11
|
+
# on their own - and
|
|
11
12
|
# fire on the client after the DOM has been morphed, so handlers see
|
|
12
13
|
# the updated markup.
|
|
13
14
|
#
|
|
@@ -5,6 +5,10 @@ module LiveCable
|
|
|
5
5
|
module ReactiveVariables
|
|
6
6
|
extend ActiveSupport::Concern
|
|
7
7
|
|
|
8
|
+
# A client may write a shared name only if every class sharing it declares
|
|
9
|
+
# it writable. Keyed by class name so a reload replaces the entry.
|
|
10
|
+
SHARED_DECLARATIONS = Concurrent::Map.new
|
|
11
|
+
|
|
8
12
|
included do
|
|
9
13
|
class_attribute :shared_variables, default: []
|
|
10
14
|
class_attribute :reactive_variables, default: []
|
|
@@ -29,12 +33,22 @@ module LiveCable
|
|
|
29
33
|
end
|
|
30
34
|
|
|
31
35
|
create_reactive_variables(variable, initial_value, shared: shared)
|
|
36
|
+
declare_shared(variable, writable:) if shared
|
|
32
37
|
end
|
|
33
38
|
|
|
34
39
|
def shared(variable, initial_value = nil)
|
|
35
40
|
self.shared_variables = (shared_variables || []).dup << variable
|
|
36
41
|
|
|
37
42
|
create_reactive_variables(variable, initial_value, shared: true)
|
|
43
|
+
declare_shared(variable, writable: false)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @return [Boolean] whether a client message may set the variable
|
|
47
|
+
def client_writable?(variable)
|
|
48
|
+
return false unless writable_reactive_variables.include?(variable)
|
|
49
|
+
return true unless shared_reactive_variables.include?(variable)
|
|
50
|
+
|
|
51
|
+
SHARED_DECLARATIONS.fetch(variable, {}).values.all?
|
|
38
52
|
end
|
|
39
53
|
|
|
40
54
|
def actions(*names)
|
|
@@ -48,7 +62,15 @@ module LiveCable
|
|
|
48
62
|
|
|
49
63
|
private
|
|
50
64
|
|
|
65
|
+
def declare_shared(variable, writable:)
|
|
66
|
+
SHARED_DECLARATIONS.compute_if_absent(variable) { Concurrent::Map.new }[name || self] = writable
|
|
67
|
+
end
|
|
68
|
+
|
|
51
69
|
def create_reactive_variables(variable, initial_value, shared: false)
|
|
70
|
+
unless initial_value.nil? || initial_value.is_a?(Proc)
|
|
71
|
+
raise ArgumentError, "Initial value for :#{variable} must be a proc or nil"
|
|
72
|
+
end
|
|
73
|
+
|
|
52
74
|
define_method(variable) do
|
|
53
75
|
container_name = shared ? Connection::SHARED_CONTAINER : live_id
|
|
54
76
|
|
|
@@ -60,11 +82,12 @@ module LiveCable
|
|
|
60
82
|
|
|
61
83
|
return if initial_value.nil?
|
|
62
84
|
|
|
63
|
-
|
|
64
|
-
initial_value.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
85
|
+
prerender_container[variable] =
|
|
86
|
+
if initial_value.arity.positive?
|
|
87
|
+
initial_value.call(self)
|
|
88
|
+
else
|
|
89
|
+
initial_value.call
|
|
90
|
+
end
|
|
68
91
|
end
|
|
69
92
|
|
|
70
93
|
define_method("#{variable}=") do |value|
|
|
@@ -99,6 +122,11 @@ module LiveCable
|
|
|
99
122
|
@defaults = (defaults || {}).symbolize_keys
|
|
100
123
|
end
|
|
101
124
|
|
|
125
|
+
# @api private
|
|
126
|
+
def round_trip_defaults
|
|
127
|
+
self.defaults = DefaultsSigner.round_trip(defaults, self.class)
|
|
128
|
+
end
|
|
129
|
+
|
|
102
130
|
def apply_defaults
|
|
103
131
|
# Don't set defaults more than once
|
|
104
132
|
return if defaults_applied
|
|
@@ -17,6 +17,8 @@ module LiveCable
|
|
|
17
17
|
|
|
18
18
|
def render
|
|
19
19
|
@rendered = true
|
|
20
|
+
return render_in(live_connection.view_context) if live_connection
|
|
21
|
+
|
|
20
22
|
ApplicationController.renderer.render(self, layout: false)
|
|
21
23
|
end
|
|
22
24
|
|
|
@@ -38,13 +40,15 @@ module LiveCable
|
|
|
38
40
|
end
|
|
39
41
|
|
|
40
42
|
def render_in(view_context)
|
|
43
|
+
template_changed = false
|
|
44
|
+
|
|
41
45
|
view, render_context = view_context.with_render_context(self) do
|
|
42
|
-
result = view_context.render(template: to_partial_path, locals:)
|
|
46
|
+
result = view_context.render(template: to_partial_path, locals: locals(view_context))
|
|
43
47
|
|
|
44
48
|
unless result.is_a?(LiveCable::Rendering::Partial)
|
|
45
|
-
|
|
46
|
-
"[LiveCable
|
|
47
|
-
'
|
|
49
|
+
LiveCable.warn_once(
|
|
50
|
+
"[LiveCable] #{to_partial_path} was rendered without a .live.erb template; " \
|
|
51
|
+
'the full template diff is sent on every change, which is less performant.'
|
|
48
52
|
)
|
|
49
53
|
|
|
50
54
|
next result
|
|
@@ -73,8 +77,19 @@ module LiveCable
|
|
|
73
77
|
result.for_component(self, view_context).render_changes(changes)
|
|
74
78
|
end
|
|
75
79
|
|
|
80
|
+
# A render callback halted this inline render: the client keeps the
|
|
81
|
+
# child's last render, and its children stay owned but unrendered
|
|
82
|
+
if view.nil? && live_connection && !render_context.root?
|
|
83
|
+
render_context.inherit_skipped(previous_render_context) if previous_render_context
|
|
84
|
+
@previous_render_context = render_context
|
|
85
|
+
|
|
86
|
+
# Without a Partial the client stores nothing for the child to keep
|
|
87
|
+
return plain_html ? insert_root_attributes(plain_html, view_context) : child_placeholder(view_context)
|
|
88
|
+
end
|
|
89
|
+
|
|
76
90
|
unless (partial = view.is_a?(Array))
|
|
77
91
|
view = [view]
|
|
92
|
+
@plain_html = view[0] if live_connection
|
|
78
93
|
end
|
|
79
94
|
|
|
80
95
|
# If we are rendering the initial page, we only need to add the attributes to most parent element
|
|
@@ -87,11 +102,9 @@ module LiveCable
|
|
|
87
102
|
end
|
|
88
103
|
|
|
89
104
|
if previous_render_context
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
destroyed = previous_render_context.children - render_context.children - preserved
|
|
94
|
-
destroyed.each(&:destroy)
|
|
105
|
+
render_context.inherit_skipped(previous_render_context) unless template_changed
|
|
106
|
+
owned = render_context.owned_children.to_set(&:object_id)
|
|
107
|
+
previous_render_context.owned_children.each { |child| child.destroy unless owned.include?(child.object_id) }
|
|
95
108
|
end
|
|
96
109
|
|
|
97
110
|
# We didn't get a LiveCable Partial, so just return it with one part
|
|
@@ -104,7 +117,10 @@ module LiveCable
|
|
|
104
117
|
end
|
|
105
118
|
|
|
106
119
|
if live_connection
|
|
107
|
-
@
|
|
120
|
+
@plain_html = nil
|
|
121
|
+
|
|
122
|
+
# A frame with no subscription to carry it is dropped, so the client never gets these parts
|
|
123
|
+
@static_sent[previous_template_path] = true if render_context.root_component.subscribed?
|
|
108
124
|
|
|
109
125
|
@previous_render_context = render_context
|
|
110
126
|
result = LiveCable::Rendering::RenderResult.new(live_id, view, to_partial_path)
|
|
@@ -118,7 +134,7 @@ module LiveCable
|
|
|
118
134
|
# This allows JavaScript to store the component before subscription connects
|
|
119
135
|
render_context << result
|
|
120
136
|
|
|
121
|
-
return
|
|
137
|
+
return child_placeholder(view_context)
|
|
122
138
|
end
|
|
123
139
|
end
|
|
124
140
|
|
|
@@ -130,6 +146,11 @@ module LiveCable
|
|
|
130
146
|
previous_render_context&.children || []
|
|
131
147
|
end
|
|
132
148
|
|
|
149
|
+
# @return [Array<LiveCable::Component>]
|
|
150
|
+
def owned_children
|
|
151
|
+
previous_render_context&.owned_children || []
|
|
152
|
+
end
|
|
153
|
+
|
|
133
154
|
private
|
|
134
155
|
|
|
135
156
|
# @return [Hash<String, Boolean>]
|
|
@@ -141,13 +162,17 @@ module LiveCable
|
|
|
141
162
|
# @return [LiveCable::RenderContext, nil]
|
|
142
163
|
attr_reader :previous_render_context
|
|
143
164
|
|
|
165
|
+
# @return [String, nil] the last connected render of a template without a Partial
|
|
166
|
+
attr_reader :plain_html
|
|
167
|
+
|
|
144
168
|
def insert_root_attributes(html, view_context)
|
|
145
169
|
matches = html.match(/(?:\n\s*|^\s*|<!--.*?-->)<([a-zA-Z0-9-]+)/)
|
|
146
170
|
|
|
147
171
|
unless matches
|
|
172
|
+
preview = html.to_s.strip[0, 80]
|
|
148
173
|
raise LiveCable::Error,
|
|
149
174
|
"#{self.class.name} template must have a single root HTML element " \
|
|
150
|
-
|
|
175
|
+
"(could not find an opening tag in the first part of rendered output: #{preview.inspect})"
|
|
151
176
|
end
|
|
152
177
|
|
|
153
178
|
attributes = {
|
|
@@ -157,17 +182,29 @@ module LiveCable
|
|
|
157
182
|
'data-live-status-value' => subscribed? ? 'subscribed' : 'disconnected',
|
|
158
183
|
}
|
|
159
184
|
|
|
160
|
-
|
|
185
|
+
# Sign the defaults so the client can't tamper with them on the round
|
|
186
|
+
# trip and set non-writable reactive variables at subscribe time.
|
|
187
|
+
unless live_connection
|
|
188
|
+
attributes['live-defaults'] = LiveCable::DefaultsSigner.sign(defaults, live_id)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Build a new string rather than mutating `html` in place with
|
|
192
|
+
# String#insert - the rendered part may be frozen, which would raise
|
|
193
|
+
# a FrozenError.
|
|
194
|
+
insert_at = matches.end(1)
|
|
195
|
+
rendered_attributes = " #{view_context.tag.attributes(attributes)}"
|
|
196
|
+
"#{html[0...insert_at]}#{rendered_attributes}#{html[insert_at..]}".html_safe
|
|
197
|
+
end
|
|
161
198
|
|
|
162
|
-
|
|
163
|
-
|
|
199
|
+
def child_placeholder(view_context)
|
|
200
|
+
view_context.content_tag('LiveCable', nil, 'child-live-id' => live_id)
|
|
164
201
|
end
|
|
165
202
|
|
|
166
|
-
def locals
|
|
203
|
+
def locals(view_context)
|
|
167
204
|
identifiers = channel ? channel.connection.identifiers.to_a : []
|
|
168
205
|
|
|
169
206
|
(all_reactive_variables | (self.class.shared_variables || []) | identifiers).
|
|
170
|
-
to_h { |v| [v, public_send(v)] }.
|
|
207
|
+
to_h { |v| [v, view_context.live_cable_unwrap(public_send(v))] }.
|
|
171
208
|
merge(
|
|
172
209
|
component: self
|
|
173
210
|
)
|
|
@@ -11,6 +11,13 @@ module LiveCable
|
|
|
11
11
|
additional_streams.each do |stream_name|
|
|
12
12
|
channel.stop_stream_from(stream_name)
|
|
13
13
|
end
|
|
14
|
+
|
|
15
|
+
additional_streams.clear
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def stop_stream_from(channel_name)
|
|
19
|
+
channel.stop_stream_from(channel_name)
|
|
20
|
+
additional_streams.delete(channel_name)
|
|
14
21
|
end
|
|
15
22
|
|
|
16
23
|
def stream_from(channel_name, callback = nil, coder: nil, &block)
|
|
@@ -19,11 +26,35 @@ module LiveCable
|
|
|
19
26
|
channel.stream_from(channel_name, coder:) do |payload|
|
|
20
27
|
callback ||= block
|
|
21
28
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
# Stopping a stream, as disconnect does, can't take back a broadcast
|
|
30
|
+
# already queued, which may run after it or wait on the lock below
|
|
31
|
+
connection = live_connection
|
|
32
|
+
next unless connection
|
|
33
|
+
|
|
34
|
+
# Held across the callback and the render together: the callback's
|
|
35
|
+
# writes are what race an action on another worker, so locking only
|
|
36
|
+
# the broadcast would leave the race in place.
|
|
37
|
+
connection.synchronize do
|
|
38
|
+
next unless live_connection == connection
|
|
39
|
+
next unless additional_streams.include?(channel_name)
|
|
40
|
+
|
|
41
|
+
connection.reset_changeset
|
|
42
|
+
|
|
43
|
+
# A rescue_from handler that takes the error may have changed state
|
|
44
|
+
# to show it, so render in that case too; after an _error there is
|
|
45
|
+
# nothing left to render into.
|
|
46
|
+
handled = begin
|
|
47
|
+
callback.call(payload)
|
|
48
|
+
true
|
|
49
|
+
rescue StandardError => error
|
|
50
|
+
connection.rescue_error(self, error)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
connection.broadcast_changeset if handled
|
|
54
|
+
# Nothing above this rescues a stream callback, so a raising
|
|
55
|
+
# rescue_from handler or render must not escape to ActionCable
|
|
25
56
|
rescue StandardError => error
|
|
26
|
-
|
|
57
|
+
connection.handle_error(self, error)
|
|
27
58
|
end
|
|
28
59
|
end
|
|
29
60
|
end
|
data/lib/live_cable/component.rb
CHANGED
|
@@ -6,6 +6,7 @@ module LiveCable
|
|
|
6
6
|
include ReactiveVariables
|
|
7
7
|
include Identification
|
|
8
8
|
include Lifecycle
|
|
9
|
+
include Dispatching
|
|
9
10
|
include Broadcasting
|
|
10
11
|
include Events
|
|
11
12
|
include Rendering
|
|
@@ -17,6 +18,9 @@ module LiveCable
|
|
|
17
18
|
# @return [String]
|
|
18
19
|
attr_reader :id
|
|
19
20
|
|
|
21
|
+
# @return [Hash]
|
|
22
|
+
attr_reader :defaults
|
|
23
|
+
|
|
20
24
|
def initialize(id, **defaults)
|
|
21
25
|
@id = self.class.resolve_id(id)
|
|
22
26
|
@rendered = false
|
|
@@ -32,9 +36,6 @@ module LiveCable
|
|
|
32
36
|
# @return [Boolean]
|
|
33
37
|
attr_reader :subscribed
|
|
34
38
|
|
|
35
|
-
# @return [Hash]
|
|
36
|
-
attr_reader :defaults
|
|
37
|
-
|
|
38
39
|
# @return [LiveCable::Channel, nil]
|
|
39
40
|
attr_reader :channel
|
|
40
41
|
end
|