| 3 | import { getMemoOptions, memo } from '../utils' |
| 4 | |
| 5 | export function getCoreRowModel<TData extends RowData>(): ( |
| 6 | table: Table<TData>, |
| 7 | ) => () => RowModel<TData> { |
| 8 | return (table) => |
| 9 | memo( |
| 10 | () => [table.options.data], |
| 11 | ( |
| 12 | data, |
| 13 | ): { |
| 14 | rows: Row<TData>[] |
| 15 | flatRows: Row<TData>[] |
| 16 | rowsById: Record<string, Row<TData>> |
| 17 | } => { |
| 18 | const rowModel: RowModel<TData> = { |
| 19 | rows: [], |
| 20 | flatRows: [], |
| 21 | rowsById: {}, |
| 22 | } |
| 23 | |
| 24 | const accessRows = ( |
| 25 | originalRows: TData[], |
| 26 | depth = 0, |
| 27 | parentRow?: Row<TData>, |
| 28 | ): Row<TData>[] => { |
| 29 | const rows = [] as Row<TData>[] |
| 30 | |
| 31 | for (let i = 0; i < originalRows.length; i++) { |
| 32 | // This could be an expensive check at scale, so we should move it somewhere else, but where? |
| 33 | // if (!id) { |
| 34 | // if (process.env.NODE_ENV !== 'production') { |
| 35 | // throw new Error(`getRowId expected an ID, but got ${id}`) |
| 36 | // } |
| 37 | // } |
| 38 | |
| 39 | // Make the row |
| 40 | const row = createRow( |
| 41 | table, |
| 42 | table._getRowId(originalRows[i]!, i, parentRow), |
| 43 | originalRows[i]!, |
| 44 | i, |
| 45 | depth, |
| 46 | undefined, |
| 47 | parentRow?.id, |
| 48 | ) |
| 49 | |
| 50 | // Keep track of every row in a flat array |
| 51 | rowModel.flatRows.push(row) |
| 52 | // Also keep track of every row by its ID |
| 53 | rowModel.rowsById[row.id] = row |
| 54 | // Push table row into parent |
| 55 | rows.push(row) |
| 56 | |
| 57 | // Get the original subrows |
| 58 | if (table.options.getSubRows) { |
| 59 | row.originalSubRows = table.options.getSubRows( |
| 60 | originalRows[i]!, |
| 61 | i, |
| 62 | ) |