Sha256: f86e74df4708106bba79895451ab0fd40f1f05b10cfbebdaed91f718e7b47407

Contents?: true

Size: 1.8 KB

Versions: 18

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'thread'

require 'timeout'
require 'listen/event/processor'
require 'listen/thread'

module Listen
  module Event
    class Loop
      include Listen::FSM

      class Error < RuntimeError
        class NotStarted < Error; end
      end

      start_state :pre_start
      state :pre_start
      state :starting
      state :started
      state :stopped

      def initialize(config)
        @config = config
        @wait_thread = nil
        @reasons = ::Queue.new
        initialize_fsm
      end

      def wakeup_on_event
        if started? && @wait_thread&.alive?
          _wakeup(:event)
        end
      end

      def started?
        state == :started
      end

      MAX_STARTUP_SECONDS = 5.0

      def start
        # TODO: use a Fiber instead?
        return unless state == :pre_start

        transition! :starting

        @wait_thread = Listen::Thread.new("wait_thread") do
          _process_changes
        end

        Listen.logger.debug("Waiting for processing to start...")

        wait_for_state(:started, MAX_STARTUP_SECONDS) or
          raise Error::NotStarted, "thread didn't start in #{MAX_STARTUP_SECONDS} seconds (in state: #{state.inspect})"

        Listen.logger.debug('Processing started.')
      end

      def pause
        # TODO: works?
        # fail NotImplementedError
      end

      def stop
        transition! :stopped

        @wait_thread&.join
        @wait_thread = nil
      end

      def stopped?
        state == :stopped
      end

      private

      def _process_changes
        processor = Event::Processor.new(@config, @reasons)

        transition! :started

        processor.loop_for(@config.min_delay_between_events)
      end

      def _wakeup(reason)
        @reasons << reason
        @wait_thread.wakeup
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 3 rubygems

Version Path
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/listen-3.3.3/lib/listen/event/loop.rb
listen-3.4.0 lib/listen/event/loop.rb
listen-3.3.4 lib/listen/event/loop.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/listen-3.3.3/lib/listen/event/loop.rb
qiita_org-0.1.35 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
qiita_org-0.1.34 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
qiita_org-0.1.33 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
qiita_org-0.1.32 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
qiita_org-0.1.31 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
listen-3.3.3 lib/listen/event/loop.rb
listen-3.3.2 lib/listen/event/loop.rb
qiita_org-0.1.30 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
qiita_org-0.1.29 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
qiita_org-0.1.28 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
qiita_org-0.1.27 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
qiita_org-0.1.26 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
qiita_org-0.1.25 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/event/loop.rb
listen-3.3.1 lib/listen/event/loop.rb