Skip to content

Commit cd52e50

Browse files
darkleafKGOH
andauthored
Add log middleware (#18)
Co-authored-by: KGOH <kgofhedgehogs@gmail.com>
1 parent 71983f5 commit cd52e50

File tree

5 files changed

+106
-48
lines changed

5 files changed

+106
-48
lines changed

notebooks/index.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_add_side_dependency_test.clj")} "Add a side dependency"]]
9191
#_[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_instrument_test.clj")} "Instrument"]]
9292
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_update_key_test.clj")} "Update key"]]
93+
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/x_log_test.clj")} "Log"]]
9394
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/y_graceful_stop_test.clj")} "Graceful stop"]]
9495
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/y_multi_arity_service_test.clj")} "Multi arity service"]]
9596
[:li [:a {:href (clerk/doc-url "test/darkleaf/di/tutorial/z_multi_system_test.clj")} "Multi system"]]

src/darkleaf/di/core.clj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,27 @@
797797
~@body))
798798
(finally
799799
(.close resource#)))))))
800+
801+
(defn log
802+
"A logging middleware.
803+
Calls `:after-build!` and `:after-demolish!` during `di/start`.
804+
Must be the last one in the middleware chain.
805+
Both callbacks are expected to accept
806+
the following arg `{:keys [key object]}`."
807+
[& {:keys [after-build! after-demolish!]
808+
:or {after-build! (fn no-op [_])
809+
after-demolish! (fn no-op [_])}}]
810+
(fn [registry]
811+
(fn [key]
812+
(let [factory (registry key)]
813+
(reify p/Factory
814+
(dependencies [_]
815+
(p/dependencies factory))
816+
(build [_ deps]
817+
(let [obj (p/build factory deps)]
818+
(after-build! {:key key :object obj})
819+
obj))
820+
(demolish [_ obj]
821+
(p/demolish factory obj)
822+
(after-demolish! {:key key :object obj})
823+
nil))))))

test/darkleaf/di/add_side_dependency_test.clj

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,55 @@
2626

2727
(defn- a
2828
{::di/kind :component}
29-
[{log ::log}]
30-
(swap! log conj :a)
29+
[]
3130
:a)
3231

3332
(defn- b
3433
{::di/kind :component}
35-
[{log ::log}]
36-
(swap! log conj :b)
34+
[]
3735
:b)
3836

3937
(defn- c
4038
{::di/kind :component}
41-
[{log ::log}]
42-
(swap! log conj :c)
39+
[]
4340
:c)
4441

4542
(defn- d
4643
{::di/kind :component}
47-
[{log ::log}]
48-
(swap! log conj :d)
44+
[]
4945
:d)
5046

5147
(defn- e
5248
{::di/kind :component}
53-
[{log ::log}]
54-
(swap! log conj :e)
49+
[]
5550
:e)
5651

5752
(defn- f
5853
{::di/kind :component}
59-
[{log ::log}]
60-
(swap! log conj :f)
54+
[]
6155
:f)
6256

6357
(defn- g
6458
{::di/kind :component}
65-
[{log ::log}]
66-
(swap! log conj :g)
59+
[]
6760
:g)
6861

6962
(defn- h
7063
{::di/kind :component}
71-
[{log ::log}]
72-
(swap! log conj :h)
64+
[]
7365
:h)
7466

7567
(defn- side-dep
7668
{::di/kind :component}
77-
[{log ::log}]
78-
(swap! log conj :side-dep))
69+
[]
70+
:side-dep)
7971

8072
(t/deftest bug-array-map->hash-map
81-
(let [log (atom [])]
73+
(let [log (atom [])
74+
after-build! (fn [{:keys [key]}]
75+
(swap! log conj key))]
8276
(with-open [root (di/start ::root
83-
{::log log
84-
::root (di/template
77+
{::root (di/template
8578
{:a (di/ref `a)
8679
:b (di/ref `b)
8780
:c (di/ref `c)
@@ -90,8 +83,9 @@
9083
:f (di/ref `f)
9184
:g (di/ref `g)
9285
:h (di/ref `h)})}
93-
(di/add-side-dependency `side-dep))]
94-
(t/is (= [:a :b :c :d :e :f :g :h :side-dep] @log))
86+
(di/add-side-dependency `side-dep)
87+
(di/log :after-build! after-build!))]
88+
(t/is (= [`a `b `c `d `e `f `g `h `di/new-key#0 `side-dep ::root] @log))
9589
(t/is (= {:a :a
9690
:b :b
9791
:c :c
@@ -103,13 +97,14 @@
10397

10498
(defn- side-dep2
10599
{::di/kind :component}
106-
[{log ::log}]
107-
(swap! log conj :side-dep2))
100+
[]
101+
:side-dep2)
108102

109103
(t/deftest bug-array-map->hash-map-2
110-
(let [log (atom [])]
104+
(let [log (atom [])
105+
after-build! (fn [{:keys [key]}]
106+
(swap! log conj key))]
111107
(with-open [root (di/start ::root
112-
{::log log}
113108
(di/add-side-dependency `side-dep)
114109
{::root (di/template
115110
{:a (di/ref `a)
@@ -120,8 +115,11 @@
120115
:f (di/ref `f)
121116
:g (di/ref `g)
122117
:h (di/ref `h)})}
123-
(di/add-side-dependency `side-dep2))]
124-
(t/is (= [:a :side-dep :b :c :d :e :f :g :h :side-dep2] @log))
118+
(di/add-side-dependency `side-dep2)
119+
(di/log :after-build! after-build!))]
120+
(t/is (= [`di/new-key#0 `side-dep `a
121+
`b `c `d `e `f `g `h
122+
`di/new-key#1 `side-dep2 ::root] @log))
125123
(t/is (= {:a :a
126124
:b :b
127125
:c :c

test/darkleaf/di/dependencies_test.clj

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,33 @@
1111
;; c
1212

1313
(defn root
14-
{::di/stop #(swap! % conj [`root :stopped])}
15-
[{a `a, b `b, log ::log}]
16-
(swap! log conj [`root :built])
17-
log)
14+
{::di/kind :component}
15+
[{a `a, b `b}]
16+
:root)
1817

1918
(defn a
20-
{::di/stop #(swap! % conj [`a :stopped])}
21-
[{c `c, log ::log}]
22-
(swap! log conj [`a :built])
23-
log)
19+
{::di/kind :component}
20+
[{c `c}]
21+
:a)
2422

2523
(defn b
26-
{::di/stop #(swap! % conj [`b :stopped])}
27-
[{c `c, log ::log}]
28-
(swap! log conj [`b :built])
29-
log)
24+
{::di/kind :component}
25+
[{c `c}]
26+
:b)
3027

3128
(defn c
32-
{::di/stop #(swap! % conj [`c :stopped])}
33-
[{log ::log}]
34-
(swap! log conj [`c :built])
35-
log)
29+
{::di/kind :component}
30+
[]
31+
:c)
3632

3733
(t/deftest order-test
38-
(let [log (atom [])]
39-
(-> (di/start `root {::log log})
40-
(di/stop))
34+
(let [log (atom [])
35+
after-build! (fn [{:keys [key]}]
36+
(swap! log conj [key :built]))
37+
after-demolish! (fn [{:keys [key]}]
38+
(swap! log conj [key :stopped]))]
39+
(with-open [root (di/start `root (di/log :after-build! after-build!
40+
:after-demolish! after-demolish!))])
4141
(t/is (= [[`c :built]
4242
[`a :built]
4343
[`b :built]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
after-build! (fn [{:keys [key object]}]
22+
(swap! logs conj [:built key (pr-str object)]))
23+
after-demolish! (fn [{:keys [key object]}]
24+
(swap! logs conj [:demolished key (pr-str object)]))]
25+
(with-open [root (di/start `c (di/log :after-build! after-build!
26+
:after-demolish! after-demolish!))])
27+
(t/is (= [[:built `a ":a"]
28+
[:built `b
29+
"#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"]
30+
[:built `c ":c"]
31+
[:demolished `c ":c"]
32+
[:demolished `b
33+
"#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"]
34+
[:demolished `a ":a"]]
35+
@logs))))

0 commit comments

Comments
 (0)