Sha256: f6c710dfb717e2fe9b977004ae83cd0d3ae46cafb24f1e46e11d361cdc9ec104
Contents?: true
Size: 1014 Bytes
Versions: 1
Compression:
Stored size: 1014 Bytes
Contents
# frozen_string_literal: true class Async::Timer attr_reader :dealay, :repeat class Error < StandardError; end class AlreadyStarted < Error; end def initialize(delay, repeat: true, start: true, run_on_start: false, parent: Async::Task.current, &block) raise ArgumentError, "Block must be given" if block.nil? @delay = delay @repeat = repeat @run_on_start = run_on_start @parent = parent @block = block self.start if start end def stop = @task.stop def call = @block.call def active? = @active def restart stop @task.wait start end def start(run: false) raise AlreadyStarted, "Timer already started" if active? run_on_start = @run_on_start || run @active = true call if run_on_start @task = @parent.async do loop do @parent.sleep(@delay) call break unless @repeat rescue Async::Stop, Async::TimeoutError break ensure @active = false end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
async-tools-0.1.3 | lib/async/timer.rb |