Sha256: fba7640f2899c11d5945f43e668070c4515622a9a6f261cffcbf99ecf27da9de

Contents?: true

Size: 1.27 KB

Versions: 13

Compression:

Stored size: 1.27 KB

Contents

module Vedeu

  # Provides the main loop for a Vedeu application.
  #
  # Each time Vedeu starts one cycle in the application loop, it triggers the
  # `:tick` event. A completion of that cycle will trigger `:tock`. This can be
  # used by the client application for timing amongst other things.
  class MainLoop

    trap('SIGTERM') { stop! }
    trap('TERM')    { stop! }
    trap('INT')     { stop! }

    # :nocov:
    # Start the main loop.
    #
    # @return [void]
    # @yieldreturn [void] The client application.
    def self.start!
      @started = true
      @loop    = true

      while @loop
        yield

        safe_exit_point!
      end
    rescue VedeuInterrupt
      Vedeu.log(type: :debug, message: 'Vedeu execution interrupted, exiting.')
    end
    # :nocov:

    # Signal that we wish to terminate the running application.
    #
    # @return [void]
    def self.stop!
      @loop = false
    end

    # Check the application has started and we wish to continue running.
    #
    # @raise [VedeuInterrupt] When we wish to terminate the running application.
    # @return [void]
    def self.safe_exit_point!
      if @started && !@loop
        fail VedeuInterrupt

      else
        Vedeu.trigger(:tock, Time.now.to_f)

      end
    end

  end # MainLoop

end # Vedeu

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
vedeu-0.4.31 lib/vedeu/main_loop.rb
vedeu-0.4.30 lib/vedeu/main_loop.rb
vedeu-0.4.29 lib/vedeu/main_loop.rb
vedeu-0.4.28 lib/vedeu/main_loop.rb
vedeu-0.4.27 lib/vedeu/main_loop.rb
vedeu-0.4.26 lib/vedeu/main_loop.rb
vedeu-0.4.25 lib/vedeu/main_loop.rb
vedeu-0.4.24 lib/vedeu/main_loop.rb
vedeu-0.4.23 lib/vedeu/main_loop.rb
vedeu-0.4.22 lib/vedeu/main_loop.rb
vedeu-0.4.21 lib/vedeu/main_loop.rb
vedeu-0.4.20 lib/vedeu/main_loop.rb
vedeu-0.4.19 lib/vedeu/main_loop.rb