Sha256: de19fc31aa1d299070d3d23530a438d7e091141c5d1edccf7f819b1adc2eaa18

Contents?: true

Size: 969 Bytes

Versions: 1

Compression:

Stored size: 969 Bytes

Contents

# frozen_string_literal: true

module Async::App::TimerComponent
  def self.included(base)
    base.include(Async::App::Component)
    base.include(InstanceMethods)
  end

  module InstanceMethods
    def init!
      super
      @timer = Async::Timer.new(run_on_start:, start: false, on_error: method(:on_error)) { tick! }
    end

    def run!
      return if @timer.active?

      @timer.start(interval)
      info { "Started. Interval = #{interval}" }
    end

    def stop!
      @timer&.stop
      super
    end

    # TimerComponent - specific methods
    def tick!
      debug { "Started" }
      on_tick
      debug { "Finished" }
    end

    def restart!
      @timer.restart(interval)
      info { "Restarted. Polling interval=#{interval}" }
    end

    private

    def interval = raise NotImplementedError
    def run_on_start = raise NotImplementedError
    def on_tick = raise NotImplementedError
    def on_error(exception) = raise exception
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
async-tools-0.2.10 lib/async/app/timer_component.rb