Sha256: 69d6a925b116239cc5236262dd36d4b191d52c787f0895d2135bf78e9fa79b91
Contents?: true
Size: 1.1 KB
Versions: 1
Compression:
Stored size: 1.1 KB
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, call: nil, &block) # rubocop:disable Metrics/ParameterLists callables = [call, block] raise ArgumentError, "either block or call: must be given" if callables.all?(&:nil?) || callables.none?(&:nil?) @delay = delay @repeat = repeat @run_on_start = run_on_start @parent = parent @callable = call || block self.start if start end def stop = @task.stop def call = @callable.call def active? = @active def restart stop @task.wait start end def start(run: false) raise AlreadyStarted, "Timer already started" if active? @active = true @task = @parent.async do call if @run_on_start || run loop do 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.7 | lib/async/timer.rb |