Module: Turbo::Streams::Broadcasts

Includes:
ActionHelper
Included in:
Turbo::StreamsChannel
Defined in:
app/channels/turbo/streams/broadcasts.rb

Overview

Provides the broadcast actions in synchronous and asynchronous form for the Turbo::StreamsChannel. See Turbo::Broadcastable for the user-facing API that invokes these methods with most of the paperwork filled out already.

Can be used directly using something like Turbo::StreamsChannel.broadcast_remove_to :entries, target: 1.

Instance Method Summary collapse

Instance Method Details

#broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'app/channels/turbo/streams/broadcasts.rb', line 78

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

#broadcast_action_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) ⇒ Object



40
41
42
43
44
45
46
# File 'app/channels/turbo/streams/broadcasts.rb', line 40

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

#broadcast_after_later_to(*streamables, **opts) ⇒ Object



60
61
62
# File 'app/channels/turbo/streams/broadcasts.rb', line 60

def broadcast_after_later_to(*streamables, **opts)
  broadcast_action_later_to(*streamables, action: :after, **opts)
end

#broadcast_after_to(*streamables, **opts) ⇒ Object



24
25
26
# File 'app/channels/turbo/streams/broadcasts.rb', line 24

def broadcast_after_to(*streamables, **opts)
  broadcast_action_to(*streamables, action: :after, **opts)
end

#broadcast_append_later_to(*streamables, **opts) ⇒ Object



64
65
66
# File 'app/channels/turbo/streams/broadcasts.rb', line 64

def broadcast_append_later_to(*streamables, **opts)
  broadcast_action_later_to(*streamables, action: :append, **opts)
end

#broadcast_append_to(*streamables, **opts) ⇒ Object



28
29
30
# File 'app/channels/turbo/streams/broadcasts.rb', line 28

def broadcast_append_to(*streamables, **opts)
  broadcast_action_to(*streamables, action: :append, **opts)
end

#broadcast_before_later_to(*streamables, **opts) ⇒ Object



56
57
58
# File 'app/channels/turbo/streams/broadcasts.rb', line 56

def broadcast_before_later_to(*streamables, **opts)
  broadcast_action_later_to(*streamables, action: :before, **opts)
end

#broadcast_before_to(*streamables, **opts) ⇒ Object



20
21
22
# File 'app/channels/turbo/streams/broadcasts.rb', line 20

def broadcast_before_to(*streamables, **opts)
  broadcast_action_to(*streamables, action: :before, **opts)
end

#broadcast_prepend_later_to(*streamables, **opts) ⇒ Object



68
69
70
# File 'app/channels/turbo/streams/broadcasts.rb', line 68

def broadcast_prepend_later_to(*streamables, **opts)
  broadcast_action_later_to(*streamables, action: :prepend, **opts)
end

#broadcast_prepend_to(*streamables, **opts) ⇒ Object



32
33
34
# File 'app/channels/turbo/streams/broadcasts.rb', line 32

def broadcast_prepend_to(*streamables, **opts)
  broadcast_action_to(*streamables, action: :prepend, **opts)
end

#broadcast_refresh_later_to(*streamables, request_id: Turbo.current_request_id, **opts) ⇒ Object



72
73
74
75
76
# File 'app/channels/turbo/streams/broadcasts.rb', line 72

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

#broadcast_refresh_to(*streamables, **opts) ⇒ Object



36
37
38
# File 'app/channels/turbo/streams/broadcasts.rb', line 36

def broadcast_refresh_to(*streamables, **opts)
  broadcast_stream_to(*streamables, content: turbo_stream_refresh_tag)
end

#broadcast_remove_to(*streamables, **opts) ⇒ Object



8
9
10
# File 'app/channels/turbo/streams/broadcasts.rb', line 8

def broadcast_remove_to(*streamables, **opts)
  broadcast_action_to(*streamables, action: :remove, render: false, **opts)
end

#broadcast_render_later_to(*streamables, **rendering) ⇒ Object



94
95
96
# File 'app/channels/turbo/streams/broadcasts.rb', line 94

def broadcast_render_later_to(*streamables, **rendering)
  Turbo::Streams::BroadcastJob.perform_later stream_name_from(streamables), **rendering
end

#broadcast_render_to(*streamables, **rendering) ⇒ Object



90
91
92
# File 'app/channels/turbo/streams/broadcasts.rb', line 90

def broadcast_render_to(*streamables, **rendering)
  broadcast_stream_to(*streamables, content: render_format(:turbo_stream, **rendering))
end

#broadcast_replace_later_to(*streamables, **opts) ⇒ Object



48
49
50
# File 'app/channels/turbo/streams/broadcasts.rb', line 48

def broadcast_replace_later_to(*streamables, **opts)
  broadcast_action_later_to(*streamables, action: :replace, **opts)
end

#broadcast_replace_to(*streamables, **opts) ⇒ Object



12
13
14
# File 'app/channels/turbo/streams/broadcasts.rb', line 12

def broadcast_replace_to(*streamables, **opts)
  broadcast_action_to(*streamables, action: :replace, **opts)
end

#broadcast_stream_to(*streamables, content:) ⇒ Object



98
99
100
101
102
103
104
105
# File 'app/channels/turbo/streams/broadcasts.rb', line 98

def broadcast_stream_to(*streamables, content:)
  streamables.flatten!
  streamables.compact_blank!

  if streamables.present?
    ActionCable.server.broadcast stream_name_from(streamables), content
  end
end

#broadcast_update_later_to(*streamables, **opts) ⇒ Object



52
53
54
# File 'app/channels/turbo/streams/broadcasts.rb', line 52

def broadcast_update_later_to(*streamables, **opts)
  broadcast_action_later_to(*streamables, action: :update, **opts)
end

#broadcast_update_to(*streamables, **opts) ⇒ Object



16
17
18
# File 'app/channels/turbo/streams/broadcasts.rb', line 16

def broadcast_update_to(*streamables, **opts)
  broadcast_action_to(*streamables, action: :update, **opts)
end

#refresh_debouncer_for(*streamables, request_id: nil) ⇒ Object

:nodoc:



107
108
109
# File 'app/channels/turbo/streams/broadcasts.rb', line 107

def refresh_debouncer_for(*streamables, request_id: nil) # :nodoc:
  Turbo::ThreadDebouncer.for("turbo-refresh-debouncer-#{stream_name_from(streamables.including(request_id))}")
end