Class: Flok::InteractiveServer

Inherits:
Object
  • Object
show all
Defined in:
lib/flok/interactive.rb

Instance Method Summary collapse

Constructor Details

#initialize(app_js_path) ⇒ InteractiveServer

Returns a new instance of InteractiveServer.



9
10
11
12
13
14
15
16
# File 'lib/flok/interactive.rb', line 9

def initialize app_js_path
  @app_js_path = app_js_path
  @ctx = V8::Context.new
  @ctx.load app_js_path

  inject_ruby_into_js
  inject_dispatch_shims
end

Instance Method Details

#begin_pipeObject

Will take over any remaining IO



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/flok/interactive.rb', line 19

def begin_pipe
  loop do
    begin
      q = JSON.parse(gets)
      @ctx[:int_dispatch].call(q)
    rescue Errno::EIO
      #Can't say anything here, we don't have a pipe
    rescue V8::Error => e
      $stderr.puts "\n--------------------------------------------------" 
      $stderr.puts "V8 Boo-Boo for #{@app_js_path}"
      $stderr.puts "--------------------------------------------------\n"
      $stderr.puts e.message
      $stderr.puts "--------------------------------------------------\n" 
      exit 1
    end
  end
end

#inject_dispatch_shimsObject

Make calls to if_dispatch go to $stdout, make $stdin call int_dispatch



49
50
51
52
53
54
55
# File 'lib/flok/interactive.rb', line 49

def inject_dispatch_shims
  @ctx.eval %{
    function if_dispatch(q) {
      write(JSON.stringify(q));
    }
  }
end

#inject_ruby_into_jsObject

JS Functions#####################################################



38
39
40
41
42
43
44
45
46
# File 'lib/flok/interactive.rb', line 38

def inject_ruby_into_js 
  #Output to stdout
  @ctx["write"] = lambda do |this, str|
    str.each_line do |line|
      $stdout.puts line
      $stdout.flush
    end
  end
end