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

.client_set_timeout(interval) ⇒ Object

yields the passed in block after interval ms, or immediately if on the server.



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

def self.client_set_timeout(interval)
  if Volt.in_browser?
    `setTimeout(function() {`
      yield
    `}, interval)`
  else
    yield
  end
end

.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.



31
32
33
34
35
36
37
38
39
# File 'lib/volt/utils/timers.rb', line 31

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

  if tick_timers
    # clear
    Thread.current['tick_timers'] = nil
    tick_timers.each(&:call)
  end
end

.next_tick(&block) ⇒ Object

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



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

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