class AsyncCable::Registry

Public Instance Methods

add(channel_name, stream_name, connection) click to toggle source

Adds connection to registry. @param channel_name [String] @param stream_name [String] @param connection [AsyncCable::Connection]

# File lib/async_cable/registry.rb, line 15
def add(channel_name, stream_name, connection)
  @mutex.synchronize do
    subscribers[channel_name][stream_name].push(connection)
    connection
  end
end
each(channel_name = nil, stream_name = nil, &block) click to toggle source

Iterate connections asynchronously. @param channel_name [String,NilClass] @param stream_name [String,NilClass] @yield connection [AsyncCable::Connection]

# File lib/async_cable/registry.rb, line 52
def each(channel_name = nil, stream_name = nil, &block)
  list = find(channel_name, stream_name)
  Util.each_async(list, &block)
end
find(channel_name = nil, stream_name = nil) click to toggle source

Return all connections from all channels when `channel_name` omitted. Return all connections from channel when `stream_name` omitted. Return connections from channel stream when `channel_name` and `stream_name` provided. @param channel_name [String,NilClass] @param stream_name [String,NilClass] @return [Array<AsyncCable::Connection>,Array]

# File lib/async_cable/registry.rb, line 40
def find(channel_name = nil, stream_name = nil)
  @mutex.synchronize do
    return subscribers.values.map(&:values).flatten if channel_name.nil?
    return subscribers[channel_name].values.flatten if stream_name.nil?
    subscribers[channel_name][stream_name]
  end
end
init_mutex() click to toggle source
# File lib/async_cable/registry.rb, line 57
def init_mutex
  @mutex ||= Mutex.new
  true
end
remove(channel_name, stream_name, connection) click to toggle source

Removes connection from registry. @param channel_name [String] @param stream_name [String] @param connection [AsyncCable::Connection]

# File lib/async_cable/registry.rb, line 26
def remove(channel_name, stream_name, connection)
  @mutex.synchronize do
    subscribers[channel_name][stream_name].delete(connection)
    subscribers[channel_name].delete(stream_name) if subscribers[channel_name][stream_name].empty?
    connection
  end
end

Private Instance Methods

new_subscribers() click to toggle source
# File lib/async_cable/registry.rb, line 68
def new_subscribers
  Hash.new do |hash, channel_name|
    hash[channel_name] = Hash.new { |h, stream_name| h[stream_name] = [] }
  end
end
subscribers() click to toggle source
# File lib/async_cable/registry.rb, line 64
def subscribers
  @subscribers ||= new_subscribers
end