Class: Volt::Timers
Overview
The timers class provides useful methods for working in an asynchronus environment.
Class Method Summary collapse
- .clear_timeout(timeout_id) ⇒ Object
-
.client_set_timeout(interval) ⇒ Object
yields the passed in block after interval ms, or immediately if on the server.
-
.flush_next_tick_timers! ⇒ Object
On the server, we need to manually flush next tick timers.
-
.next_tick(&block) ⇒ Object
next tick (same as setImmediate) calls the block of code after any currently running code is finished.
Class Method Details
.clear_timeout(timeout_id) ⇒ Object
35 36 37 38 39 |
# File 'lib/volt/utils/timers.rb', line 35 def self.clear_timeout(timeout_id) if Volt.in_browser? `clearTimeout(timeout_id);` end end |
.client_set_timeout(interval) ⇒ Object
yields the passed in block after interval ms, or immediately if on the server.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/volt/utils/timers.rb', line 21 def self.client_set_timeout(interval) if Volt.in_browser? timer_id = nil `timer_id = setTimeout(function() {` yield `}, interval);` # return the timer_id timer_id 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.
43 44 45 46 47 48 49 50 51 |
# File 'lib/volt/utils/timers.rb', line 43 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 |