File tree Expand file tree Collapse file tree 4 files changed +34
-27
lines changed
Expand file tree Collapse file tree 4 files changed +34
-27
lines changed Original file line number Diff line number Diff line change 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)]
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 ))))))
Original file line number Diff line number Diff line change 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)
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
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
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))
Original file line number Diff line number Diff line change 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 ]
Original file line number Diff line number Diff line change 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" ]
You can’t perform that action at this time.
0 commit comments