Class: Rubee::WebSocketConnections
- Inherits:
-
Object
- Object
- Rubee::WebSocketConnections
- Includes:
- Singleton
- Defined in:
- lib/rubee/websocket/websocket_connections.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #flush_all ⇒ Object
-
#initialize ⇒ WebSocketConnections
constructor
A new instance of WebSocketConnections.
- #register(channel, io) ⇒ Object
- #remove(channel, io) ⇒ Object
- #remove_all(io) ⇒ Object
- #stream(channel, args = {}) ⇒ Object
Constructor Details
#initialize ⇒ WebSocketConnections
Returns a new instance of WebSocketConnections.
4 5 6 |
# File 'lib/rubee/websocket/websocket_connections.rb', line 4 def initialize @subscribers ||= Hash.new { |h, k| h[k] = [] } end |
Instance Method Details
#clear ⇒ Object
31 32 33 |
# File 'lib/rubee/websocket/websocket_connections.rb', line 31 def clear @subscribers = Hash.new { |h, k| h[k] = [] } end |
#flush_all ⇒ Object
20 21 22 |
# File 'lib/rubee/websocket/websocket_connections.rb', line 20 def flush_all @subscribers.each_value(&:clear) end |
#register(channel, io) ⇒ Object
8 9 10 |
# File 'lib/rubee/websocket/websocket_connections.rb', line 8 def register(channel, io) @subscribers[channel] << io unless @subscribers[channel].include?(io) end |
#remove(channel, io) ⇒ Object
12 13 14 |
# File 'lib/rubee/websocket/websocket_connections.rb', line 12 def remove(channel, io) @subscribers[channel].delete(io) end |
#remove_all(io) ⇒ Object
16 17 18 |
# File 'lib/rubee/websocket/websocket_connections.rb', line 16 def remove_all(io) @subscribers.each_value { _1.delete(io) } end |
#stream(channel, args = {}) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/rubee/websocket/websocket_connections.rb', line 24 def stream(channel, args = {}) ios = @subscribers[channel] if !ios&.empty? && ios.all? { _1.respond_to?(:call) } ios.each { _1.call(args) } end end |