Skip to content

Commit

Permalink
wip5
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Oct 15, 2024
1 parent 8bcce98 commit 91cbdbc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,13 @@
(fn [registry]
(fn [key]
(if (= ::cache key)
@cache
(reify p/Factory
(dependencies [_]
nil)
(build [_ _]
(assoc @cache :valid? (atom true)))
(demolish [_ obj]
(reset! (:valid? obj) false)))
(let [factory (registry key)]
(reify p/Factory
(dependencies [_]
Expand All @@ -784,6 +790,8 @@
(defn use-cache [cache]
(fn [registry]
(fn [key]
(when-not @(:valid? cache)
(throw (IllegalStateException. "Invalid cache")))
(let [factory (registry key)
cached-deps (cache [:key->deps key])
cached-obj (cache [:key->obj key])]
Expand Down
10 changes: 3 additions & 7 deletions test/darkleaf/di/tutorial/z_memoize_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
{"CONNECTION_URL" "1"}
(di/collect-cache))]


(prn cache)

(with-open [local (di/start `root
(di/use-cache cache))]
(t/is (identical? global-system @local)))
Expand All @@ -49,9 +46,8 @@
(t/is (not (identical? global-system @local))))


#_#_
(di/stop root)

(with-open [local (di/start `root
(di/use-cache cache))]
(t/is (identical? global-system @local)))))
(t/is (thrown? IllegalStateException
(di/start `root
(di/use-cache cache))))))

0 comments on commit 91cbdbc

Please sign in to comment.