123456789_123456789_123456789_123456789_123456789_

Class: ActionCable::SubscriptionAdapter::SubscriberMap

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: actioncable/lib/action_cable/subscription_adapter/subscriber_map.rb

Class Method Summary

Instance Method Summary

Constructor Details

.newSubscriberMap

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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)

[ GitHub ]

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

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

#remove_channel(channel)

[ GitHub ]

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

def remove_channel(channel)
end

#remove_subscriber(channel, subscriber)

[ GitHub ]

  
# 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