Skip to content

Commit

Permalink
Check individual selected values
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Dec 8, 2024
1 parent 2a7f072 commit eb4d195
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/store/hooks/useStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,35 @@ export function useStateStore<
);

const wrappedSnapshot = useMemo(() => {
let cached: [T, O];
let cachedTuple: [T, O];

return () => {
const current = store?.getLatestValue();
const currentValue = store?.getLatestValue();

if (!currentValue) return undefined;

// store value hasn't changed, no need to compare individual values
if (cachedTuple && cachedTuple[0] === currentValue) {
return cachedTuple[1];
}

const newlySelected = selector(currentValue);

// store value changed but selected values wouldn't have to, double-check selected
if (cachedTuple) {
let selectededAreEqualToCached = true;

if (!current) return undefined;
for (const key in cachedTuple[1]) {
if (cachedTuple[1][key] === newlySelected[key]) continue;
selectededAreEqualToCached = false;
break;
}

if (!cached || cached[0] !== current) {
cached = [current, selector(current)];
return cached[1];
if (selectededAreEqualToCached) return cachedTuple[1];
}

return cached[1];
cachedTuple = [currentValue, newlySelected];
return cachedTuple[1];
};
}, [store, selector]);

Expand Down

0 comments on commit eb4d195

Please sign in to comment.