Class: Wisp

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/wisp.rb

Defined Under Namespace

Classes: Session

Constant Summary collapse

DIR =
File.join(File.dirname(File.dirname(__FILE__)), "public")
@@sessions =
{}
@@mutex =
Mutex.new

Class Method Summary collapse

Class Method Details

.error(s) ⇒ Object



77
78
79
# File 'lib/wisp.rb', line 77

def Wisp.error(s)
  EM.next_tick { @@sessions.each_key {|ws| ws.send("E#{ s }") } }
end

.onclose(ws) ⇒ Object



31
32
33
# File 'lib/wisp.rb', line 31

def Wisp.onclose(ws)
  @@sessions.delete(ws)
end

.onmessage(ws) ⇒ Object



35
36
37
38
# File 'lib/wisp.rb', line 35

def Wisp.onmessage(ws)
  s = @@sessions[ws]
  s.ack = s.req
end

.onopen(ws) ⇒ Object



27
28
29
# File 'lib/wisp.rb', line 27

def Wisp.onopen(ws)
  @@sessions[ws] = Session[0, 0, {}, nil, nil]
end

.output(s) ⇒ Object



73
74
75
# File 'lib/wisp.rb', line 73

def Wisp.output(s)
  EM.next_tick { @@sessions.each_key {|ws| ws.send("O#{ s }") } }
end

.trace(event, file, lineno, method, binding, klass) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wisp.rb', line 40

def Wisp.trace(event, file, lineno, method, binding, klass)
  return if event != "line"
  return if file.start_with?(RbConfig::TOPDIR)
  return if file == __FILE__
  return if !File.readable?(file)
  @@mutex.synchronize do
    thread = Thread.current.object_id.to_i
    @@sessions.each do |ws, s|
      if s.prev_thread != thread
        s.prev_thread = thread
        EM.next_tick { ws.send("T%x" % thread) }
      end
      if s.prev_file != file
        s.prev_file = file
        v = { "name" => file }
        unless s.file_sent[file]
          s.file_sent[file] = true
          v["code"] = File.foreach(file).map {|l| l.chomp }
        end
        v = JSON.dump(v)
        EM.next_tick { ws.send("F" + v) }
        s.prev_lineno = nil
      end
      if s.prev_lineno != lineno
        s.prev_lineno = lineno
        s.req += 1
        EM.next_tick { ws.send("L#{ lineno }") }
      end
    end
    sleep 0.1 until @@sessions.all? {|ws, s| s.ack >= s.req }
  end
end

.waitObject



81
82
83
# File 'lib/wisp.rb', line 81

def Wisp.wait
  sleep 0.1 while @@sessions.empty?
end