Class: Pry::Visualizer

Inherits:
Object
  • Object
show all
Defined in:
lib/pry/visualizer.rb,
lib/pry/visualizer/version.rb

Overview

Manages a websocket server

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initializeVisualizer

Returns a new instance of Visualizer.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pry/visualizer.rb', line 14

def initialize

  @commands = []

  Pry.hooks.add_hook(:when_started, :viz_init) do |output, _binding, _pry|
    start_server
  end

  Pry.hooks.add_hook(:before_eval, :viz_before) do |code, _pry|
    @commands << { id: @commands.size, code: code, start: Time.now }
  end

  Pry.hooks.add_hook(:after_eval, :viz_after) do |result, _pry|
    @commands[-1].merge!(
      end:    Time.now,
      result: result
    )
    # send data to websocket
    @sock && begin
      @sock.send_data(JSON.generate({ command: @commands[-1] }))
    end
  end
end

Instance Method Details

#call(env) ⇒ Object

Rack App



43
44
45
46
47
48
49
50
# File 'lib/pry/visualizer.rb', line 43

def call(env)
  @sock = Tubesock.hijack(env)
  @sock.onclose do
    ActiveRecord::Base.clear_active_connections! if defined? ActiveRecord
  end
  @sock.listen
  [-1, {}, []]
end

#start_serverObject



38
39
40
# File 'lib/pry/visualizer.rb', line 38

def start_server
  Thread.new { Rack::Handler::Puma.run(self, Port: ENV['PORT'] || 3000) }
end