Skip to content

Commit 1db8b4c

Browse files
committed
Removed a few things
1 parent 0c490be commit 1db8b4c

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

lib/core/createCachedBounds.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ describe("createCachedBounds", () => {
6262

6363
expect(cachedBounds.size).toBe(0);
6464

65+
expect(cachedBounds.getEstimatedSize()).toEqual(0);
66+
6567
expect(() => {
6668
cachedBounds.getItemBounds(1);
6769
}).toThrow("Invalid index 1");

lib/core/createCachedBounds.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ export function createCachedBounds<Props extends object>({
1414

1515
const api = {
1616
getEstimatedSize() {
17-
const lastBounds = cache.get(cache.size - 1);
18-
if (lastBounds) {
19-
return (lastBounds.scrollOffset + lastBounds.size) / cache.size;
17+
if (itemCount === 0) {
18+
return 0;
2019
} else {
21-
const firstBounds = api.getItemBounds(0);
22-
return firstBounds.size * itemCount;
20+
const bounds = api.getItemBounds(cache.size === 0 ? 0 : cache.size - 1);
21+
assert(bounds, "Unexpected bounds cache miss");
22+
23+
return (bounds.scrollOffset + bounds.size) / cache.size;
2324
}
2425
},
2526
getItemBounds(index: number) {
@@ -68,9 +69,6 @@ export function createCachedBounds<Props extends object>({
6869

6970
return bounds;
7071
},
71-
hasItemBounds(index: number) {
72-
return cache.has(index);
73-
},
7472
get size() {
7573
return cache.size;
7674
}

lib/core/getEstimatedSize.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ describe("getEstimatedSize", () => {
2121
).toBe(0);
2222
});
2323

24+
test("should return an estimate based on the first row if no sizes have yet been measured", () => {
25+
expect(
26+
getEstimatedSize({
27+
cachedBounds: createCachedBounds({
28+
itemCount: 1,
29+
itemProps: EMPTY_OBJECT,
30+
itemSize
31+
}),
32+
itemCount: 1,
33+
itemSize
34+
})
35+
).toBe(10);
36+
});
37+
2438
test("should return an average size based on the first item if no measurements have been taken", () => {
2539
expect(
2640
getEstimatedSize({

lib/core/getEstimatedSize.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { CachedBounds, SizeFunction } from "./types";
2-
import { assert } from "../utils/assert";
32

43
export function getEstimatedSize<Props extends object>({
54
cachedBounds,
@@ -18,7 +17,6 @@ export function getEstimatedSize<Props extends object>({
1817
const bounds = cachedBounds.getItemBounds(
1918
cachedBounds.size === 0 ? 0 : cachedBounds.size - 1
2019
);
21-
assert(bounds !== undefined, "Unexpected bounds cache miss");
2220

2321
const averageItemSize =
2422
(bounds.scrollOffset + bounds.size) / cachedBounds.size;

lib/core/getOffsetForIndex.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Align } from "../types";
2-
import { assert } from "../utils/assert";
32
import { getEstimatedSize } from "./getEstimatedSize";
43
import type { CachedBounds, SizeFunction } from "./types";
54

@@ -25,12 +24,8 @@ export function getOffsetForIndex<Props extends object>({
2524
itemCount,
2625
itemSize
2726
});
28-
if (estimatedTotalSize === undefined) {
29-
return 0;
30-
}
3127

3228
const bounds = cachedBounds.getItemBounds(index);
33-
assert(bounds, `Unexpected cache miss for index ${index}`);
3429

3530
const maxOffset = Math.max(
3631
0,

lib/core/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export type Bounds = {
66
export type CachedBounds = {
77
getEstimatedSize(): number;
88
getItemBounds(index: number): Bounds;
9-
hasItemBounds(index: number): boolean;
109
size: number;
1110
};
1211

0 commit comments

Comments
 (0)