11"""Asynchronous msgpack-rpc handling in the event loop pipeline."""
22import logging
33from traceback import format_exc
4+ from typing import Any , AnyStr , Callable , Dict
45
6+ from pynvim .msgpack_rpc .msgpack_stream import MsgpackStream
57
68logger = logging .getLogger (__name__ )
79debug , info , warn = (logger .debug , logger .info , logger .warning ,)
810
911
10- class AsyncSession (object ):
12+ # response call back takes two arguments: (err, return_value)
13+ ResponseCallback = Callable [..., None ]
14+
15+
16+ class AsyncSession :
1117
1218 """Asynchronous msgpack-rpc layer that wraps a msgpack stream.
1319
@@ -16,11 +22,11 @@ class AsyncSession(object):
1622 requests and notifications.
1723 """
1824
19- def __init__ (self , msgpack_stream ):
25+ def __init__ (self , msgpack_stream : MsgpackStream ):
2026 """Wrap `msgpack_stream` on a msgpack-rpc interface."""
2127 self ._msgpack_stream = msgpack_stream
2228 self ._next_request_id = 1
23- self ._pending_requests = {}
29+ self ._pending_requests : Dict [ int , ResponseCallback ] = {}
2430 self ._request_cb = self ._notification_cb = None
2531 self ._handlers = {
2632 0 : self ._on_request ,
@@ -33,7 +39,8 @@ def threadsafe_call(self, fn):
3339 """Wrapper around `MsgpackStream.threadsafe_call`."""
3440 self ._msgpack_stream .threadsafe_call (fn )
3541
36- def request (self , method , args , response_cb ):
42+ def request (self , method : AnyStr , args : Any ,
43+ response_cb : ResponseCallback ) -> None :
3744 """Send a msgpack-rpc request to Nvim.
3845
3946 A msgpack-rpc with method `method` and argument `args` is sent to
@@ -89,8 +96,9 @@ def _on_request(self, msg):
8996 # - msg[2]: method name
9097 # - msg[3]: arguments
9198 debug ('received request: %s, %s' , msg [2 ], msg [3 ])
92- self ._request_cb (msg [2 ], msg [3 ], Response (self ._msgpack_stream ,
93- msg [1 ]))
99+ assert self ._request_cb is not None
100+ self ._request_cb (msg [2 ], msg [3 ],
101+ Response (self ._msgpack_stream , msg [1 ]))
94102
95103 def _on_response (self , msg ):
96104 # response to a previous request:
@@ -105,6 +113,7 @@ def _on_notification(self, msg):
105113 # - msg[1]: event name
106114 # - msg[2]: arguments
107115 debug ('received notification: %s, %s' , msg [1 ], msg [2 ])
116+ assert self ._notification_cb is not None
108117 self ._notification_cb (msg [1 ], msg [2 ])
109118
110119 def _on_invalid_message (self , msg ):
@@ -113,15 +122,14 @@ def _on_invalid_message(self, msg):
113122 self ._msgpack_stream .send ([1 , 0 , error , None ])
114123
115124
116- class Response (object ):
117-
125+ class Response :
118126 """Response to a msgpack-rpc request that came from Nvim.
119127
120128 When Nvim sends a msgpack-rpc request, an instance of this class is
121129 created for remembering state required to send a response.
122130 """
123131
124- def __init__ (self , msgpack_stream , request_id ):
132+ def __init__ (self , msgpack_stream : MsgpackStream , request_id : int ):
125133 """Initialize the Response instance."""
126134 self ._msgpack_stream = msgpack_stream
127135 self ._request_id = request_id
0 commit comments