Class: Karafka::Core::Instrumentation::CallbacksManager
- Inherits:
-
Object
- Object
- Karafka::Core::Instrumentation::CallbacksManager
- Defined in:
- lib/karafka/core/instrumentation/callbacks_manager.rb
Overview
This manager allows us to register multiple callbacks into a hook that is suppose to support a single callback
Instance Method Summary collapse
-
#add(id, callable) ⇒ Object
Adds a callback to the manager.
-
#call(*args) ⇒ Object
Invokes all the callbacks registered one after another.
-
#delete(id) ⇒ Object
Removes the callback from the manager.
- #initialize ⇒ ::Karafka::Core::Instrumentation::CallbacksManager constructor
Constructor Details
#initialize ⇒ ::Karafka::Core::Instrumentation::CallbacksManager
11 12 13 |
# File 'lib/karafka/core/instrumentation/callbacks_manager.rb', line 11 def initialize @callbacks = {} end |
Instance Method Details
#add(id, callable) ⇒ Object
Adds a callback to the manager
30 31 32 |
# File 'lib/karafka/core/instrumentation/callbacks_manager.rb', line 30 def add(id, callable) @callbacks[id] = callable end |
#call(*args) ⇒ Object
Note:
We do not use #each_value
here on purpose. With it being used, we cannot dispatch callbacks and add new at the same time. Since we don’t know when and in what thread things are going to be added to the manager, we need to extract values into an array and run it. That way we can add new things the same time.
Invokes all the callbacks registered one after another
22 23 24 |
# File 'lib/karafka/core/instrumentation/callbacks_manager.rb', line 22 def call(*args) @callbacks.values.each { |callback| callback.call(*args) } end |
#delete(id) ⇒ Object
Removes the callback from the manager
36 37 38 |
# File 'lib/karafka/core/instrumentation/callbacks_manager.rb', line 36 def delete(id) @callbacks.delete(id) end |