1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
require "neovim/logging"
require "neovim/connection"
require "neovim/message"
module Neovim
# @api private
class EventLoop
include Logging
def self.tcp(host, port)
new Connection.tcp(host, port)
end
def self.unix(path)
new Connection.unix(path)
end
def self.child(argv)
new Connection.child(argv)
end
def self.stdio
new Connection.stdio
end
def initialize(connection)
@running = false
@shutdown = false
@connection = connection
end
def stop
@running = false
end
def shutdown
@running = false
@shutdown = true
@connection.close
end
def request(request_id, method, *args)
log(:debug) do
{
request_id: request_id,
method: method,
arguments: args
}
end
write(:request, request_id, method, args)
end
def respond(request_id, return_value, error)
log(:debug) do
{
request_id: request_id,
return_value: return_value,
error: error
}
end
write(:response, request_id, error, return_value)
end
def notify(method, *args)
log(:debug) { {name: method, arguments: args} }
write(:notification, method, args)
end
def run
@running = true
last_value = nil
loop do
break unless @running
break if @shutdown
begin
last_value = yield(read)
rescue EOFError, Errno::EPIPE => e
log_exception(:debug, e, __method__)
shutdown
rescue => e
log_exception(:error, e, __method__)
end
end
last_value
ensure
@connection.close if @shutdown
end
def register_types(api, session)
api.types.each do |type, info|
id = info.fetch("id")
klass = Neovim.const_get(type)
log(:debug) { {type: type, id: id} }
@connection.register_type(id) do |index|
klass.new(index, session, api)
end
end
end
private
def read
array = @connection.flush.read
Message.from_array(array)
end
def write(type, *args)
message = Message.public_send(type, *args)
@connection.write(message.to_a)
end
end
end
|