Sha256: df6b5d6c351deaf03d1f4c7bb43baac9db30d55397b67eff3cb862f93668d66c

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

# typed: true
require 'ddtrace/logger'

module Datadog
  # A simple pub-sub event model for components to exchange messages through.
  class Event
    attr_reader \
      :name,
      :subscriptions

    def initialize(name)
      @name = name
      @subscriptions = {}
      @mutex = Mutex.new
    end

    def subscribe(key, &block)
      raise ArgumentError, 'Must give a block to subscribe!' unless block

      @mutex.synchronize do
        subscriptions[key] = block
      end
    end

    def unsubscribe(key)
      @mutex.synchronize do
        subscriptions.delete(key)
      end
    end

    def unsubscribe_all!
      @mutex.synchronize do
        subscriptions.clear
      end

      true
    end

    def publish(*args)
      @mutex.synchronize do
        subscriptions.each do |key, block|
          begin
            block.call(*args)
          rescue StandardError => e
            Datadog.logger.debug("Error while handling '#{key}' for '#{name}' event: #{e.message}")
          end
        end

        true
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ddtrace-0.54.2 lib/ddtrace/event.rb
ddtrace-0.54.1 lib/ddtrace/event.rb
ddtrace-0.54.0 lib/ddtrace/event.rb
ddtrace-0.53.0 lib/ddtrace/event.rb
ddtrace-0.52.0 lib/ddtrace/event.rb