Class: WaterDrop::Instrumentation::CallbacksManager
- Inherits:
-
Object
- Object
- WaterDrop::Instrumentation::CallbacksManager
- Defined in:
- lib/water_drop/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 ⇒ ::WaterDrop::Instrumentation::CallbacksManager constructor
Constructor Details
#initialize ⇒ ::WaterDrop::Instrumentation::CallbacksManager
9 10 11 |
# File 'lib/water_drop/instrumentation/callbacks_manager.rb', line 9 def initialize @callbacks = Concurrent::Hash.new end |
Instance Method Details
#add(id, callable) ⇒ Object
Adds a callback to the manager
28 29 30 |
# File 'lib/water_drop/instrumentation/callbacks_manager.rb', line 28 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
20 21 22 |
# File 'lib/water_drop/instrumentation/callbacks_manager.rb', line 20 def call(*args) @callbacks.values.each { |callback| callback.call(*args) } end |
#delete(id) ⇒ Object
Removes the callback from the manager
34 35 36 |
# File 'lib/water_drop/instrumentation/callbacks_manager.rb', line 34 def delete(id) @callbacks.delete(id) end |