Skip to content

Commit a1728da

Browse files
darkleafKGOH
andcommitted
wip4
Co-authored-by: KGOH <kgofhedgehogs@gmail.com>
1 parent bec5793 commit a1728da

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

src/darkleaf/di/core.clj

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,15 @@
798798
(finally
799799
(.close resource#)))))))
800800

801-
(defn log [built-cb demolished-cb]
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 [_])}}]
802810
(fn [registry]
803811
(fn [key]
804812
(let [factory (registry key)]
@@ -807,9 +815,9 @@
807815
(p/dependencies factory))
808816
(build [_ deps]
809817
(let [obj (p/build factory deps)]
810-
(built-cb key obj)
818+
(after-build! {:key key :object obj})
811819
obj))
812820
(demolish [_ obj]
813821
(p/demolish factory obj)
814-
(demolished-cb key obj)
822+
(after-demolish! {:key key :object obj})
815823
nil))))))

test/darkleaf/di/add_side_dependency_test.clj

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,11 @@
7070
:side-dep)
7171

7272
(t/deftest bug-array-map->hash-map
73-
(let [log (atom [])
74-
built! (fn [key obj]
75-
(swap! log conj key))
76-
demolish! (fn [key obj])]
73+
(let [log (atom [])
74+
after-build! (fn [{:keys [key]}]
75+
(swap! log conj key))]
7776
(with-open [root (di/start ::root
78-
{::log log
79-
::root (di/template
77+
{::root (di/template
8078
{:a (di/ref `a)
8179
:b (di/ref `b)
8280
:c (di/ref `c)
@@ -86,7 +84,7 @@
8684
:g (di/ref `g)
8785
:h (di/ref `h)})}
8886
(di/add-side-dependency `side-dep)
89-
(di/log built! demolish!))]
87+
(di/log :after-build! after-build!))]
9088
(t/is (= [`a `b `c `d `e `f `g `h `di/new-key#0 `side-dep ::root] @log))
9189
(t/is (= {:a :a
9290
:b :b
@@ -103,10 +101,9 @@
103101
:side-dep2)
104102

105103
(t/deftest bug-array-map->hash-map-2
106-
(let [log (atom [])
107-
built! (fn [key obj]
108-
(swap! log conj key))
109-
demolish! (fn [key obj])]
104+
(let [log (atom [])
105+
after-build! (fn [{:keys [key]}]
106+
(swap! log conj key))]
110107
(with-open [root (di/start ::root
111108
(di/add-side-dependency `side-dep)
112109
{::root (di/template
@@ -119,7 +116,7 @@
119116
:g (di/ref `g)
120117
:h (di/ref `h)})}
121118
(di/add-side-dependency `side-dep2)
122-
(di/log built! demolish!))]
119+
(di/log :after-build! after-build!))]
123120
(t/is (= [`di/new-key#0 `side-dep `a
124121
`b `c `d `e `f `g `h
125122
`di/new-key#1 `side-dep2 ::root] @log))

test/darkleaf/di/dependencies_test.clj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131
:c)
3232

3333
(t/deftest order-test
34-
(let [log (atom [])
35-
built! (fn [key obj]
36-
(swap! log conj [key :built]))
37-
demolish! (fn [key obj]
38-
(swap! log conj [key :stopped]))]
39-
(with-open [root (di/start `root (di/log built! demolish!))])
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!))])
4041
(t/is (= [[`c :built]
4142
[`a :built]
4243
[`b :built]

test/darkleaf/di/tutorial/x_log_test.clj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
:c)
1818

1919
(t/deftest log
20-
(let [logs (atom [])
21-
built! (fn [key obj]
22-
(swap! logs conj [:built key (pr-str obj)]))
23-
demolished! (fn [key obj]
24-
(swap! logs conj [:demolished key (pr-str obj)]))]
25-
(with-open [root (di/start `c (di/log built! demolished!))])
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!))])
2627
(t/is (= [[:built `a ":a"]
2728
[:built `b
2829
"#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"]

0 commit comments

Comments
 (0)