Skip to content

Commit

Permalink
fix: fix selections sets instability (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde authored Jul 28, 2022
1 parent 3b3296e commit f2ea209
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/use-selections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@ export function useSelections<Meta>(
const visibleNodes_ = useVisibleNodes(fileTree);
const visibleNodes = nodes ?? visibleNodes_;
const prevSelectionsSet = React.useRef<SubjectRange<number> | null>(null);
const selectionsSet = React.useMemo(() => {
const next = createSelectionsSet(fileTree, visibleNodes);
const selectionsSet = createSelectionsSet(fileTree, nodes ?? emptyArray);

React.useEffect(() => {
if (prevSelectionsSet.current) {
for (const nodeId of prevSelectionsSet.current) {
next.add(nodeId);
selectionsSet.add(nodeId);
}
}

return next;
}, [fileTree, visibleNodes]);

React.useEffect(() => {
prevSelectionsSet.current = selectionsSet;
}, [selectionsSet]);

Expand Down Expand Up @@ -99,6 +95,8 @@ export function useSelections<Meta>(
};
}

const emptyArray: number[] = [];

const createProps = trieMemoize(
[WeakMap, WeakMap, Map],
(
Expand Down

0 comments on commit f2ea209

Please sign in to comment.