Class: Teek::RepeatingTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/teek.rb

Overview

A cancellable repeating timer that fires on the main thread.

Created via App#every. Reschedules itself after each tick using Tcl’s after command. The block runs in the event loop, so it must complete quickly to avoid blocking the UI.

Tracks timing drift: if a tick fires significantly late (more than 2x the interval), a warning is printed to stderr. This helps catch blocks that are too slow for the requested interval.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, ms, on_error: nil, &block) ⇒ RepeatingTimer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RepeatingTimer.

Raises:

  • (ArgumentError)


791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File 'lib/teek.rb', line 791

def initialize(app, ms, on_error: nil, &block)
  raise ArgumentError, "interval must be positive, got #{ms}" if ms <= 0

  @app = app
  @interval = ms
  @block = block
  @on_error = on_error
  @cancelled = false
  @after_id = nil
  @last_error = nil
  @late_ticks = 0
  @next_expected = nil
  schedule
end

Instance Attribute Details

#intervalInteger

Returns interval in milliseconds.

Returns:

  • (Integer)

    interval in milliseconds



781
782
783
# File 'lib/teek.rb', line 781

def interval
  @interval
end

#last_errorException? (readonly)

Returns the last error if the timer stopped due to an unhandled exception, nil otherwise.

Returns:

  • (Exception, nil)

    the last error if the timer stopped due to an unhandled exception, nil otherwise



785
786
787
# File 'lib/teek.rb', line 785

def last_error
  @last_error
end

#late_ticksInteger (readonly)

Returns number of ticks that fired late (> 2x interval).

Returns:

  • (Integer)

    number of ticks that fired late (> 2x interval)



788
789
790
# File 'lib/teek.rb', line 788

def late_ticks
  @late_ticks
end

Instance Method Details

#cancelvoid

This method returns an undefined value.

Stop the timer. Safe to call multiple times.



808
809
810
811
812
813
# File 'lib/teek.rb', line 808

def cancel
  return if @cancelled
  @cancelled = true
  @app.after_cancel(@after_id) if @after_id
  @after_id = nil
end

#cancelled?Boolean

Returns true if the timer has been cancelled.

Returns:

  • (Boolean)

    true if the timer has been cancelled



816
817
818
# File 'lib/teek.rb', line 816

def cancelled?
  @cancelled
end