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.
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 |
# File 'lib/teek.rb', line 1097 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.
1087 1088 1089 |
# File 'lib/teek.rb', line 1087 def interval @interval end |
#last_error ⇒ Exception? (readonly)
Returns the last error if the timer stopped due to an unhandled exception, nil otherwise.
1091 1092 1093 |
# File 'lib/teek.rb', line 1091 def last_error @last_error end |
#late_ticks ⇒ Integer (readonly)
Returns number of ticks that fired late (> 2x interval).
1094 1095 1096 |
# File 'lib/teek.rb', line 1094 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.
1114 1115 1116 1117 1118 1119 |
# File 'lib/teek.rb', line 1114 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.
1122 1123 1124 |
# File 'lib/teek.rb', line 1122 def cancelled? @cancelled end |