Skip to content

Commit 5ea0f55

Browse files
authored
Update adapter to handle ActionCable adapterization in 8.2 (#80)
1 parent 89d34d7 commit 5ea0f55

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

lib/action_cable/subscription_adapter/solid_cable.rb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class SolidCable < ::ActionCable::SubscriptionAdapter::Base
1212

1313
def initialize(*)
1414
super
15+
@mutex =
16+
if defined?(@server)
17+
@server.mutex
18+
else
19+
Mutex.new
20+
end
21+
1522
@listener = nil
1623
end
1724

@@ -33,11 +40,20 @@ def unsubscribe(channel, callback)
3340

3441
private
3542
def listener
36-
@listener || @server.mutex.synchronize do
37-
@listener ||= Listener.new(@server.event_loop)
43+
@listener || @mutex.synchronize do
44+
@listener ||= Listener.new(self, pubsub_executor)
3845
end
3946
end
4047

48+
def pubsub_executor
49+
@pubsub_executor ||=
50+
if respond_to?(:executor, true)
51+
executor
52+
else
53+
@server.event_loop
54+
end
55+
end
56+
4157
class Listener < ::ActionCable::SubscriptionAdapter::SubscriberMap
4258
CONNECTION_ERRORS = [
4359
ActiveRecord::ConnectionFailed,
@@ -46,10 +62,12 @@ class Listener < ::ActionCable::SubscriptionAdapter::SubscriberMap
4662
]
4763
Stop = Class.new(Exception)
4864

49-
def initialize(event_loop)
50-
super()
65+
delegate :logger, to: :@adapter
5166

52-
@event_loop = event_loop
67+
def initialize(adapter, executor)
68+
super()
69+
@adapter = adapter
70+
@executor = executor
5371

5472
# Critical section begins with 0 permits. It can be understood as
5573
# being "normally held" by the listener thread. It is released
@@ -105,19 +123,19 @@ def shutdown
105123

106124
def add_channel(channel, on_success)
107125
channels[channel] = last_message_id
108-
event_loop.post(&on_success) if on_success
126+
on_success.call if on_success
109127
end
110128

111129
def remove_channel(channel)
112130
channels.delete(channel)
113131
end
114132

115133
def invoke_callback(*)
116-
event_loop.post { super }
134+
executor.post { super }
117135
end
118136

119137
private
120-
attr_reader :event_loop, :thread
138+
attr_reader :executor, :thread
121139
attr_accessor :last_id, :reconnect_attempt
122140

123141
def last_message_id

0 commit comments

Comments
 (0)