-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memoize2 #36
Draft
darkleaf
wants to merge
23
commits into
master
Choose a base branch
from
memoize2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Memoize2 #36
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e38ae77
wip
darkleaf 974d767
wip2
darkleaf d946dd9
fix identical-deps
darkleaf 490deb4
deep-contains?
darkleaf 0206b2e
cache registry
darkleaf 1a85b13
print
darkleaf 2c849f9
cache as param
darkleaf f4cb063
fix
darkleaf d8dfe0c
remove comment
darkleaf 440708a
remove pr test
darkleaf 5370bc1
fix test
darkleaf 0f7a20f
comment
darkleaf 2371f8f
fix update
darkleaf 7e42ac0
simplify cache structure
darkleaf f0593bd
deref cache
darkleaf ced835c
remove identity check
darkleaf ecca961
a new implementation: ->cache
darkleaf 7f25ebb
remove irrelevant comment
darkleaf e8c03b2
Add comment
krevedkokun 201ed3c
test
darkleaf e76cb5d
memoize test
darkleaf 8b5d433
cache -> memoize
darkleaf 67cfe35
comment
darkleaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
(ns darkleaf.di.memoize-test | ||
(:require | ||
[clojure.test :as t] | ||
[darkleaf.di.core :as di])) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(t/deftest memoize-test | ||
(let [a 'a | ||
identity* (memoize identity)] | ||
(identity* a) | ||
(t/is (not (identical? a (identity 'a)))) | ||
(t/is (identical? a (identity* 'a))))) | ||
|
||
(defn- some+identical? [a b] | ||
(and (some? a) | ||
(some? b) | ||
(identical? a b))) | ||
|
||
(defn- some+not-identical? [a b] | ||
(and (some? a) | ||
(some? b) | ||
(not (identical? a b)))) | ||
|
||
(defn a | ||
{::di/kind :component} | ||
[{_ ::param}] | ||
(Object.)) | ||
|
||
(t/deftest ok-test | ||
(let [mem (di/->memoize) | ||
registry (fn [] | ||
[{::param (Object.)} | ||
mem])] | ||
(with-open [main (di/start `a (registry)) | ||
secondary (di/start `a (registry))] | ||
(t/is (some+identical? @main @secondary))))) | ||
|
||
(t/deftest changed-not-identical-test | ||
(let [mem (di/->memoize) | ||
registry (fn [] | ||
[{::param (Object.)} | ||
mem])] | ||
(with-open [main (di/start `a (registry)) | ||
secondary (di/start `a (registry) | ||
{::param (Object.)})] | ||
(t/is (some+not-identical? @main @secondary))))) | ||
|
||
(t/deftest changed-equal-and-identical-test | ||
(let [mem (di/->memoize) | ||
registry (fn [] | ||
[{::param :equal-and-identical} | ||
mem])] | ||
(with-open [main (di/start `a (registry)) | ||
secondary (di/start `a (registry) | ||
{::param :equal-and-identical})] | ||
(t/is (some+identical? @main @secondary))))) | ||
|
||
|
||
(t/deftest changed-equal-but-not-identical-test | ||
(let [mem (di/->memoize) | ||
registry (fn [] | ||
[{::param 'equal-but-not-identical} | ||
mem])] | ||
(with-open [main (di/start `a (registry)) | ||
secondary (di/start `a (registry) | ||
{::param 'equal-but-not-identical})] | ||
(t/is (some+identical? @main @secondary))))) | ||
|
||
(t/deftest changed-equal-but-different-test | ||
(let [mem (di/->memoize) | ||
registry (fn [] | ||
[{::param []} | ||
mem])] | ||
(with-open [main (di/start `a (registry)) | ||
secondary (di/start `a (registry) | ||
{::param '()})] | ||
(t/is (some+identical? @main @secondary))))) | ||
|
||
|
||
(t/deftest start-stop-order-test | ||
(let [mem (di/->memoize) | ||
registry (fn [] | ||
[{::param :param} | ||
mem]) | ||
log (atom []) | ||
callbacks (fn [system] | ||
{:after-build! (fn [{:keys [key]}] | ||
(swap! log conj [:start system key])) | ||
:after-demolish! (fn [{:keys [key]}] | ||
(swap! log conj [:stop system key]))})] | ||
(with-open [_ (di/start `a | ||
(registry) | ||
(di/log (callbacks :main))) | ||
_ (di/start [::x `a] | ||
{::x :x} | ||
(di/log (callbacks :second)) | ||
(registry)) | ||
_ (di/start [::y `a] | ||
{::y :y} | ||
(di/log (callbacks :third)) | ||
(registry))]) | ||
|
||
(t/is (= [[:start :main ::param] | ||
[:start :main `a] | ||
[:start :main ::di/implicit-root] | ||
|
||
[:start :second ::x] | ||
[:start :second ::di/implicit-root] | ||
|
||
[:start :third ::y] | ||
[:start :third ::di/implicit-root] | ||
|
||
[:stop :third ::di/implicit-root] | ||
[:stop :third ::y] | ||
|
||
[:stop :second ::di/implicit-root] | ||
[:stop :second ::x] | ||
|
||
[:stop :main ::di/implicit-root] | ||
[:stop :main `a] | ||
[:stop :main ::param]] | ||
@log)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
(ns darkleaf.di.tutorial.z-memoize-test ;;todo: name | ||
(:require | ||
[clojure.test :as t] | ||
[darkleaf.di.core :as di])) | ||
|
||
(defn connection | ||
{::di/stop #(reset! % :stopped)} | ||
[{url "CONNECTION_URL"}] | ||
(atom :started)) | ||
|
||
(defn migrations | ||
"A long running side effect" | ||
{::di/kind :component} | ||
[{connection `connection}] | ||
#_ | ||
(when (= :stopped @connection) | ||
(throw (IllegalStateException. "Connection is not started"))) | ||
(random-uuid)) | ||
|
||
(defn root | ||
{::di/kind :component} | ||
[{migrations `migrations}] | ||
{:migrations migrations}) | ||
|
||
;; todo: check registry placement | ||
|
||
#_ | ||
(t/deftest ok | ||
(let [[cache global-system :as root] | ||
(di/start [::di/cache `root] | ||
{"CONNECTION_URL" "1"} | ||
(di/collect-cache))] | ||
|
||
(with-open [local (di/start `root | ||
(di/use-cache cache))] | ||
(t/is (identical? global-system @local))) | ||
|
||
(with-open [local (di/start `root | ||
(di/use-cache cache) | ||
{"CONNECTION_URL" "1"})] | ||
(t/is (identical? global-system @local))) | ||
|
||
(with-open [local (di/start `root | ||
;; `use-cache` should be the first registry | ||
(di/use-cache cache) | ||
{"CONNECTION_URL" "2"})] | ||
(t/is (not (identical? global-system @local)))) | ||
|
||
|
||
(di/stop root) | ||
|
||
(t/is (thrown? IllegalStateException | ||
(di/start `root | ||
(di/use-cache cache)))))) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returns middleware that memoizes all registry build accesses. Must be the last in a registry