import { createRow } from '../core/row' import { Table, Row, RowModel, RowData } from '../types' import { memo } from '../utils' export function getCoreRowModel(): ( table: Table ) => () => RowModel { return table => memo( () => [table.options.data], ( data ): { rows: Row[] flatRows: Row[] rowsById: Record> } => { const rowModel: RowModel = { rows: [], flatRows: [], rowsById: {}, } const accessRows = ( originalRows: TData[], depth = 0, parent?: Row ): Row[] => { const rows = [] as Row[] for (let i = 0; i < originalRows.length; i++) { // This could be an expensive check at scale, so we should move it somewhere else, but where? // if (!id) { // if (process.env.NODE_ENV !== 'production') { // throw new Error(`getRowId expected an ID, but got ${id}`) // } // } // Make the row const row = createRow( table, table._getRowId(originalRows[i]!, i, parent), originalRows[i]!, i, depth ) // Keep track of every row in a flat array rowModel.flatRows.push(row) // Also keep track of every row by its ID rowModel.rowsById[row.id] = row // Push table row into parent rows.push(row) // Get the original subrows if (table.options.getSubRows) { row.originalSubRows = table.options.getSubRows( originalRows[i]!, i ) // Then recursively access them if (row.originalSubRows?.length) { row.subRows = accessRows(row.originalSubRows, depth + 1, row) } } } return rows } rowModel.rows = accessRows(data) return rowModel }, { key: process.env.NODE_ENV === 'development' && 'getRowModel', debug: () => table.options.debugAll ?? table.options.debugTable, onChange: () => { table._autoResetPageIndex() }, } ) }