Commit 0cf0d62
Fix memoize re-invoking factories that return undefined (#22)
## Problem
`memoize()` uses `typeof memo !== "undefined"` as its cache-hit check,
so any factory whose result is `undefined` is silently re-invoked on
**every** `container.get()` call. This breaks the documented
run-once/singleton guarantee — notably for the side-effect initializer
pattern the docs recommend with `run()` (e.g. `Injectable("initCache",
["request"], (req) => populateCache(req))`, whose factory returns
nothing and therefore re-runs on every subsequent resolution).
Repro:
```ts
let calls = 0;
const c = Container.provides("init", () => { calls++; return undefined; });
c.get("init"); c.get("init"); c.get("init");
// calls === 3, expected 1
```
## Fix
Track invocation with an explicit `invoked` flag 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
- New `memoize.spec.ts`: undefined-result caching, throw-then-retry,
`delegate`/`isMemoized` behavior
- Container-level regression test in `Container.spec.ts`
All tests pass with 100% coverage maintained; ESLint clean.
Co-authored-by: Your Name <you@example.com>1 parent 9616a63 commit 0cf0d62
3 files changed
Lines changed: 59 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
397 | 397 | | |
398 | 398 | | |
399 | 399 | | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
400 | 409 | | |
401 | 410 | | |
402 | 411 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
13 | 17 | | |
14 | 18 | | |
15 | | - | |
| 19 | + | |
16 | 20 | | |
| 21 | + | |
17 | 22 | | |
18 | 23 | | |
19 | 24 | | |
| |||
0 commit comments