Class: Neovim::AsyncSession

Inherits:
Object
  • Object
show all
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

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.

Examples:

async_session.notify(:vim_input, "jk")

Parameters:

  • method (Symbol, String)

    The RPC method name

  • *args (Array)

    The arguments to the RPC method

Returns:

  • (self)


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.

Examples:

async_session.request(:vim_strwidth, "foobar") do |response|
  $stderr.puts("Got a response #{response}")
end

Parameters:

  • method (Symbol, String)

    The RPC method name

  • *args (Array)

    The arguments to the RPC method

Returns:

  • (self)


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.

Parameters:

  • session (Session) (defaults to: nil)

    The current session

Yields:

  • (Object)

See Also:



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

#shutdownvoid

This method returns an undefined value.

Shut down the event loop.

See Also:



90
91
92
# File 'lib/neovim/async_session.rb', line 90

def shutdown
  @msgpack_stream.shutdown
end

#stopvoid

This method returns an undefined value.

Stop the event loop.

See Also:



82
83
84
# File 'lib/neovim/async_session.rb', line 82

def stop
  @msgpack_stream.stop
end