Skip to content

Commit

Permalink
use core apis by builder pattern reference to fix some performance is…
Browse files Browse the repository at this point in the history
…sues
  • Loading branch information
KevinVandy committed Oct 23, 2024
1 parent ba5e032 commit b015b47
Show file tree
Hide file tree
Showing 43 changed files with 209 additions and 335 deletions.
4 changes: 2 additions & 2 deletions examples/lit/virtualized-rows/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { styleMap } from 'lit/directives/style-map.js'
import { Ref, createRef, ref } from 'lit/directives/ref.js'
import { VirtualizerController } from '@tanstack/lit-virtual'
import { Person, makeData } from './makeData.ts'
import type { ColumnDef, Row } from '@tanstack/lit-table'
import type { ColumnDef } from '@tanstack/lit-table'

const columns: Array<ColumnDef<any, Person>> = [
{
Expand Down Expand Up @@ -149,7 +149,7 @@ class LitTableExample extends LitElement {
.getVirtualItems(),
(item) => item.key,
(item) => {
const row = rows[item.index] as Row<any, Person>
const row = rows[item.index]
return html`
<tr
style=${styleMap({
Expand Down
2 changes: 1 addition & 1 deletion examples/react/pagination/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
createSortedRowModel,
filterFns,
flexRender,
processingFns,
sortingFns,
tableFeatures,
processingFns,
useTable,
} from '@tanstack/react-table'
import { makeData } from './makeData'
Expand Down
6 changes: 3 additions & 3 deletions examples/react/sorting/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const rootElement = document.getElementById('root')
if (!rootElement) throw new Error('Failed to find the root element')

ReactDOM.createRoot(rootElement).render(
// <React.StrictMode>
<App />,
// </React.StrictMode>,
<React.StrictMode>
<App />
</React.StrictMode>,
)
6 changes: 3 additions & 3 deletions packages/table-core/src/core/cells/Cells.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { assignAPIs } from '../../utils'
import { cell_getContext, cell_getValue, cell_renderValue } from './Cells.utils'
import type { Table_Internal } from '../../types/Table'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type { Cell } from '../../types/Cell'

export const Cells: TableFeature = {
Expand All @@ -12,11 +12,11 @@ export const Cells: TableFeature = {
TValue extends CellData = CellData,
>(
cell: Cell<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
table: Table_Internal<TFeatures, TData>,
) => {
assignAPIs(cell, table, [
{
fn: () => cell_getValue(cell, table) as any,
fn: () => cell_getValue(cell),
},
{
fn: () => cell_renderValue(cell, table),
Expand Down
10 changes: 3 additions & 7 deletions packages/table-core/src/core/cells/Cells.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { row_getValue } from '../rows/Rows.utils'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
Expand All @@ -8,19 +7,16 @@ export function cell_getValue<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(
cell: Cell<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
): TValue {
return row_getValue(cell.row, table, cell.column.id) as TValue
>(cell: Cell<TFeatures, TData, TValue>): TValue {
return cell.row.getValue(cell.column.id)
}

export function cell_renderValue<
TFeatures extends TableFeatures,
TData extends RowData,
TValue extends CellData = CellData,
>(cell: Cell<TFeatures, TData, TValue>, table: Table<TFeatures, TData>) {
return cell_getValue(cell, table) ?? table.options.renderFallbackValue
return cell.getValue() ?? table.options.renderFallbackValue
}

export function cell_getContext<
Expand Down
19 changes: 9 additions & 10 deletions packages/table-core/src/core/columns/Columns.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { assignAPIs } from '../../utils'
import { table_getState } from '../table/Tables.utils'
import {
column_getFlatColumns,
column_getLeafColumns,
tableGetDefaultColumnDef,
table_getAllColumns,
table_getAllFlatColumns,
table_getAllFlatColumnsById,
table_getAllLeafColumns,
table_getColumn,
table_getDefaultColumnDef,
} from './Columns.utils'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
import type { Table_Internal } from '../../types/Table'
import type { Column } from '../../types/Column'

export const Columns: TableFeature = {
Expand All @@ -22,7 +21,7 @@ export const Columns: TableFeature = {
TValue extends CellData = CellData,
>(
column: Column<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
table: Table_Internal<TFeatures, TData>,
) => {
assignAPIs(column, table, [
{
Expand All @@ -32,8 +31,8 @@ export const Columns: TableFeature = {
{
fn: () => column_getLeafColumns(column, table),
memoDeps: () => [
table_getState(table).columnOrder,
table_getState(table).grouping,
table.getState().columnOrder,
table.getState().grouping,
table.options.columns,
table.options.groupedColumnMode,
],
Expand All @@ -42,11 +41,11 @@ export const Columns: TableFeature = {
},

constructTable: <TFeatures extends TableFeatures, TData extends RowData>(
table: Table<TFeatures, TData>,
table: Table_Internal<TFeatures, TData>,
) => {
assignAPIs(table, table, [
{
fn: () => tableGetDefaultColumnDef(table),
fn: () => table_getDefaultColumnDef(table),
memoDeps: () => [table.options.defaultColumn],
},
{
Expand All @@ -64,8 +63,8 @@ export const Columns: TableFeature = {
{
fn: () => table_getAllLeafColumns(table),
memoDeps: () => [
table_getState(table).columnOrder,
table_getState(table).grouping,
table.getState().columnOrder,
table.getState().grouping,
table.options.columns,
table.options.groupedColumnMode,
],
Expand Down
21 changes: 8 additions & 13 deletions packages/table-core/src/core/columns/Columns.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export function column_getFlatColumns<
>(
column: Column<TFeatures, TData, TValue>,
): Array<Column<TFeatures, TData, TValue>> {
return [
column,
...column.columns.flatMap((col) => column_getFlatColumns(col)),
]
return [column, ...column.columns.flatMap((col) => col.getFlatColumns())]
}

export function column_getLeafColumns<
Expand All @@ -33,7 +30,7 @@ export function column_getLeafColumns<
): Array<Column<TFeatures, TData, TValue>> {
if (column.columns.length) {
const leafColumns = column.columns.flatMap(
(col) => column_getLeafColumns(col, table), // recursive
(col) => col.getLeafColumns(), // recursive
)

return table_getOrderColumnsFn(table)(leafColumns) as any
Expand All @@ -42,7 +39,7 @@ export function column_getLeafColumns<
return [column]
}

export function tableGetDefaultColumnDef<
export function table_getDefaultColumnDef<
TFeatures extends TableFeatures,
TData extends RowData,
>(
Expand Down Expand Up @@ -104,9 +101,7 @@ export function table_getAllFlatColumns<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>): Array<Column<TFeatures, TData, unknown>> {
return table_getAllColumns(table).flatMap((column) =>
column_getFlatColumns(column),
)
return table.getAllColumns().flatMap((column) => column.getFlatColumns())
}

export function table_getAllFlatColumnsById<
Expand All @@ -115,7 +110,7 @@ export function table_getAllFlatColumnsById<
>(
table: Table<TFeatures, TData>,
): Record<string, Column<TFeatures, TData, unknown>> {
return table_getAllFlatColumns(table).reduce(
return table.getAllFlatColumns().reduce(
(acc, column) => {
acc[column.id] = column
return acc
Expand All @@ -128,8 +123,8 @@ export function table_getAllLeafColumns<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>): Array<Column<TFeatures, TData, unknown>> {
const leafColumns = table_getAllColumns(table).flatMap(
(c) => column_getLeafColumns(c, table), // recursive
const leafColumns = table.getAllColumns().flatMap(
(c) => c.getLeafColumns(), // recursive
)
return table_getOrderColumnsFn(table)(leafColumns)
}
Expand All @@ -141,7 +136,7 @@ export function table_getColumn<
table: Table<TFeatures, TData>,
columnId: string,
): Column<TFeatures, TData, unknown> | undefined {
const column = table_getAllFlatColumnsById(table)[columnId]
const column = table.getAllFlatColumnsById()[columnId]

if (process.env.NODE_ENV !== 'production' && !column) {
console.warn(`[Table] Column with id '${columnId}' does not exist.`)
Expand Down
3 changes: 1 addition & 2 deletions packages/table-core/src/core/columns/constructColumn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { tableGetDefaultColumnDef } from './Columns.utils'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Table } from '../../types/Table'
Expand All @@ -20,7 +19,7 @@ export function constructColumn<
depth: number,
parent?: Column<TFeatures, TData, TValue>,
): Column<TFeatures, TData, TValue> {
const defaultColumn = tableGetDefaultColumnDef(table)
const defaultColumn = table.getDefaultColumnDef()

const resolvedColumnDef = {
...defaultColumn,
Expand Down
24 changes: 15 additions & 9 deletions packages/table-core/src/core/headers/Headers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assignAPIs } from '../../utils'
import { table_getState } from '../table/Tables.utils'
import {
table_getCenterHeaderGroups,
table_getLeftHeaderGroups,
Expand Down Expand Up @@ -27,9 +26,16 @@ export const Headers: TableFeature = {
header: Header<TFeatures, TData, TValue>,
table: Table<TFeatures, TData>,
): void => {
header.getLeafHeaders = () => header_getLeafHeaders(header)

header.getContext = () => header_getContext(header, table)
assignAPIs(header, table, [
{
fn: () => header_getLeafHeaders(header),
memoDeps: () => [table.options.columns],
},
{
fn: () => header_getContext(header, table),
memoDeps: () => [table.options.columns],
},
])
},

constructTable: <TFeatures extends TableFeatures, TData extends RowData>(
Expand All @@ -40,19 +46,19 @@ export const Headers: TableFeature = {
fn: () => table_getHeaderGroups(table),
memoDeps: () => [
table.options.columns,
table_getState(table).columnOrder,
table_getState(table).grouping,
table_getState(table).columnPinning,
table.getState().columnOrder,
table.getState().grouping,
table.getState().columnPinning,
table.options.groupedColumnMode,
],
},
{
fn: () => table_getFooterGroups(table),
memoDeps: () => [table_getHeaderGroups(table)],
memoDeps: () => [table.getHeaderGroups()],
},
{
fn: () => table_getFlatHeaders(table),
memoDeps: () => [table_getHeaderGroups(table)],
memoDeps: () => [table.getHeaderGroups()],
},
{
fn: () => table_getLeafHeaders(table),
Expand Down
12 changes: 5 additions & 7 deletions packages/table-core/src/core/headers/Headers.utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { table_getAllColumns } from '../columns/Columns.utils'
import { table_getVisibleLeafColumns } from '../../features/column-visibility/ColumnVisibility.utils'
import { table_getState } from '../table/Tables.utils'
import {
getDefaultColumnPinningState,
table_getCenterHeaderGroups,
table_getLeftHeaderGroups,
table_getRightHeaderGroups,
} from '../../features/column-pinning/ColumnPinning.utils'
import { table_getVisibleLeafColumns } from '../../features/column-visibility/ColumnVisibility.utils'
import { buildHeaderGroups } from './buildHeaderGroups'
import type { Header } from '../../types/Header'
import type { RowData } from '../../types/type-utils'
Expand Down Expand Up @@ -50,8 +48,8 @@ export function table_getHeaderGroups<
TData extends RowData,
>(table: Table<TFeatures, TData>) {
const { left, right } =
table_getState(table).columnPinning ?? getDefaultColumnPinningState()
const allColumns = table_getAllColumns(table)
table.getState().columnPinning ?? getDefaultColumnPinningState()
const allColumns = table.getAllColumns()
const leafColumns = table_getVisibleLeafColumns(table)

const leftColumns = left
Expand Down Expand Up @@ -79,15 +77,15 @@ export function table_getFooterGroups<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>) {
const headerGroups = table_getHeaderGroups(table)
const headerGroups = table.getHeaderGroups()
return [...headerGroups].reverse()
}

export function table_getFlatHeaders<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table<TFeatures, TData>) {
const headerGroups = table_getHeaderGroups(table)
const headerGroups = table.getHeaderGroups()
return headerGroups
.map((headerGroup) => {
return headerGroup.headers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { constructRow } from '../rows/constructRow'
import { isDev, tableMemo } from '../../utils'
import { table_getRowId } from '../rows/Rows.utils'
import { table_autoResetPageIndex } from '../../features/row-pagination/RowPagination.utils'
import type { RowModel } from './RowModels.types'
import type { RowData } from '../../types/type-utils'
Expand Down Expand Up @@ -52,7 +51,7 @@ function _createCoreRowModel<
// Make the row
const row = constructRow(
table,
table_getRowId(originalRow, table, i, parentRow),
table.getRowId(originalRow, i, parentRow),
originalRow,
i,
depth,
Expand Down
5 changes: 2 additions & 3 deletions packages/table-core/src/core/rows/Rows.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assignAPIs } from '../../utils'
import { table_getAllLeafColumns } from '../columns/Columns.utils'
import {
row_getAllCells,
row_getAllCellsByColumnId,
Expand All @@ -25,11 +24,11 @@ export const Rows: TableFeature = {
assignAPIs(row, table, [
{
fn: () => row_getAllCellsByColumnId(row, table),
memoDeps: () => [row_getAllCells(row, table)],
memoDeps: () => [row.getAllCells()],
},
{
fn: () => row_getAllCells(row, table),
memoDeps: () => [table_getAllLeafColumns(table)],
memoDeps: () => [table.getAllLeafColumns()],
},
{
fn: () => row_getLeafRows(row),
Expand Down
Loading

0 comments on commit b015b47

Please sign in to comment.