Class: Teek::RepeatingTimer
- Inherits:
-
Object
- Object
- Teek::RepeatingTimer
- 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.
Instance Attribute Summary collapse
-
#interval ⇒ Integer
Interval in milliseconds.
-
#last_error ⇒ Exception?
readonly
The last error if the timer stopped due to an unhandled exception, nil otherwise.
-
#late_ticks ⇒ Integer
readonly
Number of ticks that fired late (> 2x interval).
Instance Method Summary collapse
-
#cancel ⇒ void
Stop the timer.
-
#cancelled? ⇒ Boolean
True if the timer has been cancelled.
-
#initialize(app, ms, on_error: nil, &block) ⇒ RepeatingTimer
constructor
private
A new instance of RepeatingTimer.
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.
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
#interval ⇒ Integer
Returns interval in milliseconds.
781 782 783 |
# File 'lib/teek.rb', line 781 def interval @interval end |
#last_error ⇒ Exception? (readonly)
Returns 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_ticks ⇒ Integer (readonly)
Returns 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
#cancel ⇒ void
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.
816 817 818 |
# File 'lib/teek.rb', line 816 def cancelled? @cancelled end |