forked from hotwired/turbo-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcasts.rb
127 lines (100 loc) · 4.55 KB
/
broadcasts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Provides the broadcast actions in synchronous and asynchronous form for the <tt>Turbo::StreamsChannel</tt>.
# See <tt>Turbo::Broadcastable</tt> for the user-facing API that invokes these methods with most of the paperwork filled out already.
#
# Can be used directly using something like <tt>Turbo::StreamsChannel.broadcast_remove_to :entries, target: 1</tt>.
module Turbo::Streams::Broadcasts
include Turbo::Streams::ActionHelper
def broadcast_remove_to(*streamables, **opts)
broadcast_action_to(*streamables, action: :remove, render: false, **opts)
end
def broadcast_replace_to(*streamables, **opts)
broadcast_action_to(*streamables, action: :replace, **opts)
end
def broadcast_update_to(*streamables, **opts)
broadcast_action_to(*streamables, action: :update, **opts)
end
def broadcast_before_to(*streamables, **opts)
broadcast_action_to(*streamables, action: :before, **opts)
end
def broadcast_after_to(*streamables, **opts)
broadcast_action_to(*streamables, action: :after, **opts)
end
def broadcast_append_to(*streamables, **opts)
broadcast_action_to(*streamables, action: :append, **opts)
end
def broadcast_prepend_to(*streamables, **opts)
broadcast_action_to(*streamables, action: :prepend, **opts)
end
def broadcast_refresh_to(*streamables, **opts)
broadcast_stream_to(*streamables, content: turbo_stream_refresh_tag)
end
def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering)
attributes.deep_symbolize_keys! if RUBY_VERSION < "3"
broadcast_stream_to(*streamables, content: turbo_stream_action_tag(
action, target: target, targets: targets, template: render_broadcast_action(rendering), **attributes)
)
end
def broadcast_replace_later_to(*streamables, **opts)
broadcast_action_later_to(*streamables, action: :replace, **opts)
end
def broadcast_update_later_to(*streamables, **opts)
broadcast_action_later_to(*streamables, action: :update, **opts)
end
def broadcast_before_later_to(*streamables, **opts)
broadcast_action_later_to(*streamables, action: :before, **opts)
end
def broadcast_after_later_to(*streamables, **opts)
broadcast_action_later_to(*streamables, action: :after, **opts)
end
def broadcast_append_later_to(*streamables, **opts)
broadcast_action_later_to(*streamables, action: :append, **opts)
end
def broadcast_prepend_later_to(*streamables, **opts)
broadcast_action_later_to(*streamables, action: :prepend, **opts)
end
def broadcast_refresh_later_to(*streamables, request_id: Turbo.current_request_id, **opts)
refresh_debouncer_for(*streamables, request_id: request_id).debounce do
Turbo::Streams::BroadcastStreamJob.perform_later stream_name_from(streamables), content: turbo_stream_refresh_tag(request_id: request_id, **opts).to_str # Sidekiq requires job arguments to be valid JSON types, such as String
end
end
def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering)
streamables.flatten!
streamables.compact_blank!
if streamables.present?
target = convert_to_turbo_stream_dom_id(target)
targets = convert_to_turbo_stream_dom_id(targets, include_selector: true)
Turbo::Streams::ActionBroadcastJob.perform_later \
stream_name_from(streamables), action: action, target: target, targets: targets, attributes: attributes, **rendering
end
end
def broadcast_render_to(*streamables, **rendering)
broadcast_stream_to(*streamables, content: render_format(:turbo_stream, **rendering))
end
def broadcast_render_later_to(*streamables, **rendering)
Turbo::Streams::BroadcastJob.perform_later stream_name_from(streamables), **rendering
end
def broadcast_stream_to(*streamables, content:)
streamables.flatten!
streamables.compact_blank!
if streamables.present?
ActionCable.server.broadcast stream_name_from(streamables), content
end
end
def refresh_debouncer_for(*streamables, request_id: nil) # :nodoc:
Turbo::ThreadDebouncer.for("turbo-refresh-debouncer-#{stream_name_from(streamables.including(request_id))}")
end
private
def render_format(format, **rendering)
ApplicationController.render(formats: [ format ], **rendering)
end
def render_broadcast_action(rendering)
content = rendering.delete(:content)
html = rendering.delete(:html)
render = rendering.delete(:render)
if render == false
nil
else
content || html || (render_format(:html, **rendering) if rendering.present?)
end
end
end