Sha256: 76803d2916872d2a978afb1e0f69e558d34cf08d64b9bc61f93c590e09ab29ec

Contents?: true

Size: 882 Bytes

Versions: 1

Compression:

Stored size: 882 Bytes

Contents

# frozen_string_literal: true

require "dry/events/publisher"

module Dry
  module Monitor
    CLOCK = Clock.new

    class Notifications
      include Core::Constants
      include Events::Publisher["Dry::Monitor::Notifications"]

      attr_reader :id, :clock

      def initialize(id)
        @id = id
        @clock = CLOCK
      end

      def start(event_id, payload)
        instrument(event_id, payload)
      end

      def stop(event_id, payload)
        instrument(event_id, payload)
      end

      def instrument(event_id, payload = EMPTY_HASH, &block)
        result, time = @clock.measure(&block) if block_given?

        process(event_id, payload) do |event, listener|
          if time
            listener.(event.payload(payload.merge(time: time)))
          else
            listener.(event)
          end
        end

        result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-monitor-1.0.0 lib/dry/monitor/notifications.rb