Skip to content

Commit

Permalink
Refactor state update on render
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Dec 14, 2024
1 parent 69496bf commit fb42df0
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 42 deletions.
21 changes: 12 additions & 9 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,18 @@ export const createVirtualStore = (
break;
}
case ACTION_ITEMS_LENGTH_CHANGE: {
if (payload[1]) {
applyJump(updateCacheLength(cache, payload[0], true));
_scrollMode = SCROLL_BY_SHIFT;
mutated = UPDATE_VIRTUAL_STATE;
} else {
updateCacheLength(cache, payload[0]);
// https://github.com/inokawa/virtua/issues/552
// https://github.com/inokawa/virtua/issues/557
mutated = UPDATE_VIRTUAL_STATE;
const [length, isShift] = payload;
if (cache._length !== length) {
if (isShift) {
applyJump(updateCacheLength(cache, length, true));
_scrollMode = SCROLL_BY_SHIFT;
mutated = UPDATE_VIRTUAL_STATE;
} else {
updateCacheLength(cache, length);
// https://github.com/inokawa/virtua/issues/552
// https://github.com/inokawa/virtua/issues/557
mutated = UPDATE_VIRTUAL_STATE;
}
}
break;
}
Expand Down
8 changes: 2 additions & 6 deletions src/react/VGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,8 @@ export const VGrid = forwardRef<VGridHandle, VGridProps>(
];
});
// The elements length and cached items length are different just after element is added/removed.
if (rowCount !== vStore.$getItemsLength()) {
vStore.$update(ACTION_ITEMS_LENGTH_CHANGE, [rowCount]);
}
if (colCount !== hStore.$getItemsLength()) {
hStore.$update(ACTION_ITEMS_LENGTH_CHANGE, [colCount]);
}
vStore.$update(ACTION_ITEMS_LENGTH_CHANGE, [rowCount]);
hStore.$update(ACTION_ITEMS_LENGTH_CHANGE, [colCount]);

const vRerender = useRerender(vStore);
const hRerender = useRerender(hStore);
Expand Down
8 changes: 2 additions & 6 deletions src/react/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,8 @@ export const Virtualizer = forwardRef<VirtualizerHandle, VirtualizerProps>(
});

// The elements length and cached items length are different just after element is added/removed.
if (count !== store.$getItemsLength()) {
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, shift]);
}
if (startMargin !== store.$getStartSpacerSize()) {
store.$update(ACTION_START_OFFSET_CHANGE, startMargin);
}
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, shift]);
store.$update(ACTION_START_OFFSET_CHANGE, startMargin);

const rerender = useRerender(store);

Expand Down
4 changes: 1 addition & 3 deletions src/react/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ export const WindowVirtualizer = forwardRef<
];
});
// The elements length and cached items length are different just after element is added/removed.
if (count !== store.$getItemsLength()) {
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, shift]);
}
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, shift]);

const rerender = useRerender(store);

Expand Down
8 changes: 2 additions & 6 deletions src/solid/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ export const Virtualizer = <T,>(props: VirtualizerProps<T>): JSX.Element => {
on(
() => props.data.length,
(count) => {
if (count !== store.$getItemsLength()) {
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, props.shift]);
}
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, props.shift]);
}
)
);
Expand All @@ -253,9 +251,7 @@ export const Virtualizer = <T,>(props: VirtualizerProps<T>): JSX.Element => {
on(
() => props.startMargin || 0,
(value) => {
if (value !== store.$getStartSpacerSize()) {
store.$update(ACTION_START_OFFSET_CHANGE, value);
}
store.$update(ACTION_START_OFFSET_CHANGE, value);
}
)
);
Expand Down
4 changes: 1 addition & 3 deletions src/solid/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ export const WindowVirtualizer = <T,>(
on(
() => props.data.length,
(len) => {
if (len !== store.$getItemsLength()) {
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [len, props.shift]);
}
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [len, props.shift]);
}
)
);
Expand Down
8 changes: 2 additions & 6 deletions src/svelte/Virtualizer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,11 @@
});
$effect.pre(() => {
if (data.length !== store.$getItemsLength()) {
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [data.length, shift]);
}
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [data.length, shift]);
});
$effect.pre(() => {
if (startMargin !== store.$getStartSpacerSize()) {
store.$update(ACTION_START_OFFSET_CHANGE, startMargin);
}
store.$update(ACTION_START_OFFSET_CHANGE, startMargin);
});
let prevJumpCount: number | undefined;
Expand Down
4 changes: 1 addition & 3 deletions src/svelte/WindowVirtualizer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@
});
$effect.pre(() => {
if (data.length !== store.$getItemsLength()) {
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [data.length, shift]);
}
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [data.length, shift]);
});
let prevJumpCount: number | undefined;
Expand Down

0 comments on commit fb42df0

Please sign in to comment.