Class: Neovim::AsyncSession
- Inherits:
-
Object
- Object
- Neovim::AsyncSession
- Includes:
- Logging
- Defined in:
- lib/neovim/async_session.rb
Overview
Handles formatting RPC requests and writing them to the MsgpackStream
. This exposes an asynchronous API, in which responses are handled in callbacks.
Instance Method Summary collapse
-
#initialize(msgpack_stream) ⇒ AsyncSession
constructor
A new instance of AsyncSession.
-
#notify(method, *args) ⇒ self
Send an RPC notification.
-
#request(method, *args, &response_cb) ⇒ self
Send an RPC request and enqueue it’s callback to be called when a response is received.
-
#run(session = nil) {|Object| ... } ⇒ void
Run the event loop, yielding received RPC messages to the block.
-
#shutdown ⇒ void
Shut down the event loop.
-
#stop ⇒ void
Stop the event loop.
Constructor Details
#initialize(msgpack_stream) ⇒ AsyncSession
Returns a new instance of AsyncSession.
12 13 14 15 16 |
# File 'lib/neovim/async_session.rb', line 12 def initialize(msgpack_stream) @msgpack_stream = msgpack_stream @request_id = 0 @pending_requests = {} end |
Instance Method Details
#notify(method, *args) ⇒ self
Send an RPC notification. Notifications don’t receive a response from nvim
.
45 46 47 48 |
# File 'lib/neovim/async_session.rb', line 45 def notify(method, *args) @msgpack_stream.write([2, method, args]) self end |
#request(method, *args, &response_cb) ⇒ self
Send an RPC request and enqueue it’s callback to be called when a response is received.
28 29 30 31 32 33 34 35 |
# File 'lib/neovim/async_session.rb', line 28 def request(method, *args, &response_cb) reqid = @request_id @request_id += 1 @msgpack_stream.write([0, reqid, method, args]) @pending_requests[reqid] = response_cb || Proc.new {} self end |
#run(session = nil) {|Object| ... } ⇒ void
This method returns an undefined value.
Run the event loop, yielding received RPC messages to the block. RPC requests and notifications from nvim
will be wrapped in Request
and Notification
objects, respectively, and responses will be passed to their callbacks with optional errors.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/neovim/async_session.rb', line 60 def run(session=nil, &callback) @msgpack_stream.run(session) do |msg| kind, *payload = msg case kind when 0 handle_request(payload, callback) when 1 handle_response(payload) when 2 handle_notification(payload, callback) end end rescue => e fatal("got unexpected error #{e}") debug(e.backtrace.join("\n")) end |
#shutdown ⇒ void
This method returns an undefined value.
Shut down the event loop.
90 91 92 |
# File 'lib/neovim/async_session.rb', line 90 def shutdown @msgpack_stream.shutdown end |
#stop ⇒ void
This method returns an undefined value.
Stop the event loop.
82 83 84 |
# File 'lib/neovim/async_session.rb', line 82 def stop @msgpack_stream.stop end |