Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Memoize achievement inferencer #3061

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
]);
Comment on lines +41 to +44
Copy link
Member Author

Choose a reason for hiding this comment

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

useTypedSelector automatically listens to any state changes and reruns the callback, resulting in a new instance of the inferencer created each time the state is updated. This led to the bugged behaviour where it could not keep track of the state of the newly added (unpublished) goals and achievements.

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
Loading