Class: Volt::Timers

Inherits:
Object show all
Defined in:
lib/volt/utils/timers.rb

Overview

The timers class provides useful methods for working in an asynchronus environment.

Class Method Summary collapse

Class Method Details

.flush_next_tick_timers!Object

On the server, we need to manually flush next tick timers. This is done automatically in the console after each enter.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/volt/utils/timers.rb', line 20

def self.flush_next_tick_timers!
  tick_timers = Thread.current['tick_timers']

  if tick_timers
    # clear
    Thread.current['tick_timers'] = nil
    tick_timers.each do |timer|
      # Run the timer
      timer.call
    end
  end
end

.next_tick(&block) ⇒ Object

next tick (same as setImmediate) calls the block of code after any currently running code is finished.



7
8
9
10
11
12
13
14
15
16
# File 'lib/volt/utils/timers.rb', line 7

def self.next_tick(&block)
  if Volt.in_browser?
    `setImmediate(function() {`
      yield
    `})`
  else
    tick_timers = (Thread.current['tick_timers'] ||= [])
    tick_timers << block
  end
end