Skip to content

Commit

Permalink
Merge pull request #615 from seanpdoyle/issue-614
Browse files Browse the repository at this point in the history
Flatten and compact `*streamables` arguments
  • Loading branch information
brunoprietog authored Jun 21, 2024
2 parents de76217 + a325680 commit c30f7d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
16 changes: 13 additions & 3 deletions app/channels/turbo/streams/broadcasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ def broadcast_refresh_later_to(*streamables, request_id: Turbo.current_request_i
end

def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering)
Turbo::Streams::ActionBroadcastJob.perform_later \
stream_name_from(streamables), action: action, target: target, targets: targets, attributes: attributes, **rendering
streamables.flatten!
streamables.compact_blank!

if streamables.present?
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)
Expand All @@ -88,7 +93,12 @@ def broadcast_render_later_to(*streamables, **rendering)
end

def broadcast_stream_to(*streamables, content:)
ActionCable.server.broadcast stream_name_from(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:
Expand Down
19 changes: 19 additions & 0 deletions test/streams/broadcastable_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
require "test_helper"
require "action_cable"
require "minitest/mock"

class Turbo::BroadcastableTest < ActionCable::Channel::TestCase
include ActiveJob::TestHelper, Turbo::Streams::ActionHelper

setup { @message = Message.new(id: 1, content: "Hello!") }

test "broadcasting ignores blank streamables" do
ActionCable.server.stub :broadcast, proc { flunk "expected no broadcasts" } do
@message.broadcast_remove_to nil
@message.broadcast_remove_to [nil]
@message.broadcast_remove_to ""
@message.broadcast_remove_to [""]
end
end

test "broadcasting later ignores blank streamables" do
assert_no_enqueued_jobs do
@message.broadcast_append_later_to nil
@message.broadcast_append_later_to [nil]
@message.broadcast_append_later_to ""
@message.broadcast_append_later_to [""]
end
end

test "broadcasting remove to stream now" do
assert_broadcast_on "stream", turbo_stream_action_tag("remove", target: "message_1") do
@message.broadcast_remove_to "stream"
Expand Down

0 comments on commit c30f7d9

Please sign in to comment.