Skip to content

Commit 11b832e

Browse files
committed
Fix blocking messages thread
1 parent 664813b commit 11b832e

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/neovim_client/rpc.clj

+25-15
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@
7676
(.close input-stream)
7777
(log/info "input and output streams closed"))
7878

79+
(defn- handle-response [message-table msg]
80+
(let [msg-id (id msg)
81+
msg-value (value msg)
82+
{f :fn} (get @message-table msg-id)]
83+
(swap! message-table dissoc msg-id)
84+
(when f (f msg-value))))
85+
86+
(defn- handle-request [component method-table msg]
87+
(let [msg-method (method msg)
88+
msg-id (id msg)
89+
f (get @method-table msg-method method-not-found)
90+
result (f msg)
91+
response-msg (->response-msg msg-id result)]
92+
(send-message-async! component response-msg nil)))
93+
94+
(defn- handle-notify [method-table msg]
95+
(let [f (get @method-table (method msg) method-not-found)]
96+
(f msg)))
97+
7998
(defn new
8099
[input-stream output-stream]
81100
(let [in-chan (create-input-channel input-stream)
@@ -84,7 +103,7 @@
84103
method-table (atom {})
85104
component {:input-stream input-stream
86105
:output-stream output-stream
87-
:out-chan (create-output-channel output-stream)
106+
:out-chan (create-output-channel output-stream)
88107
:in-chan in-chan
89108
:message-table message-table
90109
:method-table method-table}]
@@ -97,26 +116,17 @@
97116
(condp = (msg-type msg)
98117

99118
msg/+response+
100-
(let [f (:fn (get @message-table (id msg)))]
101-
(swap! message-table dissoc (id msg))
102-
;; Don't block the handler to execute this.
103-
(async/thread (when f (f (value msg)))))
119+
(async/thread-call #(handle-response message-table msg))
104120

105121
msg/+request+
106-
(let [f (get @method-table (method msg) method-not-found)
107-
;; TODO - add async/thread here, remove from methods.
108-
result (f msg)]
109-
(send-message-async!
110-
component (->response-msg (id msg) result) nil))
122+
(async/thread-call #(handle-request component method-table msg))
111123

112124
msg/+notify+
113-
(let [f (get @method-table (method msg) method-not-found)
114-
;; TODO - see above.
115-
result (f msg)]))
125+
(async/thread-call #(handle-notify method-table msg)))
116126

117127
(recur)))
118128
(catch Throwable t (log/info
119-
"Exception in message handler, aborting!"
120-
t))))
129+
"Exception in message handler, aborting!"
130+
t))))
121131

122132
component))

0 commit comments

Comments
 (0)