MCPcopy Create free account
hub / github.com/bvaughn/react-virtualized / PositionCache

Class PositionCache

lib/components/Masonry/PositionCache.ts:8–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6// O(log(n)) lookup of cells to render for a given viewport size
7// O(1) lookup of shortest measured column (so we know when to enter phase 1)
8export class PositionCache {
9 // Tracks the height of each column
10 _columnSizeMap: { [x: number]: number } = {};
11
12 // Store tops and bottoms of each cell for fast intersection lookup.
13 _intervalTree = createIntervalTree();
14
15 // Maps cell index to x coordinates for quick lookup.
16 _leftMap: { [index: number]: number } = {};
17
18 // Maps cell index to the y coordinate of its bottom edge.
19 _bottomMap: { [index: number]: number } = {};
20
21 estimateTotalHeight(
22 cellCount: number,
23 columnCount: number,
24 defaultCellHeight: number
25 ): number {
26 const unmeasuredCellCount = cellCount - this.count;
27 if (unmeasuredCellCount < 0) {
28 // Some of the cached cells have since been removed (cellCount shrank);
29 // only the remaining cells (all of which have been positioned) contribute to the height.
30 let size = 0;
31 for (let index = 0; index < cellCount; index++) {
32 const bottom = this._bottomMap[index];
33 if (bottom !== undefined && bottom > size) {
34 size = bottom;
35 }
36 }
37 return size;
38 }
39
40 return (
41 this.tallestColumnSize +
42 Math.ceil(unmeasuredCellCount / columnCount) * defaultCellHeight
43 );
44 }
45
46 // Render all cells visible within the viewport range defined.
47 range(
48 scrollTop: number,
49 clientHeight: number,
50 renderCallback: RenderCallback
51 ): void {
52 this._intervalTree.queryInterval(
53 scrollTop,
54 scrollTop + clientHeight,
55 ([top, , index]) =>
56 renderCallback(index as number, this._leftMap[index as number], top)
57 );
58 }
59
60 setPosition(index: number, left: number, top: number, height: number): void {
61 this._intervalTree.insert([top, top + height, index]);
62 this._leftMap[index] = left;
63 this._bottomMap[index] = top + height;
64
65 const columnSizeMap = this._columnSizeMap;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected