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

#messagesArray<Hash>

pending handle messages

Returns:

  • (Array<Hash>)


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

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



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

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

#startvoid

This method returns an undefined value.



43
44
45
46
47
48
49
# File 'lib/solargraph/language_server/host/message_worker.rb', line 43

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

#stopvoid

This method returns an undefined value.



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

def stop
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


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

def stopped?
  @stopped
end

#tickvoid

This method returns an undefined value.



52
53
54
55
56
57
58
59
# File 'lib/solargraph/language_server/host/message_worker.rb', line 52

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