Class: Tuile::EventQueue::Ticker
- Inherits:
-
Object
- Object
- Tuile::EventQueue::Ticker
- Defined in:
- lib/tuile/event_queue.rb,
sig/tuile.rbs
Overview
Handle returned by #tick. Cancel a running ticker via #cancel.
Internally wraps a Concurrent::TimerTask whose firing posts a single
submit-block to the owning Tuile::EventQueue; the user's block therefore
always runs on the event-loop thread and may freely mutate UI. If the
user block raises, the Ticker auto-cancels and the exception is
re-raised so it flows through the loop's normal error handling
(Screen#on_error for the default Tuile setup).
Instance Method Summary collapse
-
#cancel ⇒ void
Stops the ticker.
-
#cancelled? ⇒ Boolean
@return — true once #cancel has been called.
-
#fire ⇒ void
Runs on the event-loop thread.
-
#initialize(event_queue, interval, block) ⇒ Ticker
constructor
@param
event_queue— queue to dispatch tick calls onto.
Constructor Details
#initialize(event_queue, interval, block) ⇒ Ticker
@param event_queue — queue to dispatch tick calls onto.
@param interval — seconds between firings (positive).
@param block — called as block.call(tick_count) on each fire.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/tuile/event_queue.rb', line 289 def initialize(event_queue, interval, block) @event_queue = event_queue @block = block @tick = 0 # AtomicBoolean rather than a plain ivar: cancel may run on any # thread (caller code, the event-loop thread from inside the block, # or the IO executor on an error path), and we want both a CAS-style # one-shot guard against double-shutdown and well-defined visibility # on non-MRI Rubies. @cancelled = Concurrent::AtomicBoolean.new(false) @timer = Concurrent::TimerTask.new(execution_interval: interval) do @event_queue.submit { fire } end @timer.execute end |
Instance Method Details
#cancel ⇒ void
This method returns an undefined value.
Stops the ticker. Idempotent and safe to call from any thread, including from inside the tick block. Any tick already queued on the event loop at the moment of cancellation is dropped before the user block runs.
313 314 315 316 317 |
# File 'lib/tuile/event_queue.rb', line 313 def cancel return unless @cancelled.make_true # CAS: only the winner shuts down @timer.shutdown end |
#cancelled? ⇒ Boolean
@return — true once #cancel has been called.
306 |
# File 'lib/tuile/event_queue.rb', line 306 def cancelled? = @cancelled.true? |
#fire ⇒ void
This method returns an undefined value.
Runs on the event-loop thread.
323 324 325 326 327 328 329 330 331 |
# File 'lib/tuile/event_queue.rb', line 323 def fire return if @cancelled.true? @block.call(@tick) @tick += 1 rescue StandardError cancel raise end |