Sha256: 2c9b88c757976d4fe0c04c10398340c57b7fee12cdae5250067a6ec796ff166b

Contents?: true

Size: 1.38 KB

Versions: 11

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module WaterDrop
  module Instrumentation
    # This manager allows us to register multiple callbacks into a hook that is suppose to support
    # a single callback
    class CallbacksManager
      # @return [::WaterDrop::Instrumentation::CallbacksManager]
      def initialize
        @callbacks = Concurrent::Hash.new
      end

      # Invokes all the callbacks registered one after another
      #
      # @param args [Object] any args that should go to the callbacks
      # @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.
      def call(*args)
        @callbacks.values.each { |callback| callback.call(*args) }
      end

      # Adds a callback to the manager
      #
      # @param id [String] id of the callback (used when deleting it)
      # @param callable [#call] object that responds to a `#call` method
      def add(id, callable)
        @callbacks[id] = callable
      end

      # Removes the callback from the manager
      # @param id [String] id of the callback we want to remove
      def delete(id)
        @callbacks.delete(id)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
waterdrop-2.4.2 lib/waterdrop/instrumentation/callbacks_manager.rb
waterdrop-2.4.1 lib/waterdrop/instrumentation/callbacks_manager.rb
waterdrop-2.4.0 lib/waterdrop/instrumentation/callbacks_manager.rb
waterdrop-2.3.3 lib/waterdrop/instrumentation/callbacks_manager.rb
waterdrop-2.3.2 lib/waterdrop/instrumentation/callbacks_manager.rb
waterdrop-2.3.1 lib/waterdrop/instrumentation/callbacks_manager.rb
waterdrop-2.3.0 lib/waterdrop/instrumentation/callbacks_manager.rb
waterdrop-2.2.0 lib/waterdrop/instrumentation/callbacks_manager.rb
waterdrop-2.1.0 lib/water_drop/instrumentation/callbacks_manager.rb
waterdrop-2.0.7 lib/water_drop/instrumentation/callbacks_manager.rb
waterdrop-2.0.6 lib/water_drop/instrumentation/callbacks_manager.rb