Class: AsyncCable::Registry
- Inherits:
-
Object
- Object
- AsyncCable::Registry
- Extended by:
- SingleForwardable
- Includes:
- Singleton
- Defined in:
- lib/async_cable/registry.rb
Instance Method Summary collapse
-
#add(channel_name, stream_name, connection) ⇒ Object
Adds connection to registry.
-
#each(channel_name = nil, stream_name = nil, &block) ⇒ Object
Iterate connections asynchronously.
-
#find(channel_name = nil, stream_name = nil) ⇒ Array<AsyncCable::Connection>, Array
Return all connections from all channels when ‘channel_name` omitted.
- #init_mutex ⇒ Object
-
#remove(channel_name, stream_name, connection) ⇒ Object
Removes connection from registry.
Instance Method Details
#add(channel_name, stream_name, connection) ⇒ Object
Adds connection to registry.
15 16 17 18 19 20 |
# 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) ⇒ Object
Iterate connections asynchronously.
52 53 54 55 |
# 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) ⇒ Array<AsyncCable::Connection>, Array
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.
40 41 42 43 44 45 46 |
# 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 ⇒ Object
57 58 59 60 |
# File 'lib/async_cable/registry.rb', line 57 def init_mutex @mutex ||= Mutex.new true end |
#remove(channel_name, stream_name, connection) ⇒ Object
Removes connection from registry.
26 27 28 29 30 31 32 |
# 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 |