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

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ MessageWorker

Returns a new instance of MessageWorker.

Parameters:



11
12
13
14
15
16
# File 'lib/solargraph/language_server/host/message_worker.rb', line 11

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

Instance Method Details

#messagesObject

pending handle messages



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

def messages
  @messages ||= []
end

#queue(message) ⇒ void

This method returns an undefined value.

Parameters:

  • message (Hash)

    The message should be handle. will pass back to Host#receive



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

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

#startObject



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

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

#stopObject



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

def stop
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


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

def stopped?
  @stopped
end

#tickObject



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

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