Sha256: 80e91f7575df0e5f549686eddbbc0c6179c61c3bdd54116ad9853d373f0800d8

Contents?: true

Size: 1.16 KB

Versions: 39

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8

module OneApm
  module Support
    class EventLoop
      class Timer

        attr_reader :next_fire_time, :event, :interval, :last_fired_at

        def initialize(interval, event, repeat=false)
          @interval      = interval
          @event         = event
          @repeat        = repeat
          @started_at    = Time.now
          @last_fired_at = nil
          reschedule
        end

        def reschedule
          @next_fire_time = calculate_next_fire_time
        end

        def advance(amount)
          @next_fire_time -= amount
        end

        def last_interval_start
          @last_fired_at || @started_at
        end

        def calculate_next_fire_time
          now = Time.now
          return now if @interval == 0
          fire_time = @last_fired_at || now
          while fire_time <= now
            fire_time += @interval
          end
          fire_time
        end

        def set_fired_time
          @last_fired_at = Time.now
        end

        def due?(now=Time.now)
          now >= @next_fire_time
        end

        def finished?
          !@repeat && @last_fired_at
        end

      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.4.1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.4.0 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.7 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.7.rc1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.6 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.6.rc1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.5 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.5.rc1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.4 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.4.rc1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.3 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.3.rc1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.2 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.2.rc1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.1.rc1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.0 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.3.0.rc1 lib/one_apm/support/event/timer.rb
oneapm_rpm-1.2.9 lib/one_apm/support/event/timer.rb