Skip to content

Commit 0d6622a

Browse files
committed
Separate cache from store
1 parent 0bf6673 commit 0d6622a

13 files changed

Lines changed: 28 additions & 54 deletions

src/core/cache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type InternalCacheSnapshot, type ItemsRange } from "./types.js";
1+
import { CacheSnapshot, type ItemsRange } from "./types.js";
22
import { clamp, floor, max, min, sort } from "./utils.js";
33

44
type Writeable<T> = {
@@ -175,8 +175,8 @@ export const estimateDefaultItemSize = (
175175
*/
176176
export const initCache = (
177177
length: number,
178-
itemSize: number,
179-
snapshot?: InternalCacheSnapshot
178+
itemSize: number = 40,
179+
snapshot?: CacheSnapshot
180180
): Cache => {
181181
return {
182182
_defaultItemSize: snapshot ? snapshot[1] : itemSize,
@@ -197,7 +197,7 @@ export const initCache = (
197197
/**
198198
* @internal
199199
*/
200-
export const takeCacheSnapshot = (cache: Cache): InternalCacheSnapshot => {
200+
export const takeCacheSnapshot = (cache: Cache): CacheSnapshot => {
201201
return [cache._sizes.slice(), cache._defaultItemSize];
202202
};
203203

src/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export {
99
getScrollSize,
1010
type StateVersion,
1111
} from "./store.js";
12+
export { initCache } from "./cache.js";
1213
export {
1314
createScroller,
1415
createWindowScroller,

src/core/store.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
initCache,
32
getItemSize as _getItemSize,
43
getItemOffset as _getItemOffset,
54
UNCACHED,
@@ -9,14 +8,10 @@ import {
98
computeRange,
109
takeCacheSnapshot,
1110
findIndex,
11+
type Cache,
1212
} from "./cache.js";
1313
import { isIOSWebKit } from "./environment.js";
14-
import type {
15-
CacheSnapshot,
16-
InternalCacheSnapshot,
17-
ItemResize,
18-
ItemsRange,
19-
} from "./types.js";
14+
import type { CacheSnapshot, ItemResize, ItemsRange } from "./types.js";
2015
import { abs, max, min, NULL } from "./utils.js";
2116

2217
const MAX_INT_32 = 0x7fffffff;
@@ -117,10 +112,8 @@ export type VirtualStore = {
117112
* @internal
118113
*/
119114
export const createVirtualStore = (
120-
elementsCount: number,
121-
itemSize: number = 40,
115+
cache: Cache,
122116
ssrCount: number = 0,
123-
cacheSnapshot?: CacheSnapshot | undefined,
124117
shouldAutoEstimateItemSize: boolean = false
125118
): VirtualStore => {
126119
let isSSR = !!ssrCount;
@@ -137,11 +130,6 @@ export const createVirtualStore = (
137130
let _prevRange: ItemsRange = [0, isSSR ? max(ssrCount - 1, 0) : -1];
138131
let _totalMeasuredSize = 0;
139132

140-
const cache = initCache(
141-
elementsCount,
142-
itemSize,
143-
cacheSnapshot as unknown as InternalCacheSnapshot | undefined
144-
);
145133
const subscribers = new Set<[number, Subscriber]>();
146134
const getRelativeScrollOffset = () => scrollOffset - startSpacerSize;
147135
const getVisibleOffset = () => getRelativeScrollOffset() + pendingJump + jump;

src/core/types.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ export type ItemResize = Readonly<[index: number, size: number]>;
33
/** @internal */
44
export type ItemsRange = Readonly<[startIndex: number, endIndex: number]>;
55

6-
/** @internal */
7-
export type InternalCacheSnapshot = [sizes: number[], defaultSize: number];
8-
9-
declare const cacheSymbol: unique symbol;
106
/**
117
* Serializable cache snapshot.
128
*
139
* **This is not intended to be modified by users. And it is not guaranteed to work if you pass it to the different version of this package.**
1410
*/
15-
export interface CacheSnapshot {
16-
[cacheSymbol]: never;
17-
}
11+
export type CacheSnapshot = [sizes: number[], defaultSize: number];
1812

1913
export type ScrollToIndexAlign = "start" | "center" | "end" | "nearest";
2014

src/react/VGrid.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
isRTLDocument,
2323
UPDATE_SCROLL_EVENT,
2424
UPDATE_SCROLL_END_EVENT,
25+
initCache,
2526
} from "../core/index.js";
2627
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect.js";
2728
import { refKey } from "./utils.js";
@@ -275,13 +276,11 @@ export const VGrid = forwardRef<VGridHandle, VGridProps>(
275276
): ReactElement => {
276277
const [rowStore, colStore, resizer, scroller] = useStatic(() => {
277278
const _rowStore = createVirtualStore(
278-
rowCount,
279-
cellHeight,
279+
initCache(rowCount, cellHeight),
280280
initialRowCount
281281
);
282282
const _colStore = createVirtualStore(
283-
colCount,
284-
cellWidth,
283+
initCache(colCount, cellWidth),
285284
initialColCount
286285
);
287286
return [

src/react/Virtualizer.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
ScrollToIndexOpts,
2424
microtask,
2525
sort,
26+
initCache,
2627
} from "../core/index.js";
2728
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect.js";
2829
import { getKey, refKey } from "./utils.js";
@@ -204,10 +205,8 @@ export const Virtualizer = forwardRef<VirtualizerHandle, VirtualizerProps>(
204205
const [store, resizer, scroller, isHorizontal] = useStatic(() => {
205206
const _isHorizontal = !!horizontalProp;
206207
const _store = createVirtualStore(
207-
count,
208-
itemSize,
208+
initCache(count, itemSize, cache),
209209
ssrCount,
210-
cache,
211210
!itemSize
212211
);
213212
return [

src/react/WindowVirtualizer.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
createWindowResizer,
1919
CacheSnapshot,
2020
ScrollToIndexOpts,
21+
initCache,
2122
} from "../core/index.js";
2223
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect.js";
2324
import { getKey, refKey } from "./utils.js";
@@ -154,10 +155,8 @@ export const WindowVirtualizer = forwardRef<
154155
const [store, resizer, scroller, isHorizontal] = useStatic(() => {
155156
const _isHorizontal = !!horizontalProp;
156157
const _store = createVirtualStore(
157-
count,
158-
itemSize,
158+
initCache(count, itemSize, cache),
159159
ssrCount,
160-
cache,
161160
!itemSize
162161
);
163162

src/solid/Virtualizer.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
ScrollToIndexOpts,
3232
CacheSnapshot,
3333
sort,
34+
initCache,
3435
} from "../core/index.js";
3536
import { ListItem } from "./ListItem.js";
3637
import { isSameRange } from "./utils.js";
@@ -178,10 +179,8 @@ export const Virtualizer = <T,>(props: VirtualizerProps<T>): JSX.Element => {
178179
);
179180

180181
const store = createVirtualStore(
181-
props.data.length,
182-
itemSize,
182+
initCache(props.data.length, itemSize, cache),
183183
undefined,
184-
cache,
185184
!itemSize
186185
);
187186
const resizer = createResizer(store, horizontal);

src/solid/WindowVirtualizer.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
ItemsRange,
2525
ScrollToIndexOpts,
2626
CacheSnapshot,
27+
initCache,
2728
} from "../core/index.js";
2829
import { ListItem } from "./ListItem.js";
2930
import { isSameRange } from "./utils.js";
@@ -124,10 +125,8 @@ export const WindowVirtualizer = <T,>(
124125
} = props;
125126

126127
const store = createVirtualStore(
127-
props.data.length,
128-
itemSize,
128+
initCache(props.data.length, itemSize, cache),
129129
undefined,
130-
cache,
131130
!itemSize
132131
);
133132
const resizer = createWindowResizer(store, horizontal);

src/svelte/Virtualizer.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
createScroller,
1212
createVirtualStore,
1313
getScrollSize as _getScrollSize,
14+
initCache,
1415
} from "../core/index.js";
1516
import { defaultGetKey, styleToString, iterRange } from "./utils.js";
1617
import ListItem from "./ListItem.svelte";
@@ -39,9 +40,7 @@
3940
}: Props = $props();
4041
4142
const store = createVirtualStore(
42-
data.length,
43-
itemSize,
44-
undefined,
43+
initCache(data.length, itemSize),
4544
undefined,
4645
!itemSize
4746
);

0 commit comments

Comments
 (0)