Skip to content

Commit 65cfc39

Browse files
committed
Delegate cancellation to promise
1 parent 2a98bf7 commit 65cfc39

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/lsp4clj/server.clj

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
(defprotocol IBlockingDerefOrCancel
3232
(deref-or-cancel [this timeout-ms timeout-val]))
3333

34-
(defrecord PendingRequest [p id method started server]
34+
(defrecord PendingRequest [p id method started]
3535
clojure.lang.IDeref
3636
(deref [_] (deref p))
3737
clojure.lang.IBlockingDeref
@@ -56,12 +56,8 @@
5656
(isCancelled [_] (p/cancelled? p))
5757
(isDone [_] (p/done? p))
5858
(cancel [_ _interrupt?]
59-
(if (p/done? p)
60-
false
61-
(do
62-
(p/cancel! p)
63-
(protocols.endpoint/send-notification server "$/cancelRequest" {:id id})
64-
true)))
59+
(p/cancel! p)
60+
(p/cancelled? p))
6561
p.protocols/IPromiseFactory
6662
(-promise [_] p))
6763

@@ -91,11 +87,17 @@
9187
Sends `$/cancelRequest` only once, though `lsp4clj.server/deref-or-cancel` or
9288
`future-cancel` can be called multiple times."
9389
[id method started server]
94-
(map->PendingRequest {:p (p/deferred)
95-
:id id
96-
:method method
97-
:started started
98-
:server server}))
90+
;; Chaining `(-> (p/deferred) (p/catch ...))` seems like it should work, but
91+
;; doesn't.
92+
(let [p (p/deferred)]
93+
(p/catch p CancellationException
94+
(fn [ex]
95+
(protocols.endpoint/send-notification server "$/cancelRequest" {:id id})
96+
(p/rejected ex)))
97+
(map->PendingRequest {:p p
98+
:id id
99+
:method method
100+
:started started})))
99101

100102
(defn ^:private format-error-code [description error-code]
101103
(let [{:keys [code message]} (lsp.errors/by-key error-code)]

test/lsp4clj/server_test.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@
290290
(h/assert-take output-ch)
291291
(is (future-cancel req))
292292
(is (= "$/cancelRequest" (:method (h/assert-take output-ch))))
293-
(is (not (future-cancel req)))
293+
(is (future-cancel req))
294294
(h/assert-no-take output-ch)
295295
(server/shutdown server)))
296296

@@ -402,9 +402,12 @@
402402
:input-ch input-ch})
403403
_ (server/start server nil)
404404
req (p/promise (server/send-request server "req" {:body "foo"}))]
405+
(h/assert-take output-ch)
405406
(p/cancel! req)
406407
(is (p/done? req))
407408
(is (p/cancelled? req))
409+
(is (= {:jsonrpc "2.0", :method "$/cancelRequest", :params {:id 1}}
410+
(h/assert-take output-ch)))
408411
(server/shutdown server))))
409412

410413
(def fixed-clock

0 commit comments

Comments
 (0)