Skip to content

Commit

Permalink
wip4
Browse files Browse the repository at this point in the history
Co-authored-by: KGOH <[email protected]>
  • Loading branch information
darkleaf and KGOH committed Oct 23, 2024
1 parent 6d9c1d6 commit dcfd65d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
14 changes: 11 additions & 3 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,15 @@
(finally
(.close resource#)))))))

(defn log [built-cb demolished-cb]
(defn log
"A logging middleware.
Calls `:after-build!` and `:after-demolish!` during `di/start`.
Must be the last one in the middleware chain.
Both callbacks are expected to accept
the following arg `{:keys [key object]}`."
[& {:keys [after-build! after-demolish!]
:or {after-build! (fn no-op [_])
after-demolish! (fn no-op [_])}}]
(fn [registry]
(fn [key]
(let [factory (registry key)]
Expand All @@ -807,9 +815,9 @@
(p/dependencies factory))
(build [_ deps]
(let [obj (p/build factory deps)]
(built-cb key obj)
(after-build! {:key key :object obj})
obj))
(demolish [_ obj]
(p/demolish factory obj)
(demolished-cb key obj)
(after-demolish! {:key key :object obj})
nil))))))
21 changes: 9 additions & 12 deletions test/darkleaf/di/add_side_dependency_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@
:side-dep)

(t/deftest bug-array-map->hash-map
(let [log (atom [])
built! (fn [key obj]
(swap! log conj key))
demolish! (fn [key obj])]
(let [log (atom [])
after-build! (fn [{:keys [key]}]
(swap! log conj key))]
(with-open [root (di/start ::root
{::log log
::root (di/template
{::root (di/template
{:a (di/ref `a)
:b (di/ref `b)
:c (di/ref `c)
Expand All @@ -86,7 +84,7 @@
:g (di/ref `g)
:h (di/ref `h)})}
(di/add-side-dependency `side-dep)
(di/log built! demolish!))]
(di/log :after-build! after-build!))]
(t/is (= [`a `b `c `d `e `f `g `h `di/new-key#0 `side-dep ::root] @log))
(t/is (= {:a :a
:b :b
Expand All @@ -103,10 +101,9 @@
:side-dep2)

(t/deftest bug-array-map->hash-map-2
(let [log (atom [])
built! (fn [key obj]
(swap! log conj key))
demolish! (fn [key obj])]
(let [log (atom [])
after-build! (fn [{:keys [key]}]
(swap! log conj key))]
(with-open [root (di/start ::root
(di/add-side-dependency `side-dep)
{::root (di/template
Expand All @@ -119,7 +116,7 @@
:g (di/ref `g)
:h (di/ref `h)})}
(di/add-side-dependency `side-dep2)
(di/log built! demolish!))]
(di/log :after-build! after-build!))]
(t/is (= [`di/new-key#0 `side-dep `a
`b `c `d `e `f `g `h
`di/new-key#1 `side-dep2 ::root] @log))
Expand Down
13 changes: 7 additions & 6 deletions test/darkleaf/di/dependencies_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
:c)

(t/deftest order-test
(let [log (atom [])
built! (fn [key obj]
(swap! log conj [key :built]))
demolish! (fn [key obj]
(swap! log conj [key :stopped]))]
(with-open [root (di/start `root (di/log built! demolish!))])
(let [log (atom [])
after-build! (fn [{:keys [key]}]
(swap! log conj [key :built]))
after-demolish! (fn [{:keys [key]}]
(swap! log conj [key :stopped]))]
(with-open [root (di/start `root (di/log :after-build! after-build!
:after-demolish! after-demolish!))])
(t/is (= [[`c :built]
[`a :built]
[`b :built]
Expand Down
13 changes: 7 additions & 6 deletions test/darkleaf/di/tutorial/x_log_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
:c)

(t/deftest log
(let [logs (atom [])
built! (fn [key obj]
(swap! logs conj [:built key (pr-str obj)]))
demolished! (fn [key obj]
(swap! logs conj [:demolished key (pr-str obj)]))]
(with-open [root (di/start `c (di/log built! demolished!))])
(let [logs (atom [])
after-build! (fn [{:keys [key object]}]
(swap! logs conj [:built key (pr-str object)]))
after-demolish! (fn [{:keys [key object]}]
(swap! logs conj [:demolished key (pr-str object)]))]
(with-open [root (di/start `c (di/log :after-build! after-build!
:after-demolish! after-demolish!))])
(t/is (= [[:built `a ":a"]
[:built `b
"#darkleaf.di.core/service #'darkleaf.di.tutorial.x-log-test/b"]
Expand Down

0 comments on commit dcfd65d

Please sign in to comment.