Sha256: 72bdbd19fe1b03d5c53c6e8f80104f94ac7d19652d605cee543bbfed4b3d4830

Contents?: true

Size: 1.08 KB

Versions: 7

Compression:

Stored size: 1.08 KB

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

    # yields the passed in block after interval ms, or immediately if on the
    # server.
    def self.client_set_timeout(interval)
      if Volt.in_browser?
        `setTimeout(function() {`
          yield
        `}, interval)`
      else
        yield
      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

7 entries across 7 versions & 1 rubygems

Version Path
volt-0.9.4.pre3 lib/volt/utils/timers.rb
volt-0.9.4.pre2 lib/volt/utils/timers.rb
volt-0.9.4.pre1 lib/volt/utils/timers.rb
volt-0.9.3 lib/volt/utils/timers.rb
volt-0.9.3.pre6 lib/volt/utils/timers.rb
volt-0.9.3.pre5 lib/volt/utils/timers.rb
volt-0.9.3.pre4 lib/volt/utils/timers.rb