-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: avoid Action Cable "hard" monkey-patching
This PR changes the (historical) way of making Action Cable compatible with AnyCable: - Extend (via #prepend) instead of reopening classes - Preserve the original functionality (by using #anycabled? checks where necessary) Closes #165
- Loading branch information
Showing
24 changed files
with
500 additions
and
500 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "action_cable/channel" | ||
|
||
ActionCable::Channel::Base.prepend(Module.new do | ||
def subscribe_to_channel(force: false) | ||
return if anycabled? && !force | ||
super() | ||
end | ||
|
||
def handle_subscribe | ||
subscribe_to_channel(force: true) | ||
end | ||
|
||
def start_periodic_timers | ||
super unless anycabled? | ||
end | ||
|
||
def stop_periodic_timers | ||
super unless anycabled? | ||
end | ||
|
||
def stream_from(broadcasting, _callback = nil, _options = {}) | ||
return super unless anycabled? | ||
|
||
connection.anycable_socket.subscribe identifier, broadcasting | ||
end | ||
|
||
def stop_stream_from(broadcasting) | ||
return super unless anycabled? | ||
|
||
connection.anycable_socket.unsubscribe identifier, broadcasting | ||
end | ||
|
||
def stop_all_streams | ||
return super unless anycabled? | ||
|
||
connection.anycable_socket.unsubscribe_from_all identifier | ||
end | ||
|
||
private | ||
|
||
def anycabled? | ||
# Use instance variable check here for testing compatibility | ||
connection.instance_variable_defined?(:@anycable_socket) | ||
end | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require "action_cable/connection" | ||
require "anycable/rails/connections/serializable_identification" | ||
|
||
ActionCable::Connection::Base.include(AnyCable::Rails::Connections::SerializableIdentification) | ||
ActionCable::Connection::Base.prepend(Module.new do | ||
attr_reader :anycable_socket | ||
attr_accessor :anycable_request_builder | ||
|
||
# In AnyCable, we lazily populate env by passing it through the middleware chain, | ||
# so we access it via #request | ||
def env | ||
return super unless anycabled? | ||
|
||
request.env | ||
end | ||
|
||
def anycabled? | ||
@anycable_socket | ||
end | ||
|
||
private | ||
|
||
def request | ||
return super unless anycabled? | ||
|
||
@request ||= anycable_request_builder.build_rack_request(@env) | ||
end | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.