Class: Sidekiq::Hierarchy::CallbackRegistry
- Inherits:
-
Object
- Object
- Sidekiq::Hierarchy::CallbackRegistry
- Includes:
- Mutex_m
- Defined in:
- lib/sidekiq/hierarchy/callback_registry.rb
Instance Method Summary collapse
-
#initialize ⇒ CallbackRegistry
constructor
A new instance of CallbackRegistry.
-
#publish(event, *args) ⇒ Object
Call listeners for a given event one by one.
-
#subscribe(event, callback) ⇒ Object
Thread-safe to prevent clobbering, though this should probably never be called outside initialization anyway.
Constructor Details
#initialize ⇒ CallbackRegistry
Returns a new instance of CallbackRegistry.
8 9 10 11 |
# File 'lib/sidekiq/hierarchy/callback_registry.rb', line 8 def initialize @callbacks = {} super end |
Instance Method Details
#publish(event, *args) ⇒ Object
Call listeners for a given event one by one. Note that no method signature contracts are enforced.
26 27 28 29 30 |
# File 'lib/sidekiq/hierarchy/callback_registry.rb', line 26 def publish(event, *args) if to_notify = @callbacks[event] to_notify.each { |callback| callback.call(*args) rescue nil } end end |
#subscribe(event, callback) ⇒ Object
Thread-safe to prevent clobbering, though this should probably never be called outside initialization anyway. callback is a proc/lambda that implements #call
16 17 18 19 20 21 22 |
# File 'lib/sidekiq/hierarchy/callback_registry.rb', line 16 def subscribe(event, callback) synchronize do @callbacks[event] ||= [] @callbacks[event] << callback end self end |