Sha256: fa8626596a163a117259d302b30489a3e79d6ea0bdbb0074f08b332d363ad695
Contents?: true
Size: 1.7 KB
Versions: 2
Compression:
Stored size: 1.7 KB
Contents
# encoding: utf-8 require 'timers' module TTY class Prompt class Timeout Error = Class.new(RuntimeError) TIMEOUT_HANDLER = proc { |t| t.raise Error, 'timeout expired' } def initialize(options = {}) @timeout_handler = options.fetch(:timeout_handler) { TIMEOUT_HANDLER } @interval_handler = options.fetch(:interval_handler) { proc {} } @lock = Mutex.new @running = true @timers = Timers::Group.new end def self.timeout(time, interval, &block) (@scheduler ||= new).timeout(time, interval, &block) end # Evalute block and time it # # @param [Float] time # the time by which to stop # @param [Float] interval # the interval time for each tick # # @api public def timeout(time, interval, &block) @runner = async_run(time, interval) @running = block.() @runner.join end def async_run(time, interval) Thread.new do Thread.current.abort_on_exception = true start = Time.now interval_timer = @timers.every(interval) do runtime = Time.now - start delta = time - runtime if delta.round >= 0 @interval_handler.(delta.round) end end while @running @lock.synchronize { @timers.wait runtime = Time.now - start delta = time - runtime if delta <= 0.0 @timeout_handler.(Thread.current) break end } end interval_timer.cancel end end end # Scheduler end # Prompt end # TTY
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
tty-prompt-0.13.0 | lib/tty/prompt/timeout.rb |
austb-tty-prompt-0.13.0 | lib/tty/prompt/timeout.rb |