Class: IO

Inherits:
Object
  • Object
show all
Defined in:
lib/apple_tv_converter/io_patch.rb

Overview

Monkey Patch timeout support into the IO class

Instance Method Summary collapse

Instance Method Details

#each_with_timeout(pid, seconds, sep_string = $/) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/apple_tv_converter/io_patch.rb', line 21

def each_with_timeout(pid, seconds, sep_string=$/)
  sleeping_queue = Queue.new
  thread = nil

  timer_set = lambda do
    thread = new_thread(pid) { AppleTvConverter::Timer.timeout(seconds) { sleeping_queue.pop } }
  end

  timer_cancel = lambda do
    thread.kill if thread rescue nil
  end

  timer_set.call
  each(sep_string) do |buffer|
    timer_cancel.call
    yield buffer
    timer_set.call
  end
ensure
  timer_cancel.call
end