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 message.

this make check pending message possible, and maybe cancelled to speedup process

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:



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

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>)


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

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



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

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

#startvoid

This method returns an undefined value.



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

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

#stopvoid

This method returns an undefined value.



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

def stop
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/solargraph/language_server/host/message_worker.rb', line 27

def stopped?
  @stopped
end

#tickvoid

This method returns an undefined value.



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

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