|
5 | 5 | [msgpack.core :as msgpack] |
6 | 6 | [neovim-client.message :refer [id value msg-type method params |
7 | 7 | ->response-msg] |
8 | | - :as msg]) |
| 8 | + :as msg]) |
9 | 9 | (:import (java.io DataInputStream DataOutputStream))) |
10 | 10 |
|
11 | 11 | (defn- method-not-found |
|
76 | 76 | (.close input-stream) |
77 | 77 | (log/info "input and output streams closed")) |
78 | 78 |
|
| 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 | + |
79 | 98 | (defn new |
80 | 99 | [input-stream output-stream] |
81 | 100 | (let [in-chan (create-input-channel input-stream) |
|
84 | 103 | method-table (atom {}) |
85 | 104 | component {:input-stream input-stream |
86 | 105 | :output-stream output-stream |
87 | | - :out-chan (create-output-channel output-stream) |
| 106 | + :out-chan (create-output-channel output-stream) |
88 | 107 | :in-chan in-chan |
89 | 108 | :message-table message-table |
90 | 109 | :method-table method-table}] |
|
97 | 116 | (condp = (msg-type msg) |
98 | 117 |
|
99 | 118 | 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)) |
104 | 120 |
|
105 | 121 | 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)) |
111 | 123 |
|
112 | 124 | 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))) |
116 | 126 |
|
117 | 127 | (recur))) |
118 | 128 | (catch Throwable t (log/info |
119 | | - "Exception in message handler, aborting!" |
120 | | - t)))) |
| 129 | + "Exception in message handler, aborting!" |
| 130 | + t)))) |
121 | 131 |
|
122 | 132 | component)) |
0 commit comments