Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method ActionCable::Channel#stream_or_reject_for to stream if record is present, otherwise reject the connection #38375

Merged
merged 1 commit into from
Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions actioncable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Add `ActionCable::Channel#stream_or_reject_for` to stream if record is present, otherwise reject the connection

*Atul Bhosale*

* Add `ActionCable::Channel#stop_stream_from` and `#stop_stream_for` to unsubscribe from a specific stream.

*Zhang Kang*
Expand Down
12 changes: 12 additions & 0 deletions actioncable/lib/action_cable/channel/streams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ def stop_all_streams
end.clear
end

# Calls stream_for if record is present, otherwise calls reject.
# This method is intended to be called when you're looking
# for a record based on a parameter, if its found it will start
# streaming. If the record is nil then it will reject the connection.
Comment on lines +127 to +130
Copy link

@bekicot bekicot Feb 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code is already tell by itself what it does. The comment is no longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am ok to remove it when I get another comment from someone from core/committer team(who has access to merge this PR)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's reasonable.

def stream_or_reject_for(record)
if record
stream_for record
else
reject
end
end

private
delegate :pubsub, to: :connection

Expand Down
33 changes: 33 additions & 0 deletions actioncable/test/channel/stream_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,39 @@ class StreamTest < ActionCable::TestCase
end
end

test "stream_or_reject_for" do
run_in_eventmachine do
connection = TestConnection.new

channel = ChatChannel.new connection, ""
channel.subscribe_to_channel
channel.stream_or_reject_for Room.new(1)
wait_for_async

pubsub_call = channel.pubsub.class.class_variable_get "@@subscribe_called"

assert_equal "action_cable:stream_tests:chat:Room#1-Campfire", pubsub_call[:channel]
assert_instance_of Proc, pubsub_call[:callback]
assert_instance_of Proc, pubsub_call[:success_callback]
end
end

test "reject subscription when nil is passed to stream_or_reject_for" do
run_in_eventmachine do
connection = TestConnection.new
channel = ChatChannel.new connection, "{id: 1}", id: 1
channel.subscribe_to_channel
channel.stream_or_reject_for nil
assert_nil connection.last_transmission

wait_for_async

rejection = { "identifier" => "{id: 1}", "type" => "reject_subscription" }
connection.transmit(rejection)
assert_equal rejection, connection.last_transmission
end
end

test "stream_from subscription confirmation" do
run_in_eventmachine do
connection = TestConnection.new
Expand Down