2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-21 19:43:12 -05:00
|
|
|
module ActionCable
|
|
|
|
module SubscriptionAdapter
|
|
|
|
class SubscriberMap
|
|
|
|
def initialize
|
2016-10-28 23:05:58 -04:00
|
|
|
@subscribers = Hash.new { |h, k| h[k] = [] }
|
2016-01-21 19:43:12 -05:00
|
|
|
@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)
|
2016-05-16 00:07:35 -04:00
|
|
|
list = @sync.synchronize do
|
|
|
|
return if !@subscribers.key?(channel)
|
|
|
|
@subscribers[channel].dup
|
|
|
|
end
|
|
|
|
|
2016-01-21 19:43:12 -05:00
|
|
|
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
|