Sha256: 103cd0be115bd8ef574d474fdd39dadd8ff3493d022c1bc0389519ba50e89294

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 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
      ensure
        @active = false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
async-tools-0.1.8 lib/async/timer.rb