Class: ActionCable::SubscriptionAdapter::SubscriberMap

Inherits:
Object
  • Object
show all
Defined in:
actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb

Instance Method Summary collapse

Constructor Details

#initializeSubscriberMap


4
5
6
7
# File 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb', line 4

def initialize
  @subscribers = Hash.new { |h,k| h[k] = [] }
  @sync = Mutex.new
end

Instance Method Details

#add_channel(channel, on_success) ⇒ Object


45
46
47
# File 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb', line 45

def add_channel(channel, on_success)
  on_success.call if on_success
end

#add_subscriber(channel, subscriber, on_success) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb', line 9

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

#broadcast(channel, message) ⇒ Object


34
35
36
37
38
39
40
41
42
43
# File 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb', line 34

def broadcast(channel, message)
  list = @sync.synchronize do
    return if !@subscribers.key?(channel)
    @subscribers[channel].dup
  end

  list.each do |subscriber|
    invoke_callback(subscriber, message)
  end
end

#invoke_callback(callback, message) ⇒ Object


52
53
54
# File 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb', line 52

def invoke_callback(callback, message)
  callback.call message
end

#remove_channel(channel) ⇒ Object


49
50
# File 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb', line 49

def remove_channel(channel)
end

#remove_subscriber(channel, subscriber) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb', line 23

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