Module: Hyperloop::Console

Defined in:
lib/hyperloop/console/engine.rb,
lib/hyperloop/console/version.rb,
lib/hyperloop/console/evaluate.rb,
lib/hyperloop/console/hyper_console.rb,
lib/hyperloop/console/hyper_console.rb,
lib/hyperloop/console/client_components.rb

Defined Under Namespace

Modules: Rails Classes: CodeEditor, CodeHistoryItem, CodeMirror, Config, DebugConsole, Evaluate, Response

Constant Summary collapse

VERSION =
"0.1.4"

Class Method Summary collapse

Class Method Details

.console(title: nil, context: nil) ⇒ Object



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

.console_idObject



10
11
12
13
14
15
# File 'lib/hyperloop/console/hyper_console.rb', line 10

def self.console_id
  @console_id ||= (
    `sessionStorage.getItem('Hyperloop::Console::MainWindowId')` ||
    SecureRandom.uuid.tap { |id| `sessionStorage.setItem('Hyperloop::Console::MainWindowId', #{id})` }
    )
end

.messenger(target_id, kind, m) ⇒ Object



17
18
19
# File 'lib/hyperloop/console/hyper_console.rb', line 17

def self.messenger(target_id, kind, m)
  Response.run(target_id: target_id, kind: kind, message: m.gsub(/\n$/, ''))
end