Class: Neovim::Session Private

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/neovim/session.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Wraps an event loop in a synchronous API using Fibers.

Defined Under Namespace

Classes: Exited

Constant Summary

Constants included from Logging

Logging::TIMESTAMP_FORMAT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, logger, logger=

Constructor Details

#initialize(event_loop) ⇒ Session

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Session


21
22
23
24
25
26
27
28
# File 'lib/neovim/session.rb', line 21

def initialize(event_loop)
  @event_loop = event_loop
  @main_thread = Thread.current
  @main_fiber = Fiber.current
  @response_handlers = Hash.new(-> {})
  @pending_messages = []
  @request_id = 0
end

Instance Attribute Details

#request_id=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


12
13
14
# File 'lib/neovim/session.rb', line 12

def request_id=(value)
  @request_id = value
end

Instance Method Details

#notify(method, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


81
82
83
# File 'lib/neovim/session.rb', line 81

def notify(method, *args)
  @event_loop.notify(method, *args)
end

#request(method, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Make an RPC request and return its response.

If this method is called inside a callback, we are already inside a Fiber handler. In that case, we write to the stream and yield the Fiber. Once the response is received, resume the Fiber and return the result.

If this method is called outside a callback, write to the stream and run the event loop until a response is received. Messages received in the meantime are enqueued to be handled later.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/neovim/session.rb', line 54

def request(method, *args)
  main_thread_only do
    @request_id += 1
    blocking = Fiber.current == @main_fiber

    log(:debug) do
      {
        method_name: method,
        request_id: @request_id,
        blocking: blocking,
        arguments: args
      }
    end

    @event_loop.request(@request_id, method, *args)
    response = blocking ? blocking_response : yielding_response

    raise(Exited) if response.nil?
    raise(response.error) if response.error
    response.value
  end
end

#respond(request_id, value, error = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


77
78
79
# File 'lib/neovim/session.rb', line 77

def respond(request_id, value, error=nil)
  @event_loop.respond(request_id, value, error)
end

#run(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/neovim/session.rb', line 30

def run(&block)
  @running = true

  while (pending = @pending_messages.shift)
    Fiber.new { pending.received(@response_handlers, &block) }.resume
  end

  return unless @running

  @event_loop.run do |message|
    Fiber.new { message.received(@response_handlers, &block) }.resume
  end
end

#shutdownObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


85
86
87
88
# File 'lib/neovim/session.rb', line 85

def shutdown
  @running = false
  @event_loop.shutdown
end

#stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


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

def stop
  @running = false
  @event_loop.stop
end