Sha256: 65bb62a16ce9868d8c41214a4fdc31d07fce4b0ddf3d9dc4bd6e832b5465e860
Contents?: true
Size: 1.17 KB
Versions: 1
Compression:
Stored size: 1.17 KB
Contents
# encoding: utf-8 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 end def self.timeout(secs, interval, &block) (@scheduler ||= new).timeout(secs, interval, &block) end def timeout(secs, interval, &block) return block.() if secs.nil? || secs.to_i.zero? @lock.synchronize do @runner = Thread.new { run_in(secs, interval) } end block.() end def run_in(secs, interval) Thread.current.abort_on_exception = true start = Time.now loop do sleep(interval) runtime = Time.now - start delta = secs - runtime @interval_handler.(delta.round) if delta < 0.0 @timeout_handler.(Thread.current) break end end end end # Scheduler end # Prompt end # TTY
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
tty-prompt-0.12.0 | lib/tty/prompt/timeout.rb |