Class: Lightpanda::Client::Subscriber
- Inherits:
-
Object
- Object
- Lightpanda::Client::Subscriber
- Defined in:
- lib/lightpanda/client/subscriber.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #dispatch(event, params) ⇒ Object
-
#initialize ⇒ Subscriber
constructor
A new instance of Subscriber.
- #subscribe(event, &block) ⇒ Object
- #subscribed?(event) ⇒ Boolean
- #unsubscribe(event, block = nil) ⇒ Object
Constructor Details
#initialize ⇒ Subscriber
Returns a new instance of Subscriber.
6 7 8 9 |
# File 'lib/lightpanda/client/subscriber.rb', line 6 def initialize @subscriptions = Hash.new { |h, k| h[k] = [] } @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ Object
37 38 39 |
# File 'lib/lightpanda/client/subscriber.rb', line 37 def clear @mutex.synchronize { @subscriptions.clear } end |
#dispatch(event, params) ⇒ Object
27 28 29 30 31 |
# File 'lib/lightpanda/client/subscriber.rb', line 27 def dispatch(event, params) callbacks = @mutex.synchronize { @subscriptions[event].dup } callbacks.each { |callback| callback.call(params) } end |
#subscribe(event, &block) ⇒ Object
11 12 13 14 15 |
# File 'lib/lightpanda/client/subscriber.rb', line 11 def subscribe(event, &block) @mutex.synchronize do @subscriptions[event] << block end end |
#subscribed?(event) ⇒ Boolean
33 34 35 |
# File 'lib/lightpanda/client/subscriber.rb', line 33 def subscribed?(event) @mutex.synchronize { @subscriptions.key?(event) && @subscriptions[event].any? } end |
#unsubscribe(event, block = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/lightpanda/client/subscriber.rb', line 17 def unsubscribe(event, block = nil) @mutex.synchronize do if block @subscriptions[event].delete(block) else @subscriptions.delete(event) end end end |