Sha256: 101975441dbabba1118bafc2760b75ae0e434d1ca2d4cc441a1db37eebf08219

Contents?: true

Size: 1.28 KB

Versions: 10

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Mutant
  class Timer
    include Anima.new(:process)

    # The now monotonic time
    #
    # @return [Float]
    def now
      process.clock_gettime(Process::CLOCK_MONOTONIC)
    end

    class Deadline
      include Anima.new(:timer, :allowed_time)

      def initialize(*arguments)
        super(*arguments)
        @start_at = timer.now
      end

      # Test if deadline is expired
      #
      # @return [Boolean]
      def expired?
        time_left <= 0
      end

      # Deadline status snapshot
      class Status
        include Anima.new(:time_left)

        # Test if deadline is not yet expired
        def ok?
          time_left.nil? || time_left.positive?
        end
      end # Status

      # Capture a deadline status
      #
      # @return [Status]
      def status
        Status.new(time_left: time_left)
      end

      # Probe the time left
      #
      # @return [Float, nil]
      def time_left
        allowed_time - (timer.now - @start_at)
      end

      # Deadline that never expires
      class None < self
        include Concord.new

        # The time left
        #
        # @return [Float, nil]
        def time_left; end

        def expired?
          false
        end
      end
    end # Deadline
  end # Timer
end # Mutant

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mutant-0.11.28 lib/mutant/timer.rb
mutant-0.11.27 lib/mutant/timer.rb
mutant-0.11.26 lib/mutant/timer.rb
mutant-0.11.25 lib/mutant/timer.rb
mutant-0.11.24 lib/mutant/timer.rb
mutant-0.11.23 lib/mutant/timer.rb
mutant-0.11.22 lib/mutant/timer.rb
mutant-0.11.21 lib/mutant/timer.rb
mutant-0.11.20 lib/mutant/timer.rb
mutant-0.11.19 lib/mutant/timer.rb