Sha256: fcabefe7c4b19dbd4869e067cde0486485fc4e7d46cad5ceaeafbe204e32c8be

Contents?: true

Size: 831 Bytes

Versions: 4

Compression:

Stored size: 831 Bytes

Contents

module Volt
  # The timers class provides useful methods for working in an asynchronus environment.
  class Timers
    # next tick (same as setImmediate) calls the block of code after any currently
    # running code is finished.
    def self.next_tick(&block)
      if Volt.in_browser?
        `setImmediate(function() {`
        yield
        `})`
      else
        tick_timers = (Thread.current['tick_timers'] ||= [])
        tick_timers << block
      end
    end

    # On the server, we need to manually flush next tick timers.
    # This is done automatically in the console after each enter.
    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
volt-0.9.3.pre3 lib/volt/utils/timers.rb
volt-0.9.3.pre2 lib/volt/utils/timers.rb
volt-0.9.3.pre1 lib/volt/utils/timers.rb
volt-0.9.2 lib/volt/utils/timers.rb