Sha256: 4aab3d0f8e314c52c78fa17c82b449df4c61cbfb13114b9a760a4c5518b5a457

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

module WhirledPeas
  module Frame
    class EventLoop
      def initialize(template_factory, refresh_rate, logger=NullLogger.new)
        @template_factory = template_factory
        @queue = Queue.new
        @refresh_rate = refresh_rate
        @logger = logger
      end

      def enqueue(name, duration, args)
        queue.push([name, duration, args])
      end

      def running?
        @running
      end

      def start
        logger.info('EVENT LOOP') { 'Starting' }
        @running = true
        screen = UI::Screen.new
        sleep(0.01) while queue.empty?  # Wait for the first event
        remaining_frames = 1
        template = nil
        while @running && remaining_frames > 0
          frame_at = Time.now
          next_frame_at = frame_at + 1.0 / refresh_rate
          remaining_frames -= 1
          if remaining_frames > 0
            screen.refresh if screen.needs_refresh?
          elsif !queue.empty?
            name, duration, args = queue.pop
            remaining_frames = duration ? duration * refresh_rate : 1
            template = template_factory.build(name, args)
            screen.paint(template)
          end
          sleep([0, next_frame_at - Time.now].max)
        end
        logger.info('EVENT LOOP') { 'Exiting normally' }
      rescue
        logger.warn('EVENT LOOP') { 'Exiting with error' }
        @running = false
        raise
      ensure
        screen.finalize if screen
      end

      def stop
        logger.info('EVENT LOOP') { 'Stopping...' }
        @running = false
      end

      private

      attr_reader :template_factory, :queue, :refresh_rate, :logger
    end
    private_constant :EventLoop
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whirled_peas-0.2.0 lib/whirled_peas/frame/event_loop.rb