Sha256: 1f2c9162fe3ada9c176d20349f04b5697498fb4949e914816c092c4fa7e31451

Contents?: true

Size: 586 Bytes

Versions: 2

Compression:

Stored size: 586 Bytes

Contents

require 'thread'
require 'monitor'

module Observed
  class BasicEventBus
    def initialize
      @monitor = ::Monitor.new
      @subscribers = []
    end
    def emit(tag, *params)
      handle_event(tag, *params)
    end

    def on_receive(pattern, &block)
      @monitor.synchronize do
        @subscribers.push [pattern, block]
      end
    end

    private

    def handle_event(tag, *params)
      @monitor.synchronize do
        @subscribers.each do |pattern, s|
          if pattern.match(tag)
            s.call *params
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
observed-0.2.0 lib/observed/basic_event_bus.rb
observed-0.2.0.rc2 lib/observed/basic_event_bus.rb