Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1000,17 +1000,23 @@
(throw (ex-info "A memoized registry should be the first middleware"
{:type ::wrong-memoized-registry-position})))
(fn [key]
(let [factory (.computeIfAbsent factories key
(fn [_]
(let [factory (registry key)]
(add-factory-watch factory
#(.remove factories key factory))
factory)))]
(reify p/Factory
(dependencies [_]
(p/dependencies factory))
(build [_ deps _add-stop]
(.computeIfAbsent objs [factory deps]
(fn [_] (p/build factory deps add-stop))))
(description [_]
(p/description factory)))))))))
(let [factory (.computeIfAbsent factories key
(fn [_]
(let [factory (registry key)]
(add-factory-watch factory
#(.remove factories key factory))
factory)))
description (p/description factory)]
(cond
(::side-dependency description)
factory

:else
(reify p/Factory
(dependencies [_]
(p/dependencies factory))
(build [_ deps _add-stop]
(.computeIfAbsent objs [factory deps]
(fn [_] (p/build factory deps add-stop))))
(description [_]
(p/description factory))))))))))
17 changes: 17 additions & 0 deletions test/darkleaf/di/memoize_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@
[:stop `invalidation-a]]
@log))))

(t/deftest side-dependency-test
(defn- side-dependency
{::di/kind :component}
[{calls ::calls}]
(swap! calls inc)
:ok)

(let [calls (atom 0)
mem (di/->memoize {::param :param
::calls calls}
(di/add-side-dependency `side-dependency))]
(di/start `a mem)
(di/start `a mem)
(t/is (= 2 @calls))
(di/stop mem)))
Comment on lines +227 to +241
Copy link
Owner Author

@darkleaf darkleaf Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

с другой стороны, как раз затеяли memoize, чтобы не запускать side-dependency



(comment

(require '[clj-async-profiler.core :as prof])
Expand Down