Sha256: 8a2de4f79cb616bc9572eeabfaa2a57faa854511ef8ad08fe79f2cb59550a4c0

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'thread'

module Ray
  module DSL
    # This class is the one that dispatches events in your program, the one that
    # makes everything work. You may want to create one by yourself if you don't
    # want to use the other classes that use it. You just have to call run every
    # time you need the events to be processed.
    class EventRunner
      def initialize
        @handlers    = []
        @event_list  = []
        @next_events = []

        @mutex = Mutex.new
      end

      # Sends all the known events to our listeners.
      def run
        @mutex.synchronize do
          @event_list  = @next_events
          @next_events = []
        end

        @event_list.each do |ev|
          @handlers.select { |i| i.match? ev }.each do |handler|
            handler.call(ev)
          end
        end
      end

      def add_handler(type, args, block)
        @handlers << Ray::DSL::Handler.new(type, args, block)
      end

      def add_event(type, args)
        @mutex.synchronize { @next_events << Ray::DSL::Event.new(type, args) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ray-0.0.1 lib/ray/dsl/event_runner.rb