Skip to content

Commit

Permalink
refactor: expose event loop as a property rather than a field
Browse files Browse the repository at this point in the history
event loops for Session and Nvim are final and can't be changed.
  • Loading branch information
wookayin committed Oct 16, 2023
1 parent a1347ee commit be5810b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 12 additions & 1 deletion pynvim/api/nvim.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Main Nvim interface."""

import asyncio
import os
import sys
import threading
Expand Down Expand Up @@ -140,7 +142,16 @@ def __init__(
self._err_cb: Callable[[str], Any] = lambda _: None
else:
self._err_cb = err_cb
self.loop = self._session.loop._loop

@property
def loop(self) -> asyncio.AbstractEventLoop:
"""Get the event loop (exposed to rplugins).""" # noqa

# see #294: for python 3.4+, the only available and guaranteed
# implementation of msgpack_rpc BaseEventLoop is the AsyncioEventLoop.
# The underlying asyncio event loop is exposed to rplugins.
# pylint: disable=protected-access
return self._session.loop._loop # type: ignore

def _from_nvim(self, obj: Any, decode: Optional[TDecodeMode] = None) -> Any:
if decode is None:
Expand Down
9 changes: 7 additions & 2 deletions pynvim/msgpack_rpc/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from pynvim.compat import check_async
from pynvim.msgpack_rpc.async_session import AsyncSession
from pynvim.msgpack_rpc.event_loop.base import BaseEventLoop

if sys.version_info < (3, 8):
from typing_extensions import Literal
Expand Down Expand Up @@ -42,7 +43,7 @@ class Notification(NamedTuple):
Message = Union[Request, Notification]


class Session(object):
class Session:

"""Msgpack-rpc session layer that uses coroutines for a synchronous API.
Expand All @@ -59,11 +60,15 @@ def __init__(self, async_session: AsyncSession):
self._pending_messages: Deque[Message] = deque()
self._is_running = False
self._setup_exception: Optional[Exception] = None
self.loop = async_session.loop
self._loop_thread: Optional[threading.Thread] = None
self.error_wrapper: Callable[[Tuple[int, str]], Exception] = \
lambda e: Exception(e[1])

@property
def loop(self) -> BaseEventLoop:
"""Get the underlying msgpack EventLoop."""
return self._async_session.loop

def threadsafe_call(
self, fn: Callable[..., Any], *args: Any, **kwargs: Any
) -> None:
Expand Down

0 comments on commit be5810b

Please sign in to comment.