aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2016-01-22 11:13:12 +1030
committerMatthew Draper <matthew@trebex.net>2016-01-24 15:52:47 +1030
commitdccc15d4030f250f38987328b6201282c1ef34a5 (patch)
tree0ed4a4769529bc89de3030bc62c56a3d8947db55 /actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb
parent83d2c39d5eb8d82ba124b6725d08c8e90760c764 (diff)
downloadrails-dccc15d4030f250f38987328b6201282c1ef34a5.tar.gz
rails-dccc15d4030f250f38987328b6201282c1ef34a5.tar.bz2
rails-dccc15d4030f250f38987328b6201282c1ef34a5.zip
Split internal subscriber tracking from Postgres adapter
Diffstat (limited to 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb')
-rw-r--r--actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb b/actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb
new file mode 100644
index 0000000000..37eed09793
--- /dev/null
+++ b/actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb
@@ -0,0 +1,53 @@
+module ActionCable
+ module SubscriptionAdapter
+ class SubscriberMap
+ def initialize
+ @subscribers = Hash.new { |h,k| h[k] = [] }
+ @sync = Mutex.new
+ end
+
+ def add_subscriber(channel, subscriber, on_success)
+ @sync.synchronize do
+ new_channel = !@subscribers.key?(channel)
+
+ @subscribers[channel] << subscriber
+
+ if new_channel
+ add_channel channel, on_success
+ elsif on_success
+ on_success.call
+ end
+ end
+ end
+
+ def remove_subscriber(channel, subscriber)
+ @sync.synchronize do
+ @subscribers[channel].delete(subscriber)
+
+ if @subscribers[channel].empty?
+ @subscribers.delete channel
+ remove_channel channel
+ end
+ end
+ end
+
+ def broadcast(channel, message)
+ list = @sync.synchronize { @subscribers[channel].dup }
+ list.each do |subscriber|
+ invoke_callback(subscriber, message)
+ end
+ end
+
+ def add_channel(channel, on_success)
+ on_success.call if on_success
+ end
+
+ def remove_channel(channel)
+ end
+
+ def invoke_callback(callback, message)
+ callback.call message
+ end
+ end
+ end
+end