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

Constant Summary collapse

NQQ_STUB_ID =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socketPath = ARGV[0], extensionId = ARGV[1]) ⇒ NotepadqqApi

Returns a new instance of NotepadqqApi.



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

def initialize(socketPath = ARGV[0], extensionId = ARGV[1])
  @socketPath = socketPath
  @extensionId = extensionId
  
  @messageChannel = MessageChannel.new(@socketPath)
  @messageInterpreter = MessageInterpreter.new(@messageChannel)
end

Instance Attribute Details

#extensionIdObject (readonly)

Returns the value of attribute extensionId.



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

def extensionId
  @extensionId
end

Instance Method Details

#notepadqqObject

Returns an instance of Notepadqq



62
63
64
65
# File 'lib/notepadqq_api.rb', line 62

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

#onWindowCreated(&callback) ⇒ Object

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.



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

def onWindowCreated(&callback)
  capturedWindows = []
  
  # Invoke the callback for every currently open window
  notepadqq.windows.each do |window|
    unless capturedWindows.include? window
      capturedWindows.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 capturedWindows.include? window
      capturedWindows.push window
      callback.call(window)
    end
  end
end

#runEventLoopObject

Start reading messages and calling event handlers



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

def runEventLoop
  yield
  
  while true do
    messages = @messageChannel.getMessages
    messages.each do |msg|
      @messageInterpreter.processMessage(msg)
    end
  end
  
end