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/didChange',
  'textDocument/didClose',
  'textDocument/didOpen',
  'textDocument/didSave',
  'workspace/didChangeConfiguration',
  'workspace/didChangeWatchedFiles',
  'workspace/didCreateFiles',
  'workspace/didChangeWorkspaceFolders',
  'workspace/didDeleteFiles',
  'workspace/didRenameFiles'
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ MessageWorker



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

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

Instance Method Details

#messagesArray<Hash>

pending handle messages



32
33
34
# File 'lib/solargraph/language_server/host/message_worker.rb', line 32

def messages
  @messages ||= []
end

#queue(message) ⇒ void



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

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

#startvoid



55
56
57
58
59
60
61
# File 'lib/solargraph/language_server/host/message_worker.rb', line 55

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

#stopvoid



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

def stop
  @stopped = true
end

#stopped?Boolean



36
37
38
# File 'lib/solargraph/language_server/host/message_worker.rb', line 36

def stopped?
  @stopped
end

#tickvoid



64
65
66
67
68
69
70
71
# File 'lib/solargraph/language_server/host/message_worker.rb', line 64

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