From dcfd65dfff227ecd89162f695012b2b305afeef8 Mon Sep 17 00:00:00 2001 From: Mikhail Kuzmin Date: Wed, 23 Oct 2024 17:00:14 +0400 Subject: [PATCH] wip4 Co-authored-by: KGOH --- src/darkleaf/di/core.clj | 14 ++++++++++--- test/darkleaf/di/add_side_dependency_test.clj | 21 ++++++++----------- test/darkleaf/di/dependencies_test.clj | 13 ++++++------ test/darkleaf/di/tutorial/x_log_test.clj | 13 ++++++------ 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index 1a73306a..8d090420 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -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)] @@ -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)))))) diff --git a/test/darkleaf/di/add_side_dependency_test.clj b/test/darkleaf/di/add_side_dependency_test.clj index 3b6fcfdf..dbfeded6 100644 --- a/test/darkleaf/di/add_side_dependency_test.clj +++ b/test/darkleaf/di/add_side_dependency_test.clj @@ -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) @@ -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 @@ -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 @@ -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)) diff --git a/test/darkleaf/di/dependencies_test.clj b/test/darkleaf/di/dependencies_test.clj index 3cde4a76..0c87e1bf 100644 --- a/test/darkleaf/di/dependencies_test.clj +++ b/test/darkleaf/di/dependencies_test.clj @@ -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] diff --git a/test/darkleaf/di/tutorial/x_log_test.clj b/test/darkleaf/di/tutorial/x_log_test.clj index bda02d2d..ae8fa1ae 100644 --- a/test/darkleaf/di/tutorial/x_log_test.clj +++ b/test/darkleaf/di/tutorial/x_log_test.clj @@ -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"]