Skip to content

Commit 0bdc02b

Browse files
committed
Add log middleware
1 parent 00dcf18 commit 0bdc02b

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/darkleaf/di/core.clj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,19 @@
780780
~@body))
781781
(finally
782782
(.close resource#)))))))
783+
784+
(defn log [built-cb demolished-cb]
785+
(fn [registry]
786+
(fn [key]
787+
(let [factory (registry key)]
788+
(reify p/Factory
789+
(dependencies [_]
790+
(p/dependencies factory))
791+
(build [_ deps]
792+
(let [obj (p/build factory deps)]
793+
(built-cb key deps obj)
794+
obj))
795+
(demolish [_ obj]
796+
(p/demolish factory obj)
797+
(demolished-cb key obj)
798+
nil))))))
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(ns darkleaf.di.tutorial.x-log-test
2+
(:require
3+
[clojure.test :as t]
4+
[darkleaf.di.core :as di]))
5+
6+
(defn a
7+
{::di/kind :component}
8+
[]
9+
:a)
10+
11+
(defn b [{a `a}]
12+
:b)
13+
14+
(defn c
15+
{::di/kind :component}
16+
[{b `b}]
17+
:c)
18+
19+
(t/deftest log
20+
(let [logs (atom [])
21+
built! (fn [key deps obj]
22+
(swap! logs conj [:built key deps obj]))
23+
demolished! (fn [key obj]
24+
(swap! logs conj [:demolished key obj]))
25+
[a b c
26+
:as system] (di/start [`a `b `c]
27+
(di/log built! demolished!))]
28+
(di/stop system)
29+
(t/is (= [[:built `a {} a]
30+
[:built `b {`a a} b]
31+
[:built `c {`b b} c]
32+
[:built ::di/implicit-root {`a a `b b `c c} [a b c]]
33+
[:demolished ::di/implicit-root [a b c]]
34+
[:demolished `c c]
35+
[:demolished `b b]
36+
[:demolished `a a]]
37+
@logs))))

0 commit comments

Comments
 (0)