1
1
"""Asynchronous msgpack-rpc handling in the event loop pipeline."""
2
2
import logging
3
3
from traceback import format_exc
4
+ from typing import Any , AnyStr , Callable , Dict
4
5
6
+ from pynvim .msgpack_rpc .msgpack_stream import MsgpackStream
5
7
6
8
logger = logging .getLogger (__name__ )
7
9
debug , info , warn = (logger .debug , logger .info , logger .warning ,)
8
10
9
11
10
- class AsyncSession (object ):
12
+ # response call back takes two arguments: (err, return_value)
13
+ ResponseCallback = Callable [..., None ]
14
+
15
+
16
+ class AsyncSession :
11
17
12
18
"""Asynchronous msgpack-rpc layer that wraps a msgpack stream.
13
19
@@ -16,11 +22,11 @@ class AsyncSession(object):
16
22
requests and notifications.
17
23
"""
18
24
19
- def __init__ (self , msgpack_stream ):
25
+ def __init__ (self , msgpack_stream : MsgpackStream ):
20
26
"""Wrap `msgpack_stream` on a msgpack-rpc interface."""
21
27
self ._msgpack_stream = msgpack_stream
22
28
self ._next_request_id = 1
23
- self ._pending_requests = {}
29
+ self ._pending_requests : Dict [ int , ResponseCallback ] = {}
24
30
self ._request_cb = self ._notification_cb = None
25
31
self ._handlers = {
26
32
0 : self ._on_request ,
@@ -33,7 +39,8 @@ def threadsafe_call(self, fn):
33
39
"""Wrapper around `MsgpackStream.threadsafe_call`."""
34
40
self ._msgpack_stream .threadsafe_call (fn )
35
41
36
- def request (self , method , args , response_cb ):
42
+ def request (self , method : AnyStr , args : Any ,
43
+ response_cb : ResponseCallback ) -> None :
37
44
"""Send a msgpack-rpc request to Nvim.
38
45
39
46
A msgpack-rpc with method `method` and argument `args` is sent to
@@ -89,8 +96,9 @@ def _on_request(self, msg):
89
96
# - msg[2]: method name
90
97
# - msg[3]: arguments
91
98
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 ]))
94
102
95
103
def _on_response (self , msg ):
96
104
# response to a previous request:
@@ -105,6 +113,7 @@ def _on_notification(self, msg):
105
113
# - msg[1]: event name
106
114
# - msg[2]: arguments
107
115
debug ('received notification: %s, %s' , msg [1 ], msg [2 ])
116
+ assert self ._notification_cb is not None
108
117
self ._notification_cb (msg [1 ], msg [2 ])
109
118
110
119
def _on_invalid_message (self , msg ):
@@ -113,15 +122,14 @@ def _on_invalid_message(self, msg):
113
122
self ._msgpack_stream .send ([1 , 0 , error , None ])
114
123
115
124
116
- class Response (object ):
117
-
125
+ class Response :
118
126
"""Response to a msgpack-rpc request that came from Nvim.
119
127
120
128
When Nvim sends a msgpack-rpc request, an instance of this class is
121
129
created for remembering state required to send a response.
122
130
"""
123
131
124
- def __init__ (self , msgpack_stream , request_id ):
132
+ def __init__ (self , msgpack_stream : MsgpackStream , request_id : int ):
125
133
"""Initialize the Response instance."""
126
134
self ._msgpack_stream = msgpack_stream
127
135
self ._request_id = request_id
0 commit comments