Sha256: c716b77b8d992e583c2c63aef2397db971258a69727959a643b5d86c605a248d
Contents?: true
Size: 1.32 KB
Versions: 1
Compression:
Stored size: 1.32 KB
Contents
# frozen_string_literal: true class Async::Timer attr_reader :dealay, :repeat class Error < StandardError; end class AlreadyStarted < Error; end def initialize(delay, # rubocop:disable Metrics/CyclomaticComplexity,Metrics/ParameterLists repeat: true, start: true, run_on_start: false, call: nil, on_error: nil, parent: Async::Task.current, &block) 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 @callable = call || block @on_error = on_error || ->(e) { raise e } @parent = parent self.start if start end def stop = @task.stop(true) def call = @callable.call def active? = @active def restart stop start end def start(run: false) raise AlreadyStarted, "Timer already started" if active? @active = true @task = @parent.async do rescued_call if @run_on_start || run loop do sleep(@delay) rescued_call break unless @repeat ensure @active = false end end end def rescued_call call rescue StandardError => e @on_error.call(e) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
async-tools-0.2.9 | lib/async/timer.rb |