Module: Mongo::Monitoring::Subscribable
- Included in:
- Mongo::Monitoring, Global
- Defined in:
- lib/mongo/monitoring.rb
Overview
Contains subscription methods common between monitoring and global event subscriptions.
Instance Method Summary collapse
-
#subscribe(topic, subscriber) ⇒ Object
Subscribe a listener to an event topic.
-
#subscribers ⇒ Hash<String, Object>
Get all the subscribers.
-
#subscribers?(topic) ⇒ true, false
Determine if there are any subscribers for a particular event.
-
#unsubscribe(topic, subscriber) ⇒ Object
Unsubscribe a listener from an event topic.
Instance Method Details
#subscribe(topic, subscriber) ⇒ Object
It is possible to subscribe the same listener to the same topic
Subscribe a listener to an event topic.
multiple times, in which case the listener will be invoked as many times as it is subscribed and to unsubscribe it the same number of unsubscribe calls will be needed.
96 97 98 |
# File 'lib/mongo/monitoring.rb', line 96 def subscribe(topic, subscriber) subscribers_for(topic).push(subscriber) end |
#subscribers ⇒ Hash<String, Object>
Get all the subscribers.
148 149 150 |
# File 'lib/mongo/monitoring.rb', line 148 def subscribers @subscribers ||= {} end |
#subscribers?(topic) ⇒ true, false
Determine if there are any subscribers for a particular event.
165 166 167 |
# File 'lib/mongo/monitoring.rb', line 165 def subscribers?(topic) !subscribers_for(topic).empty? end |
#unsubscribe(topic, subscriber) ⇒ Object
Global subscriber registry is separate from per-client subscriber registry. The same subscriber can be subscribed to events from a particular client as well as globally; unsubscribing globally will not unsubscribe that subscriber from the client it was explicitly subscribed to.
Currently the list of global subscribers is copied into a client whenever the client is created. Thus unsubscribing a subscriber globally has no effect for existing clients - they will continue sending events to the unsubscribed subscriber.
Unsubscribe a listener from an event topic.
If the listener was subscribed to the event topic multiple times, this call removes a single subscription.
If the listener was not subscribed to the topic, this operation is a no-op and no exceptions are raised.
129 130 131 132 133 134 135 |
# File 'lib/mongo/monitoring.rb', line 129 def unsubscribe(topic, subscriber) subs = subscribers_for(topic) index = subs.index(subscriber) if index subs.delete_at(index) end end |