Skip to content

Commit

Permalink
Only update ref value if achievements change
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvonen committed Sep 6, 2024
1 parent 9003502 commit 9c8a39d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/hooks/achievements.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ref} from 'vue';
import {intersection, mapValues} from 'remeda';
import {intersection, isDeepEqual, mapValues} from 'remeda';
import {createActor, setup, type MachineContext} from 'xstate';
import type {GuardArgs} from 'xstate/guards';
import {BEANS, FRUITS, GRAINS, LEAFIES, ROOTS, VEGETABLES} from '@/utils/constants';
Expand Down Expand Up @@ -301,9 +301,12 @@ export function useAchievements() {

const achievements = ref(mapValues(actor.getSnapshot().value, Number) as Achievements);

const subscription = actor.subscribe(
(snapshot) => (achievements.value = mapValues(snapshot.value, Number)),
);
const subscription = actor.subscribe((snapshot) => {
const mapped = mapValues(snapshot.value, Number);
if (!isDeepEqual(achievements.value, mapped)) {
achievements.value = mapped;
}
});

window.addEventListener('beforeunload', () => {
actor.stop();
Expand Down

0 comments on commit 9c8a39d

Please sign in to comment.