Skip to content

Commit

Permalink
nREPL middleware: only ack the eval after it has finished
Browse files Browse the repository at this point in the history
Some clients seem to be more sensitive to this than others, but this seems
generally to be the right thing to do.

In the process we stop piggiebacking on the default exec function, thus also
bypassing the session thread in favor of scheduling directly on the game thread.
  • Loading branch information
plexus committed Sep 22, 2021
1 parent 98858b2 commit 5bdb56a
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/lambdaisland/witchcraft/nrepl/task_eval.clj
Expand Up @@ -7,30 +7,30 @@
[nrepl.misc :as misc :refer [response-for]]
[nrepl.transport :as t]))

(defn run-task [thunk session]
(wc/run-task
#(if-let [whoami (and (not wc/*default-world*)
(:witchcraft/whoami (meta session)))]
(let [player (wc/player whoami)
world (wc/world player)]
(binding [wc/*default-world* world]
(.run thunk)))
(.run thunk))))
(defn scheduled-exec [session]
(fn [id ^Runnable thunk ^Runnable ack]
(wc/run-task
#(if-let [whoami (and (not wc/*default-world*)
(:witchcraft/whoami (meta session)))]
(let [player (wc/player whoami)
world (wc/world player)]
(binding [wc/*default-world* world]
(.run thunk)
(.run ack)))
(do
(.run thunk)
(.run ack))))))

(defn wrap-eval
[h]
(fn [{:keys [op session] :as msg}]
(when (and (:exec (meta session))
(when (and session
(:exec (meta session))
(not (::decorated (meta session))))
(alter-meta! session
(fn [session-meta]
(-> session-meta
(update :exec
(fn [exec]
(fn [id thunk ack]
(exec id
(partial run-task thunk session)
ack))))
(assoc :exec (scheduled-exec session))
(assoc ::decorated true)))))
(h msg)))

Expand Down

0 comments on commit 5bdb56a

Please sign in to comment.