lib/backport/server/interval.rb in backport-1.0.0 vs lib/backport/server/interval.rb in backport-1.1.0
- old
+ new
@@ -7,21 +7,35 @@
# @param block [Proc] The proc to run on each interval.
# @yieldparam [Interval]
def initialize period, &block
@period = period
@block = block
- @last_time = Time.now
+ @ready = false
end
def starting
- @last_time = Time.now
+ @ready = false
+ run_ready_thread
end
def tick
- now = Time.now
- return unless now - @last_time >= @period
+ return unless @ready
@block.call self
- @last_time = now
+ @ready = false
+ end
+
+ private
+
+ def run_ready_thread
+ Thread.new do
+ until stopped?
+ sleep @period
+ break if stopped?
+ @ready = true
+ changed
+ notify_observers self
+ end
+ end
end
end
end
end