| 12 | * @return Object mapping cell index to cell metadata (size, offset) |
| 13 | */ |
| 14 | export function initCellMetadata({ |
| 15 | cellCount, |
| 16 | size |
| 17 | }: { |
| 18 | cellCount: number; |
| 19 | size: number | ((params: { index: number }) => number); |
| 20 | }): CellMetadata[] { |
| 21 | const sizeGetter = typeof size === "function" ? size : () => size; |
| 22 | |
| 23 | const cellMetadata: CellMetadata[] = []; |
| 24 | let offset = 0; |
| 25 | |
| 26 | for (let i = 0; i < cellCount; i++) { |
| 27 | const size = sizeGetter({ index: i }); |
| 28 | |
| 29 | if (size == null || isNaN(size)) { |
| 30 | throw Error(`Invalid size returned for cell ${i} of value ${size}`); |
| 31 | } |
| 32 | |
| 33 | cellMetadata[i] = { |
| 34 | size, |
| 35 | offset |
| 36 | }; |
| 37 | |
| 38 | offset += size; |
| 39 | } |
| 40 | |
| 41 | return cellMetadata; |
| 42 | } |