Class: Sidekiq::Hierarchy::CallbackRegistry

Inherits:
Object
  • Object
show all
Includes:
Mutex_m
Defined in:
lib/sidekiq/hierarchy/callback_registry.rb

Instance Method Summary collapse

Constructor Details

#initializeCallbackRegistry

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