Skip to content

Commit

Permalink
fix: Memoize achievement inferencer (#3061)
Browse files Browse the repository at this point in the history
Uses a per-component instance singleton and memoizes it, as we only need
to keep track of the initial states of the control panel.

Also added a TODO comment for future refactoring to more modern React
principles using component state and hooks.
  • Loading branch information
RichDom2185 authored Oct 8, 2024
1 parent dbc8ec8 commit 8213d21
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/pages/achievement/control/AchievementControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ const AchievementControl: React.FC = () => {
[dispatch]
);

const inferencer = useTypedSelector(
state => new AchievementInferencer(state.achievement.achievements, state.achievement.goals)
// TODO: This is a hacky fix. By right, we shouldn't need to use an
// inferencer instance since we can encapsulate the logic using hooks
// and component state.
const [initialAchievements, initialGoals] = useTypedSelector(state => [
state.achievement.achievements,
state.achievement.goals
]);
const inferencer = useMemo(
() => new AchievementInferencer(initialAchievements, initialGoals),
// We only want to create the inferencer once
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);

/**
Expand Down

0 comments on commit 8213d21

Please sign in to comment.