Class: Solargraph::LanguageServer::Host::MessageWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/language_server/host/message_worker.rb

Overview

A serial worker Thread to handle incoming messages.

Constant Summary collapse

UPDATE_METHODS =
[
  'textDocument/didOpen',
  'textDocument/didChange',
  'workspace/didChangeWatchedFiles'
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ MessageWorker

Returns a new instance of MessageWorker.

Parameters:



16
17
18
19
20
21
# File 'lib/solargraph/language_server/host/message_worker.rb', line 16

def initialize(host)
  @host = host
  @mutex = Mutex.new
  @resource = ConditionVariable.new
  @stopped = true
end

Instance Method Details

#messagesArray<Hash>

pending handle messages

Returns:

  • (Array<Hash>)


25
26
27
# File 'lib/solargraph/language_server/host/message_worker.rb', line 25

def messages
  @messages ||= []
end

#queue(message) ⇒ void

This method returns an undefined value.

Parameters:

  • message (Hash)

    The message to handle. Will be forwarded to Host#receive



40
41
42
43
44
45
# File 'lib/solargraph/language_server/host/message_worker.rb', line 40

def queue(message)
  @mutex.synchronize do
    messages.push(message)
    @resource.signal
  end
end

#startvoid

This method returns an undefined value.



48
49
50
51
52
53
54
# File 'lib/solargraph/language_server/host/message_worker.rb', line 48

def start
  return unless @stopped
  @stopped = false
  Thread.new do
    tick until stopped?
  end
end

#stopvoid

This method returns an undefined value.



34
35
36
# File 'lib/solargraph/language_server/host/message_worker.rb', line 34

def stop
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/solargraph/language_server/host/message_worker.rb', line 29

def stopped?
  @stopped
end

#tickvoid

This method returns an undefined value.



57
58
59
60
61
62
63
64
# File 'lib/solargraph/language_server/host/message_worker.rb', line 57

def tick
  message = @mutex.synchronize do
    @resource.wait(@mutex) if messages.empty?
    next_message
  end
  handler = @host.receive(message)
  handler&.send_response
end