Skip to content

Centralize weight use cases in DI container#1453

Merged
marcuscastelo merged 49 commits intomarcuscastelo/issue1447from
marcuscastelo/issue1447-remove-shims
Mar 23, 2026
Merged

Centralize weight use cases in DI container#1453
marcuscastelo merged 49 commits intomarcuscastelo/issue1447from
marcuscastelo/issue1447-remove-shims

Conversation

@marcuscastelo
Copy link
Owner

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.

marcuscastelo and others added 8 commits December 6, 2025 11:35
- 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
Copilot AI review requested due to automatic review settings December 6, 2025 15:03
@marcuscastelo marcuscastelo self-assigned this Dec 6, 2025
@vercel
Copy link

vercel bot commented Dec 6, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
macroflows Ready Ready Preview, Comment Mar 23, 2026 6:33pm

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 useCases import from weightUseCases.ts by making authDeps a required parameter
  • Centralized both weightUseCases and weightChartUseCases in 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

Copilot AI and others added 5 commits December 6, 2025 15:12
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)
Copilot AI and others added 7 commits December 6, 2025 19:32
…ile modules

Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
Co-authored-by: marcuscastelo <27441558+marcuscastelo@users.noreply.github.com>
…d-compatibility-shims

refactor(di): Remove backward-compatibility shims from telemetry, search, profile, and recentFood modules
@marcuscastelo marcuscastelo merged commit a1cf23c into marcuscastelo/issue1447 Mar 23, 2026
3 checks passed
@marcuscastelo marcuscastelo deleted the marcuscastelo/issue1447-remove-shims branch March 23, 2026 18:34
Comment on lines +14 to +23
const getMacroOverflow = () => {
const memo = createMemo<ReturnType<typeof createMacroOverflow>>(() =>
createMacroOverflow({
dayUseCases: useCases.dayUseCases(),
macroTargetUseCases,
}),
)

return memo
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants