Skip to content

Commit

Permalink
Add log middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Oct 16, 2024
1 parent 00dcf18 commit 0bdc02b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,19 @@
~@body))
(finally
(.close resource#)))))))

(defn log [built-cb demolished-cb]
(fn [registry]
(fn [key]
(let [factory (registry key)]
(reify p/Factory
(dependencies [_]
(p/dependencies factory))
(build [_ deps]
(let [obj (p/build factory deps)]
(built-cb key deps obj)
obj))
(demolish [_ obj]
(p/demolish factory obj)
(demolished-cb key obj)
nil))))))
37 changes: 37 additions & 0 deletions test/darkleaf/di/tutorial/x_log_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ns darkleaf.di.tutorial.x-log-test
(:require
[clojure.test :as t]
[darkleaf.di.core :as di]))

(defn a
{::di/kind :component}
[]
:a)

(defn b [{a `a}]
:b)

(defn c
{::di/kind :component}
[{b `b}]
:c)

(t/deftest log
(let [logs (atom [])
built! (fn [key deps obj]
(swap! logs conj [:built key deps obj]))
demolished! (fn [key obj]
(swap! logs conj [:demolished key obj]))
[a b c
:as system] (di/start [`a `b `c]
(di/log built! demolished!))]
(di/stop system)
(t/is (= [[:built `a {} a]
[:built `b {`a a} b]
[:built `c {`b b} c]
[:built ::di/implicit-root {`a a `b b `c c} [a b c]]
[:demolished ::di/implicit-root [a b c]]
[:demolished `c c]
[:demolished `b b]
[:demolished `a a]]
@logs))))

0 comments on commit 0bdc02b

Please sign in to comment.