Class: Tuile::EventQueue::Ticker

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

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

Constructor Details

#initialize(event_queue, fps, block) ⇒ Ticker

Returns a new instance of Ticker.

Parameters:

  • event_queue (EventQueue)

    queue to dispatch tick calls onto.

  • fps (Numeric)

    firings per second (positive).

  • block (Proc)

    called as ‘block.call(tick_count)` on each fire.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/tuile/event_queue.rb', line 207

def initialize(event_queue, fps, 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: 1.0 / fps) do
    @event_queue.submit { fire }
  end
  @timer.execute
end

Instance Method Details

#cancelvoid

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.



231
232
233
234
235
# File 'lib/tuile/event_queue.rb', line 231

def cancel
  return unless @cancelled.make_true # CAS: only the winner shuts down

  @timer.shutdown
end

#cancelled?Boolean

Returns true once #cancel has been called.

Returns:

  • (Boolean)

    true once #cancel has been called.



224
# File 'lib/tuile/event_queue.rb', line 224

def cancelled? = @cancelled.true?