Class: Neovim::EventLoop
- Inherits:
-
Object
- Object
- Neovim::EventLoop
- Includes:
- Logging
- Defined in:
- lib/neovim/event_loop.rb
Overview
The lowest level interface to reading from and writing to nvim
.
Class Method Summary collapse
-
.child(argv) ⇒ EventLoop
Spawn and connect to a child
nvim
process. -
.stdio ⇒ EventLoop
Connect to the current process’s standard streams.
-
.tcp(host, port) ⇒ EventLoop
Connect to a TCP socket.
-
.unix(path) ⇒ EventLoop
Connect to a UNIX domain socket.
Instance Method Summary collapse
-
#initialize(rd, wr = rd) ⇒ EventLoop
constructor
A new instance of EventLoop.
-
#run {|String| ... } ⇒ void
Run the event loop, reading from the underlying
IO
and yielding received messages to the block. -
#shutdown ⇒ void
Stop the event loop and close underlying IOs.
-
#stop ⇒ void
Stop the event loop.
-
#write(data) ⇒ self
Write data to the underlying
IO
.
Constructor Details
#initialize(rd, wr = rd) ⇒ EventLoop
Returns a new instance of EventLoop.
48 49 50 51 |
# File 'lib/neovim/event_loop.rb', line 48 def initialize(rd, wr=rd) @rd, @wr = rd, wr @running = false end |
Class Method Details
.child(argv) ⇒ EventLoop
Spawn and connect to a child nvim
process.
34 35 36 37 38 |
# File 'lib/neovim/event_loop.rb', line 34 def self.child(argv) argv = [ENV.fetch("NVIM_EXECUTABLE", "nvim"), "--embed"] | argv io = IO.popen(argv, "rb+") new(io) end |
.stdio ⇒ EventLoop
Connect to the current process’s standard streams. This is used to promote the current process to a Ruby plugin host.
44 45 46 |
# File 'lib/neovim/event_loop.rb', line 44 def self.stdio new(STDIN, STDOUT) end |
.tcp(host, port) ⇒ EventLoop
Connect to a TCP socket.
16 17 18 19 |
# File 'lib/neovim/event_loop.rb', line 16 def self.tcp(host, port) socket = TCPSocket.new(host, port) new(socket) end |
.unix(path) ⇒ EventLoop
Connect to a UNIX domain socket.
25 26 27 28 |
# File 'lib/neovim/event_loop.rb', line 25 def self.unix(path) socket = UNIXSocket.new(path) new(socket) end |
Instance Method Details
#run {|String| ... } ⇒ void
This method returns an undefined value.
Run the event loop, reading from the underlying IO
and yielding received messages to the block.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/neovim/event_loop.rb', line 79 def run @running = true loop do break unless @running = @rd.readpartial(1024 * 16) debug("received #{.inspect}") yield if block_given? end rescue EOFError warn("got EOFError") rescue => e fatal("got unexpected error #{e}") debug(e.backtrace.join("\n")) end |
#shutdown ⇒ void
This method returns an undefined value.
Stop the event loop and close underlying IOs.
105 106 107 108 109 |
# File 'lib/neovim/event_loop.rb', line 105 def shutdown stop [@rd, @wr].each(&:close) rescue IOError end |
#stop ⇒ void
This method returns an undefined value.
Stop the event loop.
98 99 100 |
# File 'lib/neovim/event_loop.rb', line 98 def stop @running = false end |
#write(data) ⇒ self
Write data to the underlying IO
. This will block until all the data has been written.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/neovim/event_loop.rb', line 58 def write(data) start = 0 size = data.size debug("writing #{data.inspect}") begin while start < size start += @wr.write_nonblock(data[start..-1]) end self rescue IO::WaitWritable IO.select(nil, [@wr], nil, 1) retry end end |