|
9 | 9 | [lsp4clj.protocols.endpoint :as protocols.endpoint] |
10 | 10 | [lsp4clj.trace :as trace] |
11 | 11 | [promesa.core :as p] |
12 | | - [promesa.protocols]) |
| 12 | + [promesa.protocols :as p.protocols]) |
13 | 13 | (:import |
14 | 14 | (java.util.concurrent CancellationException))) |
15 | 15 |
|
|
31 | 31 | (defprotocol IBlockingDerefOrCancel |
32 | 32 | (deref-or-cancel [this timeout-ms timeout-val])) |
33 | 33 |
|
34 | | -(defrecord PendingRequest [p cancelled? id method started server] |
| 34 | +(defrecord PendingRequest [p id method started server] |
35 | 35 | clojure.lang.IDeref |
36 | 36 | (deref [_] (deref p)) |
37 | 37 | clojure.lang.IBlockingDeref |
38 | 38 | (deref [_ timeout-ms timeout-val] |
39 | 39 | (deref p timeout-ms timeout-val)) |
40 | 40 | IBlockingDerefOrCancel |
41 | 41 | (deref-or-cancel [this timeout-ms timeout-val] |
42 | | - (let [result (deref this timeout-ms ::timeout)] |
43 | | - (if (= ::timeout result) |
| 42 | + (let [result (deref p timeout-ms ::timeout)] |
| 43 | + (if (identical? ::timeout result) |
44 | 44 | (do (future-cancel this) |
45 | 45 | timeout-val) |
46 | 46 | result))) |
47 | 47 | clojure.lang.IPending |
48 | | - (isRealized [_] (realized? p)) |
| 48 | + (isRealized [_] (p/done? p)) |
49 | 49 | java.util.concurrent.Future |
50 | | - (get [this] |
51 | | - (let [result (deref this)] |
52 | | - (if (= ::cancelled result) |
53 | | - (throw (java.util.concurrent.CancellationException.)) |
| 50 | + (get [_] (deref p)) |
| 51 | + (get [_ timeout unit] |
| 52 | + (let [result (deref p (.toMillis unit timeout) ::timeout)] |
| 53 | + (if (identical? ::timeout result) |
| 54 | + (throw (java.util.concurrent.TimeoutException.)) |
54 | 55 | result))) |
55 | | - (get [this timeout unit] |
56 | | - (let [result (deref this (.toMillis unit timeout) ::timeout)] |
57 | | - (case result |
58 | | - ::cancelled (throw (java.util.concurrent.CancellationException.)) |
59 | | - ::timeout (throw (java.util.concurrent.TimeoutException.)) |
60 | | - result))) |
61 | | - (isCancelled [_] @cancelled?) |
62 | | - (isDone [this] (or (.isRealized this) (.isCancelled this))) |
63 | | - (cancel [this _interrupt?] |
64 | | - (if (.isDone this) |
| 56 | + (isCancelled [_] (p/cancelled? p)) |
| 57 | + (isDone [_] (p/done? p)) |
| 58 | + (cancel [_ _interrupt?] |
| 59 | + (if (p/done? p) |
65 | 60 | false |
66 | | - (if (compare-and-set! cancelled? false true) |
67 | | - (do |
68 | | - (protocols.endpoint/send-notification server "$/cancelRequest" {:id id}) |
69 | | - (deliver p ::cancelled) |
70 | | - true) |
71 | | - false)))) |
| 61 | + (do |
| 62 | + (p/cancel! p) |
| 63 | + (protocols.endpoint/send-notification server "$/cancelRequest" {:id id}) |
| 64 | + true))) |
| 65 | + p.protocols/IPromiseFactory |
| 66 | + (-promise [_] p)) |
72 | 67 |
|
73 | 68 | ;; Avoid error: java.lang.IllegalArgumentException: Multiple methods in multimethod 'simple-dispatch' match dispatch value: class lsp4clj.server.PendingRequest -> interface clojure.lang.IPersistentMap and interface clojure.lang.IDeref, and neither is preferred |
74 | 69 | ;; Only when CIDER is running? See https://github.com/thi-ng/color/issues/10 |
|
96 | 91 | Sends `$/cancelRequest` only once, though `lsp4clj.server/deref-or-cancel` or |
97 | 92 | `future-cancel` can be called multiple times." |
98 | 93 | [id method started server] |
99 | | - (map->PendingRequest {:p (promise) |
100 | | - :cancelled? (atom false) |
| 94 | + (map->PendingRequest {:p (p/deferred) |
101 | 95 | :id id |
102 | 96 | :method method |
103 | 97 | :started started |
|
222 | 216 | (async/put! trace-ch [:debug trace-body]))) |
223 | 217 |
|
224 | 218 | (defrecord PendingReceivedRequest [result-promise cancelled?] |
225 | | - promesa.protocols/ICancellable |
| 219 | + p.protocols/ICancellable |
226 | 220 | (-cancel! [_] |
227 | 221 | (p/cancel! result-promise) |
228 | 222 | (reset! cancelled? true)) |
|
330 | 324 | (if-let [{:keys [p started] :as req} (get pending-requests id)] |
331 | 325 | (do |
332 | 326 | (trace this trace/received-response req resp started now) |
333 | | - (deliver p (if error resp result))) |
| 327 | + (p/resolve! p (if error resp result))) |
334 | 328 | (trace this trace/received-unmatched-response resp now))) |
335 | 329 | (catch Throwable e |
336 | 330 | (log-error-receiving this e resp)))) |
|
0 commit comments