Class: Neovim::Session Private

Inherits:
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: Disconnected

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

#nextObject

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.



40
41
42
43
44
# File 'lib/neovim/session.rb', line 40

def next
  return @pending_messages.shift if @pending_messages.any?

  run { |msg| stop; msg }
end

#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.



83
84
85
# File 'lib/neovim/session.rb', line 83

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.



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

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(Disconnected) 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.



79
80
81
# File 'lib/neovim/session.rb', line 79

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
# File 'lib/neovim/session.rb', line 30

def run(&block)
  block ||= ->(msg) { @pending_messages << msg }

  @running = true

  @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.



87
88
89
90
# File 'lib/neovim/session.rb', line 87

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.



92
93
94
95
# File 'lib/neovim/session.rb', line 92

def stop
  @running = false
  @event_loop.stop
end