Class: NotepadqqApi

Inherits:
Object
  • Object
show all
Defined in:
lib/notepadqq_api.rb,
lib/notepadqq_api/stubs.rb,
lib/notepadqq_api/message_channel.rb,
lib/notepadqq_api/message_interpreter.rb

Defined Under Namespace

Modules: Stubs Classes: MessageChannel, MessageInterpreter, MessageInterpreterError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket_path = ARGV[0], extension_id = ARGV[1]) ⇒ NotepadqqApi

Returns a new instance of NotepadqqApi.



12
13
14
15
16
17
18
# File 'lib/notepadqq_api.rb', line 12

def initialize(socket_path = ARGV[0], extension_id = ARGV[1])
  @socket_path = socket_path
  @extension_id = extension_id
  
  @message_channel = MessageChannel.new(@socket_path)
  @message_interpreter = MessageInterpreter.new(@message_channel)
end

Instance Attribute Details

#extension_idObject (readonly)

Returns the value of attribute extension_id.



10
11
12
# File 'lib/notepadqq_api.rb', line 10

def extension_id
  @extension_id
end

Instance Method Details

#notepadqqObject

Returns an instance of Notepadqq



68
69
70
71
# File 'lib/notepadqq_api.rb', line 68

def notepadqq
  @nqq ||= Stubs::Notepadqq.new(@message_interpreter, NQQ_STUB_ID);
  return @nqq
end

#on_window_created(&callback) ⇒ Object Also known as: onWindowCreated

Execute a block for every new window. This is preferable to the “newWindow” event of Notepadqq, because it could happen that the extension isn’t ready soon enough to receive the “newWindow” event for the first Window. This method, instead, ensures that the passed block will be called once and only once for each current or future window.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/notepadqq_api.rb', line 41

def on_window_created(&callback)
  captured_windows = []
  
  # Invoke the callback for every currently open window
  notepadqq.windows.each do |window|
    unless captured_windows.include? window
      captured_windows.push window
      callback.call(window)
    end
  end

  # Each time a new window gets opened, invoke the callback.
  # When Notepadqq is starting and initializing all the extensions,
  # we might not be fast enough to receive this event: this is why
  # we manually invoked the callback for every currently open window.
  notepadqq.on(:newWindow) do |window|
    unless captured_windows.include? window
      captured_windows.push window
      callback.call(window)
    end
  end
end

#run_event_loopObject Also known as: runEventLoop

Start reading messages and calling event handlers



21
22
23
24
25
26
27
28
29
30
# File 'lib/notepadqq_api.rb', line 21

def run_event_loop
  yield
  
  while true do
    messages = @message_channel.get_messages
    messages.each do |msg|
      @message_interpreter.process_message(msg)
    end
  end
end