Sha256: 61152405c0165d48da8bf7cecb57d903b713a8806e0b59e60411f8a081c301b7

Contents?: true

Size: 481 Bytes

Versions: 9

Compression:

Stored size: 481 Bytes

Contents

module Chrono
  class Trigger
    attr_reader :block, :source

    def initialize(source, &block)
      @source = source
      @block = block || -> {}
    end

    def once
      wait
      call
    end

    def run
      loop { once }
    end

    private

    def call
      block.call
    end

    def iterator
      @iterator ||= Iterator.new(source)
    end

    def wait
      sleep(period)
    end

    def period
      iterator.next - Time.now + 1.second
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
chrono-0.6.0 lib/chrono/trigger.rb
chrono-0.5.0 lib/chrono/trigger.rb
chrono-0.4.0 lib/chrono/trigger.rb
chrono-0.3.0 lib/chrono/trigger.rb
chrono-0.2.0 lib/chrono/trigger.rb
chrono-0.1.0 lib/chrono/trigger.rb
chrono-0.0.6 lib/chrono/trigger.rb
chrono-0.0.5 lib/chrono/trigger.rb
chrono-0.0.4 lib/chrono/trigger.rb