-
Notifications
You must be signed in to change notification settings - Fork 8
/
task_eval.clj
45 lines (40 loc) · 1.48 KB
/
task_eval.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns lambdaisland.witchcraft.nrepl.task-eval
"Schedule all nREPL eval operations on the server thread.
This way you don't have to wrap a lot of operations in a [[wc/run-task]]."
(:require [lambdaisland.witchcraft :as wc]
[nrepl.middleware :as middleware :refer [set-descriptor!]]
[nrepl.misc :as misc :refer [response-for]]
[nrepl.transport :as t]))
(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 session
(:exec (meta session))
(not (::decorated (meta session))))
(alter-meta! session
(fn [session-meta]
(-> session-meta
(assoc :exec (scheduled-exec session))
(assoc ::decorated true)))))
(h msg)))
(set-descriptor! #'wrap-eval
{:requires #{"clone"}
:expects #{"eval"}
:handles {}})
(comment
(doseq [sess (vals @@#'nrepl.middleware.session/sessions)]
(alter-meta! sess assoc :witchcraft/whoami "sunnyplexus"))
)