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

Refactor state update on render #571

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
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 @@
];
});
// 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 Expand Up @@ -321,7 +317,7 @@
scrollTo: scroller.$scrollTo,
scrollBy: scroller.$scrollBy,
};
}, []);

Check warning on line 320 in src/react/VGrid.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useImperativeHandle has missing dependencies: 'hStore', 'scroller.$scrollBy', 'scroller.$scrollTo', 'scroller.$scrollToIndex', and 'vStore'. Either include them or remove the dependency array

const render = useMemo(() => {
const cache = new Map<string, ReactNode>();
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 @@
});

// 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 Expand Up @@ -322,7 +318,7 @@
scrollTo: scroller.$scrollTo,
scrollBy: scroller.$scrollBy,
};
}, []);

Check warning on line 321 in src/react/Virtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useImperativeHandle has missing dependencies: 'scroller.$scrollBy', 'scroller.$scrollTo', 'scroller.$scrollToIndex', and 'store'. Either include them or remove the dependency array

for (let i = startIndex, j = endIndex; i <= j; i++) {
items.push(getListItem(i));
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 @@
];
});
// 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 Expand Up @@ -232,7 +230,7 @@
findEndIndex: store.$findEndIndex,
scrollToIndex: scroller.$scrollToIndex,
};
}, []);

Check warning on line 233 in src/react/WindowVirtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useImperativeHandle has missing dependencies: 'scroller.$scrollToIndex' and 'store'. Either include them or remove the dependency array

for (let i = startIndex, j = endIndex; i <= j; i++) {
const e = getElement(i);
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
Loading