class RBlock::Ticker
RBlock::Ticker
is a simple Go inspired ticker. Usage ticker = RBlock::Ticker.new(0.5)
10.times do
ticker.wait # blocks for 1/2s # do a thing every 1/2s
end
Constants
- NAME
Public Class Methods
new(interval)
click to toggle source
Constructs and starts a ticker @param interval [Numeric] interval duration in seconds
# File lib/rblocks/ticker.rb, line 20 def initialize(interval) @stop_ractor = Ractor.new { loop { Ractor.yield(Ractor.recv, move: true) } } @ractor = Ractor.new(interval, @stop_ractor, name: NAME) do |i, stop| loop do r, = Ractor.select(stop, send(nil)) break if r == stop sleep(i) Ractor.yield(Time.now) end end end
Public Instance Methods
stop!()
click to toggle source
Stops the ticker. Calls to wait
# File lib/rblocks/ticker.rb, line 44 def stop! @stop_ractor.send(:stop) and loop { break unless wait } end
wait() { |self| ... }
click to toggle source
Blocks until ractor yields and than yields and returns
# File lib/rblocks/ticker.rb, line 35 def wait t = @ractor.take yield(self) if block_given? t rescue Ractor::ClosedError raise ErrorTerminated, "ticker stopped" end