Sha256: a878fec4c75c01e803da73d45034b6834721e5082aacd9fd826fda24b9963cd7

Contents?: true

Size: 1.33 KB

Versions: 11

Compression:

Stored size: 1.33 KB

Contents

# typed: true

require_relative 'address_hash'
require_relative 'subscriber'

module Datadog
  module AppSec
    module Reactive
      # Reactive Engine
      class Engine
        def initialize
          @data = {}
          @subscribers = AddressHash.new { |h, k| h[k] = [] } # TODO: move to AddressHash initializer
          @children = []
        end

        def subscribe(*addresses, &block)
          @subscribers[addresses.freeze] << Subscriber.new(&block).freeze # TODO: move freeze to Subscriber
        end

        def publish(address, data)
          # check if someone has address subscribed
          if @subscribers.addresses.include?(address)

            # someone will be interested, set data
            @data[address] = data

            # find candidates i.e address groups that contain the just posted address
            @subscribers.with(address).each do |addresses|
              # find targets to the address group containing the posted address
              subscribers = @subscribers[addresses]

              # is all data for the targets available?
              if (addresses - @data.keys).empty?
                hash = addresses.each_with_object({}) { |a, h| h[a] = @data[a] }
                subscribers.each { |s| s.call(*hash.values) }
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ddtrace-1.8.0 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.7.0 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.6.1 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.6.0 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.5.2 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.5.1 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.5.0 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.4.2 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.4.1 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.4.0 lib/datadog/appsec/reactive/engine.rb
ddtrace-1.3.0 lib/datadog/appsec/reactive/engine.rb