Fix memoize re-invoking factories that return undefined - #22
Merged
Conversation
memoize() used 'typeof memo !== "undefined"' as its cache-hit check, so any factory whose result was undefined was re-invoked on every call. This broke the documented run-once/singleton guarantee for services registered via provides()/run() whose factories perform side effects but return nothing. Track invocation with an explicit flag instead. The flag is set only after the delegate returns, preserving the existing retry-on-throw behavior. Adds unit tests for memoize (undefined results, throw-retry, delegate exposure) and a Container-level regression test.
kburov-sc
approved these changes
Jul 2, 2026
kburov-sc
left a comment
Collaborator
There was a problem hiding this comment.
LGTM, thank you for the contribution
Contributor
Author
|
@kburov-sc , No problem. I'm glad to help anytime. |
|
@Montana the hero! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
memoize()usestypeof memo !== "undefined"as its cache-hit check, so any factory whose result isundefinedis silently re-invoked on everycontainer.get()call. This breaks the documented run-once/singleton guarantee — notably for the side-effect initializer pattern the docs recommend withrun()(e.g.Injectable("initCache", ["request"], (req) => populateCache(req)), whose factory returns nothing and therefore re-runs on every subsequent resolution).Repro:
Fix
Track invocation with an explicit
invokedflag instead of inspecting the memoized value. The flag is set only after the delegate returns, deliberately preserving the existing retry-on-throw behavior that existing tests rely on.Tests
memoize.spec.ts: undefined-result caching, throw-then-retry,delegate/isMemoizedbehaviorContainer.spec.tsAll tests pass with 100% coverage maintained; ESLint clean.