Sha256: 5bb459e46546c8dc2a59a7a4f882de1b2e1a7859afe63f97ddcd458092b0448a

Contents?: true

Size: 887 Bytes

Versions: 33

Compression:

Stored size: 887 Bytes

Contents

module Einhorn::Event
  class Timer
    attr_reader :time

    def initialize(time, start=nil, &blk)
      @time = time
      @start = start || Time.now
      @blk = blk
    end

    # TODO: abstract into some interface
    def self.open(*args, &blk)
      instance = self.new(*args, &blk)
      instance.register!
      instance
    end

    def expires_at
      @start + @time
    end

    def ring!
      now = Time.now
      Einhorn.log_debug("Ringing timer that was scheduled #{now - @start}s ago and expired #{now - expires_at}s ago")
      deregister!
      @blk.call
    end

    def register!
      Einhorn.log_debug("Scheduling a new #{self.time}s timer")
      Einhorn::Event.register_timer(self)
    end

    def deregister!
      Einhorn.log_debug("Nuking timer that expired #{Time.now - self.expires_at}s ago")
      Einhorn::Event.deregister_timer(self)
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
einhorn-0.8.2 lib/einhorn/event/timer.rb
einhorn-0.7.4 lib/einhorn/event/timer.rb
einhorn-0.7.3 lib/einhorn/event/timer.rb
einhorn-0.7.2 lib/einhorn/event/timer.rb
einhorn-0.7.1 lib/einhorn/event/timer.rb
einhorn-0.7.0 lib/einhorn/event/timer.rb
einhorn-0.6.5 lib/einhorn/event/timer.rb
einhorn-0.6.4 lib/einhorn/event/timer.rb
einhorn-0.6.3 lib/einhorn/event/timer.rb
einhorn-0.6.2 lib/einhorn/event/timer.rb
einhorn-0.6.1 lib/einhorn/event/timer.rb
einhorn-0.6.0 lib/einhorn/event/timer.rb
einhorn-0.5.7 lib/einhorn/event/timer.rb
einhorn-0.5.6 lib/einhorn/event/timer.rb
einhorn-0.5.5 lib/einhorn/event/timer.rb
einhorn-0.5.4 lib/einhorn/event/timer.rb
einhorn-0.5.3 lib/einhorn/event/timer.rb
einhorn-0.5.2 lib/einhorn/event/timer.rb
einhorn-0.5.1 lib/einhorn/event/timer.rb
einhorn-0.5.0 lib/einhorn/event/timer.rb