21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/hyperloop/console/hyper_console.rb', line 21
def self.console(title: nil, context: nil)
title ||= context || 'Hyperloop Application Console'
title = `encodeURIComponent(#{title})`
context = `encodeURIComponent(#{context})`
location = "/hyperloop/hyperloop-debug-console/#{console_id}?context=#{context}&title=#{title}"
window_params = 'width=640,height=460,scrollbars=no,location=0'
`window.open(#{location}, #{"window#{title}"}, #{window_params}).focus()`
@__console_dispatcher__ ||= Evaluate.on_dispatch do |params|
if params.target_id == console_id
cwarn = `console.warn`
clog = `console.log`
cerror = `console.error`
cinfo = `console.info`
`console.warn = function(m) { #{messenger(params.sender_id, :warn, `m`)} }`
`console.log = function(m) { #{messenger(params.sender_id, :log, `m`)} }`
`console.error = function(m) { #{messenger(params.sender_id, :error, `m`)} }`
`console.info = function(m) { #{messenger(params.sender_id, :info, `m`)} }`
begin
message = `eval(#{params.string})`.inspect
Response.run(target_id: params.sender_id, kind: :result, message: message || '')
rescue Exception => e
Response.run(target_id: params.sender_id, kind: :exception, message: e.to_s)
ensure
`console.warn = cwarn`
`console.log = clog`
`console.error = cerror`
`console.info = cinfo`
nil
end
end
end
self
end
|