Skip to content

Commit 2ed3d82

Browse files
committed
Refactor state update on render
1 parent 3078fb2 commit 2ed3d82

9 files changed

Lines changed: 30 additions & 49 deletions

File tree

src/core/scroller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export const createScroller = (
334334
scheduleImperativeScroll(() => offset);
335335
},
336336
$scrollToIndex(index, { align, smooth, offset = 0 } = {}) {
337-
index = clamp(index, 0, store.$getItemsLength() - 1);
337+
index = clamp(index, 0, store._getItemsLength() - 1);
338338

339339
if (align === "nearest") {
340340
const itemOffset = store.$getItemOffset(index);
@@ -356,7 +356,7 @@ export const createScroller = (
356356
scheduleImperativeScroll(() => {
357357
return (
358358
offset +
359-
store.$getStartSpacerSize() +
359+
store._getStartSpacerSize() +
360360
store.$getItemOffset(index) +
361361
(align === "end"
362362
? store.$getItemSize(index) - store.$getViewportSize()
@@ -542,7 +542,7 @@ export const createWindowScroller = (
542542
$scrollToIndex(index, { align, smooth, offset = 0 } = {}) {
543543
if (!containerElement) return;
544544

545-
index = clamp(index, 0, store.$getItemsLength() - 1);
545+
index = clamp(index, 0, store._getItemsLength() - 1);
546546

547547
if (align === "nearest") {
548548
const itemOffset = store.$getItemOffset(index);

src/core/store.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ export type VirtualStore = {
106106
$isUnmeasuredItem(index: number): boolean;
107107
$getItemOffset(index: number): number;
108108
$getItemSize(index: number): number;
109-
$getItemsLength(): number;
109+
_getItemsLength(): number;
110110
$getScrollOffset(): number;
111111
$isScrolling(): boolean;
112112
$getViewportSize(): number;
113-
$getStartSpacerSize(): number;
113+
_getStartSpacerSize(): number;
114114
$getTotalSize(): number;
115115
$getJumpCount(): number;
116116
_flushJump(): [number, boolean];
@@ -219,11 +219,11 @@ export const createVirtualStore = (
219219
},
220220
$getItemOffset: getItemOffset,
221221
$getItemSize: getItemSize,
222-
$getItemsLength: () => cache._length,
222+
_getItemsLength: () => cache._length,
223223
$getScrollOffset: () => scrollOffset,
224224
$isScrolling: () => _scrollDirection !== SCROLL_IDLE,
225225
$getViewportSize: () => viewportSize,
226-
$getStartSpacerSize: () => startSpacerSize,
226+
_getStartSpacerSize: () => startSpacerSize,
227227
$getTotalSize: getTotalSize,
228228
$getJumpCount: () => jumpCount,
229229
_flushJump: () => {
@@ -397,15 +397,18 @@ export const createVirtualStore = (
397397
break;
398398
}
399399
case ACTION_ITEMS_LENGTH_CHANGE: {
400-
if (payload[1]) {
401-
applyJump(updateCacheLength(cache, payload[0], true));
402-
_scrollMode = SCROLL_BY_SHIFT;
403-
mutated = UPDATE_VIRTUAL_STATE;
404-
} else {
405-
updateCacheLength(cache, payload[0]);
406-
// https://github.com/inokawa/virtua/issues/552
407-
// https://github.com/inokawa/virtua/issues/557
408-
mutated = UPDATE_VIRTUAL_STATE;
400+
const [length, isShift] = payload;
401+
if (cache._length !== length) {
402+
if (isShift) {
403+
applyJump(updateCacheLength(cache, length, true));
404+
_scrollMode = SCROLL_BY_SHIFT;
405+
mutated = UPDATE_VIRTUAL_STATE;
406+
} else {
407+
updateCacheLength(cache, length);
408+
// https://github.com/inokawa/virtua/issues/552
409+
// https://github.com/inokawa/virtua/issues/557
410+
mutated = UPDATE_VIRTUAL_STATE;
411+
}
409412
}
410413
break;
411414
}

src/react/VGrid.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,8 @@ export const VGrid = forwardRef<VGridHandle, VGridProps>(
240240
];
241241
});
242242
// The elements length and cached items length are different just after element is added/removed.
243-
if (rowCount !== vStore.$getItemsLength()) {
244-
vStore.$update(ACTION_ITEMS_LENGTH_CHANGE, [rowCount]);
245-
}
246-
if (colCount !== hStore.$getItemsLength()) {
247-
hStore.$update(ACTION_ITEMS_LENGTH_CHANGE, [colCount]);
248-
}
243+
vStore.$update(ACTION_ITEMS_LENGTH_CHANGE, [rowCount]);
244+
hStore.$update(ACTION_ITEMS_LENGTH_CHANGE, [colCount]);
249245

250246
const vRerender = useRerender(vStore);
251247
const hRerender = useRerender(hStore);

src/react/Virtualizer.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,8 @@ export const Virtualizer = forwardRef<VirtualizerHandle, VirtualizerProps>(
219219
});
220220

221221
// The elements length and cached items length are different just after element is added/removed.
222-
if (count !== store.$getItemsLength()) {
223-
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, shift]);
224-
}
225-
if (startMargin !== store.$getStartSpacerSize()) {
226-
store.$update(ACTION_START_OFFSET_CHANGE, startMargin);
227-
}
222+
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, shift]);
223+
store.$update(ACTION_START_OFFSET_CHANGE, startMargin);
228224

229225
const rerender = useRerender(store);
230226

src/react/WindowVirtualizer.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,7 @@ export const WindowVirtualizer = forwardRef<
170170
];
171171
});
172172
// The elements length and cached items length are different just after element is added/removed.
173-
if (count !== store.$getItemsLength()) {
174-
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, shift]);
175-
}
173+
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, shift]);
176174

177175
const rerender = useRerender(store);
178176

src/solid/Virtualizer.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,7 @@ export const Virtualizer = <T,>(props: VirtualizerProps<T>): JSX.Element => {
242242
on(
243243
() => props.data.length,
244244
(count) => {
245-
if (count !== store.$getItemsLength()) {
246-
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, props.shift]);
247-
}
245+
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [count, props.shift]);
248246
}
249247
)
250248
);
@@ -253,9 +251,7 @@ export const Virtualizer = <T,>(props: VirtualizerProps<T>): JSX.Element => {
253251
on(
254252
() => props.startMargin || 0,
255253
(value) => {
256-
if (value !== store.$getStartSpacerSize()) {
257-
store.$update(ACTION_START_OFFSET_CHANGE, value);
258-
}
254+
store.$update(ACTION_START_OFFSET_CHANGE, value);
259255
}
260256
)
261257
);

src/solid/WindowVirtualizer.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ export const WindowVirtualizer = <T,>(
181181
on(
182182
() => props.data.length,
183183
(len) => {
184-
if (len !== store.$getItemsLength()) {
185-
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [len, props.shift]);
186-
}
184+
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [len, props.shift]);
187185
}
188186
)
189187
);

src/svelte/Virtualizer.svelte

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,11 @@
8686
});
8787
8888
$effect.pre(() => {
89-
if (data.length !== store.$getItemsLength()) {
90-
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [data.length, shift]);
91-
}
89+
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [data.length, shift]);
9290
});
9391
9492
$effect.pre(() => {
95-
if (startMargin !== store.$getStartSpacerSize()) {
96-
store.$update(ACTION_START_OFFSET_CHANGE, startMargin);
97-
}
93+
store.$update(ACTION_START_OFFSET_CHANGE, startMargin);
9894
});
9995
10096
let prevJumpCount: number | undefined;

src/svelte/WindowVirtualizer.svelte

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@
7878
});
7979
8080
$effect.pre(() => {
81-
if (data.length !== store.$getItemsLength()) {
82-
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [data.length, shift]);
83-
}
81+
store.$update(ACTION_ITEMS_LENGTH_CHANGE, [data.length, shift]);
8482
});
8583
8684
let prevJumpCount: number | undefined;

0 commit comments

Comments
 (0)