-
Notifications
You must be signed in to change notification settings - Fork 4
/
log.cljc
30 lines (27 loc) · 1.15 KB
/
log.cljc
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
(ns darkleaf.effect.middleware.log)
(def ^:private empty-log #?(:clj clojure.lang.PersistentQueue/EMPTY
:cljs cljs.core/PersistentQueue.EMPTY))
(defn wrap-log [continuation]
(let [log (-> continuation
meta
(get ::log empty-log))]
(fn [coeffect]
(if (= ::suspend coeffect)
[[::suspended log] nil]
(let [[effect continuation] (continuation coeffect)
log (conj log {:coeffect coeffect
:next-effect effect})]
(if (some? continuation)
[effect (-> continuation
(vary-meta assoc ::log log)
wrap-log)]
[[::result effect log] nil]))))))
(defn resume [continuation log]
(if (seq log)
(let [{:keys [coeffect next-effect]} (peek log)
[effect continuation] (continuation coeffect)]
(if (not= next-effect effect)
(throw (ex-info "Unexpected effect" {:expected next-effect
:actual effect})))
(recur continuation (pop log)))
continuation))