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

Returns a new instance of SubscriberMap.



6
7
8
9
# File 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb', line 6

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

Instance Method Details

#add_channel(channel, on_success) ⇒ Object



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

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

#add_subscriber(channel, subscriber, on_success) ⇒ Object



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

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



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

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



54
55
56
# File 'actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb', line 54

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

#remove_channel(channel) ⇒ Object



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

def remove_channel(channel)
end

#remove_subscriber(channel, subscriber) ⇒ Object



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

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