import { constructRow } from '../rows/constructRow' import { makeObjectMap, skipFirstRun, tableMemo } from '../../utils' import { autoResetCellSelection, autoResetExpanded, autoResetPageIndex, autoResetSorting, } from '../featureStubs' import type { Table_Internal } from '../../types/Table' import type { RowModel } from './coreRowModelsFeature.types' import type { TableFeatures } from '../../types/TableFeatures' import type { Row } from '../../types/Row' import type { RowData } from '../../types/type-utils' /** * Creates a memoized core row model factory. * * The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline. */ export function createCoreRowModel< TFeatures extends TableFeatures, TData extends RowData, >(): ( table: Table_Internal, ) => () => RowModel { return (table) => { return tableMemo({ feature: 'coreRowModelsFeature', table, fnName: 'table.getCoreRowModel', memoDeps: () => [table.options.data], fn: () => _createCoreRowModel(table, table.options.data), onAfterUpdate: skipFirstRun(() => { autoResetExpanded(table) autoResetPageIndex(table) autoResetSorting(table) autoResetCellSelection(table) }), }) } } function accessRows( table: Table_Internal, rowModel: RowModel, originalRows: ReadonlyArray, depth = 0, parentRow?: Row, ): Array> { const rows = [] as Array> for (let i = 0; i < originalRows.length; i++) { const originalRow = originalRows[i]! // Make the row const row = constructRow( table, table.getRowId(originalRow, i, parentRow), originalRow, i, depth, undefined, parentRow?.id, ) // 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(originalRow, i) // Then recursively access them if (row.originalSubRows?.length) { row.subRows = accessRows( table, rowModel, row.originalSubRows, depth + 1, row, ) } } } return rows } function _createCoreRowModel< TFeatures extends TableFeatures, TData extends RowData, >( table: Table_Internal, data: ReadonlyArray, ): { rows: Array> flatRows: Array> rowsById: Record> } { const rowModel: RowModel = { rows: [], flatRows: [], rowsById: makeObjectMap(), } rowModel.rows = accessRows(table, rowModel, data) return rowModel }