Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blocking messages thread #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/neovim_client/rpc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@
(.close input-stream)
(log/info "input and output streams closed"))

(defn- handle-response [message-table msg]
(let [msg-id (id msg)
msg-value (value msg)
{f :fn} (get @message-table msg-id)]
(swap! message-table dissoc msg-id)
(when f (f msg-value))))

(defn- handle-request [component method-table msg]
(let [msg-method (method msg)
msg-id (id msg)
f (get @method-table msg-method method-not-found)
result (f msg)
response-msg (->response-msg msg-id result)]
(send-message-async! component response-msg nil)))

(defn- handle-notify [method-table msg]
(let [f (get @method-table (method msg) method-not-found)]
(f msg)))

(defn new
[input-stream output-stream]
(let [in-chan (create-input-channel input-stream)
Expand All @@ -84,7 +103,7 @@
method-table (atom {})
component {:input-stream input-stream
:output-stream output-stream
:out-chan (create-output-channel output-stream)
:out-chan (create-output-channel output-stream)
:in-chan in-chan
:message-table message-table
:method-table method-table}]
Expand All @@ -97,26 +116,17 @@
(condp = (msg-type msg)

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

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

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

(recur)))
(catch Throwable t (log/info
"Exception in message handler, aborting!"
t))))
"Exception in message handler, aborting!"
t))))

component))