public
Description: A process execution library which doesn't suck.
Clone URL: git://github.com/codahale/ropen.git
ropen / lib / ropen / events.rb
100644 32 lines (25 sloc) 0.491 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require "ropen"
 
# TODO: document me
# TODO: would halting the callback chain make any sense?
 
class Ropen::Events
  attr_reader :callbacks
  
  def initialize
    @callbacks = []
  end
  
  def on_output(&block)
    @callbacks << block
  end
  
  def run(stream)
    @thread = Thread.new do
      until stream.eof?
        data = stream.readpartial(1024)
        @callbacks.each do |e|
          e.call(data)
        end
      end
    end
  end
  
  def finish
    @thread.join
  end
  
end