Centralize weight use cases in DI container#1453
Centralize weight use cases in DI container#1453marcuscastelo merged 49 commits intomarcuscastelo/issue1447from
Conversation
- Refactor createWeightUseCases to accept granular authDeps instead of whole useCases object, breaking circular dependency - Add WeightUseCases to useCases.ts container - Add WeightUseCases type to container.tsx - Keep backward-compatible shim in weightUseCases.ts for legacy consumers - Next: migrate consumers to use container instead of direct imports
…iner Migrate all consumers of weightUseCases to use the centralized DI container instead of direct imports from the module: - weightChartUseCases.ts - now uses useCases.weightUseCases() - macroTargetUseCases.ts - now uses useCases.weightUseCases() - WeightView.tsx - now uses useCases.weightUseCases() - WeightEvolution.tsx - now uses useCases.weightUseCases() - MacroEvolution.tsx - now uses useCases.weightUseCases() - MacroProfile.tsx - now uses useCases.weightUseCases() - BodyMeasureChart.tsx - now uses useCases.weightUseCases() - RestoreProfileModal.tsx - now uses useCases.weightUseCases() This prepares for removal of the backward-compatible shim in weightUseCases.ts
All consumers now use the centralized DI container (useCases.weightUseCases()), so the shim export is no longer needed. Removed: - export const weightUseCases = createWeightUseCases() - export function refetchUserWeights() The factory createWeightUseCases() and type WeightUseCases remain exported for DI wiring and type safety.
… circular import - Remove useCases import from weightUseCases.ts to break circular dependency - Make authDeps a required parameter for createWeightUseCases - Update container.tsx to provide authDeps when creating default weight use-cases This breaks the circular dependency that was causing test failures when modules imported from useCases.ts at module initialization time.
- Document weightUseCases shim removal as complete - Document clipboardUseCases circular dependency blocking centralization - List remaining shims to evaluate - Add commit references for Batch 7
- Refactor createWeightChartUseCases to accept granular WeightChartDeps instead of typeof useCases to avoid circular dependency - Remove weightChartUseCases shim (all consumers now use container) - Update consumers (WeightChartTooltip, WeightEvolution) to use useCases.weightChartUseCases() - Add weightChartUseCases to useCases.ts with proper dependency wiring
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR centralizes weight and weight-chart use-cases in the DI container to eliminate circular dependencies and remove backward-compatible shims. The refactoring introduces granular dependency interfaces (WeightUseCasesDeps, WeightChartDeps) that accept only the minimal required dependencies instead of the full useCases object, breaking circular import chains. All consumers (8 files) are migrated to access these use-cases through useCases.weightUseCases() and useCases.weightChartUseCases().
Key changes:
- Removed circular
useCasesimport fromweightUseCases.tsby makingauthDepsa required parameter - Centralized both
weightUseCasesandweightChartUseCasesin the DI container - Removed backward-compatible shims from both factories
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
src/di/useCases.ts |
Added weightUseCases and weightChartUseCases to centralized container, wired with granular dependencies to avoid circular imports |
src/di/container.tsx |
Extended Container type with WeightUseCases, created default weight use-cases with auth dependencies |
src/modules/weight/application/weight/usecases/weightUseCases.ts |
Made authDeps required parameter, removed circular useCases import, removed backward-compatible shim |
src/modules/weight/application/chart/weightChartUseCases.ts |
Introduced WeightChartDeps interface, made dependencies required, removed shim |
src/sections/weight/components/WeightView.tsx |
Migrated to use useCases.weightUseCases() for update/delete operations |
src/sections/weight/components/WeightEvolution.tsx |
Migrated to use useCases.weightUseCases() and useCases.weightChartUseCases() - has critical accessor wrapping bug |
src/sections/weight/components/WeightChartTooltip.tsx |
Migrated to use useCases.weightChartUseCases() for progress calculation |
src/sections/profile/measure/components/BodyMeasureChart.tsx |
Migrated to use useCases.weightUseCases().weights() in measure processing |
src/sections/profile/components/MacroProfile.tsx |
Migrated to use useCases.weightUseCases().latest() for weight check |
src/sections/profile/components/MacroEvolution.tsx |
Migrated all chart components to use useCases.weightUseCases() - has critical accessor wrapping bug |
src/modules/diet/macro-profile/ui/RestoreProfileModal.tsx |
Migrated to use useCases.weightUseCases() for weight lookups |
src/modules/diet/macro-target/application/macroTargetUseCases.ts |
Updated default to use centralized useCases.weightUseCases(), but maintains shim pattern that may cause circular dependency |
DI-migration-plan.md |
Updated with Batch 7 progress, documents completed migrations and remaining work |
Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
…pletion) Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
…ompatibility-shims refactor(di): Remove 4 backward-compatible shims (Batch 7 partial)
…rt factories Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
…se cases directly
…ories Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
…Repository Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
…ile modules Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
….com/marcuscastelo/macroflows into marcuscastelo/issue1447-remove-shims
…d-compatibility-shims refactor(di): Remove backward-compatibility shims from telemetry, search, profile, and recentFood modules
….com/marcuscastelo/macroflows into marcuscastelo/issue1447-remove-shims
refactor(supabase): implement no-op storage for SSR environments
…roject-specific settings
src/sections/item/components/ItemView/ItemEdit/ItemEditBody.tsx
Outdated
Show resolved
Hide resolved
| const getMacroOverflow = () => { | ||
| const memo = createMemo<ReturnType<typeof createMacroOverflow>>(() => | ||
| createMacroOverflow({ | ||
| dayUseCases: useCases.dayUseCases(), | ||
| macroTargetUseCases, | ||
| }), | ||
| ) | ||
|
|
||
| return memo | ||
| } |
There was a problem hiding this comment.
Bug: A memory leak occurs because getMacroOverflow(), which creates a memo, is called inside another createMemo, leading to an accumulation of undisposed nested memos on re-evaluation.
Severity: MEDIUM
Suggested Fix
Refactor the getMacroOverflow factory function to avoid creating a new createMemo on every call. Instead, wrap the call to getMacroOverflow() itself in a createMemo at the component level. This ensures the inner reactive computations are created only once per component instance, preventing the memory leak.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: src/sections/item/components/ItemView/ItemViewMacros.tsx#L14-L23
Potential issue: In `ItemViewMacros.tsx`, the function `getMacroOverflow()` is called
within a `createMemo` that re-evaluates whenever its dependencies, such as `item` or
`originalItem`, change. The `getMacroOverflow()` function itself creates a new, nested
`createMemo` each time it is invoked. This pattern results in a memory leak, as a new
nested memo is created on every re-evaluation of the parent memo, while the previously
created nested memos are not disposed of, leading to an accumulation of orphaned
reactive computations.
Refactor weight use cases to be centralized in the DI container, removing backward-compatible shims and breaking circular dependencies. Migrate all consumers to utilize the new structure, ensuring type safety and preparing for future enhancements. Document progress and remaining tasks in the migration plan.