Skip to content

Commit d68f5f1

Browse files
committed
work on memo stuff more
1 parent e50a7a7 commit d68f5f1

File tree

12 files changed

+188
-142
lines changed

12 files changed

+188
-142
lines changed

examples/lit/basic/src/main.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ class LitTableExample extends LitElement {
113113
debugTable: true,
114114
})
115115

116-
console.log(table)
117-
118116
return html`
119117
<table>
120118
<thead>

examples/react/column-ordering/src/main.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ function App() {
103103
)
104104
}
105105

106-
console.log(table.getFlatHeaders())
107-
108106
return (
109107
<div className="p-2">
110108
<div className="inline-block border border-black shadow rounded">

packages/table-core/src/core/columns/Columns.utils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { callMemoOrStaticFn, isDev } from '../../utils'
12
import { table_getOrderColumnsFn } from '../../features/column-ordering/ColumnOrdering.utils'
2-
import { isDev } from '../../utils'
33
import { constructColumn } from './constructColumn'
44
import type { Table_Internal } from '../../types/Table'
55
import type { CellData, RowData } from '../../types/type-utils'
@@ -33,7 +33,10 @@ export function column_getLeafColumns<
3333
(col) => col.getLeafColumns(), // recursive
3434
)
3535

36-
return table_getOrderColumnsFn(column.table)(leafColumns as any) as any
36+
return callMemoOrStaticFn(
37+
column.table,
38+
table_getOrderColumnsFn,
39+
)(leafColumns as any) as any
3740
}
3841

3942
return [column]
@@ -132,7 +135,10 @@ export function table_getAllLeafColumns<
132135
const leafColumns = table.getAllColumns().flatMap(
133136
(c) => c.getLeafColumns(), // recursive
134137
)
135-
return table_getOrderColumnsFn(table)(leafColumns)
138+
return callMemoOrStaticFn(
139+
table,
140+
table_getOrderColumnsFn,
141+
)(leafColumns as any) as any
136142
}
137143

138144
export function table_getColumn<

packages/table-core/src/core/table/Tables.types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ export interface TableOptions_Table<
5353
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
5454
*/
5555
debugAll?: boolean
56+
/**
57+
* Set this option to `true` to output cache debugging information to the console.
58+
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugcache)
59+
* [Guide](https://tanstack.com/table/v8/docs/guide/tables)
60+
*/
61+
debugCache?: boolean
5662
/**
5763
* Set this option to `true` to output table debugging information to the console.
5864
* [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugtable)

packages/table-core/src/features/column-ordering/ColumnOrdering.utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { column_getVisibleLeafColumns } from '../column-visibility/ColumnVisibility.utils'
1+
import { table_getPinnedVisibleLeafColumns } from '../column-pinning/ColumnPinning.utils'
22
import type { GroupingState } from '../column-grouping/ColumnGrouping.types'
33
import type { CellData, RowData, Updater } from '../../types/type-utils'
44
import type { TableFeatures } from '../../types/TableFeatures'
@@ -19,7 +19,8 @@ export function column_getIndex<
1919
column: Column<TFeatures, TData, TValue>,
2020
position?: ColumnPinningPosition | 'center',
2121
) {
22-
const columns = column_getVisibleLeafColumns(column.table, position)
22+
const { table } = column
23+
const columns = table_getPinnedVisibleLeafColumns(table, position)
2324
return columns.findIndex((d) => d.id === column.id)
2425
}
2526

@@ -31,7 +32,7 @@ export function column_getIsFirstColumn<
3132
column: Column<TFeatures, TData, TValue>,
3233
position?: ColumnPinningPosition | 'center',
3334
) {
34-
const columns = column_getVisibleLeafColumns(column.table, position)
35+
const columns = table_getPinnedVisibleLeafColumns(column.table, position)
3536
return columns[0]?.id === column.id
3637
}
3738

@@ -43,7 +44,7 @@ export function column_getIsLastColumn<
4344
column: Column<TFeatures, TData, TValue>,
4445
position?: ColumnPinningPosition | 'center',
4546
) {
46-
const columns = column_getVisibleLeafColumns(column.table, position)
47+
const columns = table_getPinnedVisibleLeafColumns(column.table, position)
4748
return columns[columns.length - 1]?.id === column.id
4849
}
4950

packages/table-core/src/features/column-pinning/ColumnPinning.types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,16 @@ export interface Table_ColumnPinning<
225225
* [Guide](https://tanstack.com/table/v8/docs/guide/column-pinning)
226226
*/
227227
setColumnPinning: (updater: Updater<ColumnPinningState>) => void
228+
/**
229+
*
230+
*/
231+
getPinnedLeafColumns: (
232+
position: ColumnPinningPosition | 'center',
233+
) => Array<Column<TFeatures, TData, unknown>>
234+
/**
235+
*
236+
*/
237+
getPinnedVisibleLeafColumns: (
238+
position: ColumnPinningPosition | 'center',
239+
) => Array<Column<TFeatures, TData, unknown>>
228240
}

packages/table-core/src/features/column-pinning/ColumnPinning.utils.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
table_getVisibleLeafColumns,
55
} from '../column-visibility/ColumnVisibility.utils'
66
import { buildHeaderGroups } from '../../core/headers/buildHeaderGroups'
7+
import { callMemoOrStaticFn } from '../../utils'
78
import type { Row } from '../../types/Row'
89
import type { CellData, RowData, Updater } from '../../types/type-utils'
910
import type { TableFeatures } from '../../types/TableFeatures'
@@ -346,7 +347,7 @@ export function table_getLeftLeafColumns<
346347
return left
347348
.map(
348349
(columnId) =>
349-
table.getAllColumns().find((column) => column.id === columnId)!,
350+
table.getAllLeafColumns().find((column) => column.id === columnId)!,
350351
)
351352
.filter(Boolean)
352353
}
@@ -360,7 +361,7 @@ export function table_getRightLeafColumns<
360361
return right
361362
.map(
362363
(columnId) =>
363-
table.getAllColumns().find((column) => column.id === columnId)!,
364+
table.getAllLeafColumns().find((column) => column.id === columnId)!,
364365
)
365366
.filter(Boolean)
366367
}
@@ -372,7 +373,23 @@ export function table_getCenterLeafColumns<
372373
const { left, right } =
373374
table.options.state?.columnPinning ?? getDefaultColumnPinningState()
374375
const leftAndRight: Array<string> = [...left, ...right]
375-
return table.getAllColumns().filter((d) => !leftAndRight.includes(d.id))
376+
return table.getAllLeafColumns().filter((d) => !leftAndRight.includes(d.id))
377+
}
378+
379+
export function table_getPinnedLeafColumns<
380+
TFeatures extends TableFeatures,
381+
TData extends RowData,
382+
>(
383+
table: Table_Internal<TFeatures, TData>,
384+
position: ColumnPinningPosition | 'center',
385+
) {
386+
return !position
387+
? table.getAllLeafColumns()
388+
: position === 'left'
389+
? callMemoOrStaticFn(table, table_getLeftLeafColumns)
390+
: position === 'right'
391+
? callMemoOrStaticFn(table, table_getRightLeafColumns)
392+
: callMemoOrStaticFn(table, table_getCenterLeafColumns)
376393
}
377394

378395
// visible leaf columns
@@ -403,3 +420,19 @@ export function table_getCenterVisibleLeafColumns<
403420
column_getIsVisible(column),
404421
)
405422
}
423+
424+
export function table_getPinnedVisibleLeafColumns<
425+
TFeatures extends TableFeatures,
426+
TData extends RowData,
427+
>(
428+
table: Table_Internal<TFeatures, TData>,
429+
position?: ColumnPinningPosition | 'center',
430+
) {
431+
return !position
432+
? callMemoOrStaticFn(table, table_getVisibleLeafColumns)
433+
: position === 'left'
434+
? callMemoOrStaticFn(table, table_getLeftVisibleLeafColumns)
435+
: position === 'right'
436+
? callMemoOrStaticFn(table, table_getRightVisibleLeafColumns)
437+
: callMemoOrStaticFn(table, table_getCenterVisibleLeafColumns)
438+
}

packages/table-core/src/features/column-sizing/ColumnSizing.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { assignAPIs, makeStateUpdater } from '../../utils'
2-
import { column_getVisibleLeafColumns } from '../column-visibility/ColumnVisibility.utils'
1+
import { assignAPIs, callMemoOrStaticFn, makeStateUpdater } from '../../utils'
2+
import { table_getPinnedVisibleLeafColumns } from '../column-pinning/ColumnPinning.utils'
33
import {
44
column_getAfter,
55
column_getSize,
@@ -21,13 +21,12 @@ import type { TableState_All } from '../../types/TableState'
2121
import type {
2222
ColumnDef_ColumnSizing,
2323
ColumnSizingDefaultOptions,
24-
Column_ColumnSizing,
2524
Header_ColumnSizing,
2625
} from './ColumnSizing.types'
2726
import type { CellData, RowData } from '../../types/type-utils'
2827
import type { TableFeature, TableFeatures } from '../../types/TableFeatures'
2928
import type { Header } from '../../types/Header'
30-
import type { Column } from '../../types/Column'
29+
import type { Column_Internal } from '../../types/Column'
3130

3231
/**
3332
* The Column Sizing feature adds column sizing state and APIs to the table, header, and column objects.
@@ -66,7 +65,7 @@ export const ColumnSizing: TableFeature = {
6665
TData extends RowData,
6766
TValue extends CellData = CellData,
6867
>(
69-
column: Column<TFeatures, TData, TValue> & Partial<Column_ColumnSizing>,
68+
column: Column_Internal<TFeatures, TData, TValue>,
7069
): void => {
7170
assignAPIs(column, [
7271
{
@@ -76,15 +75,23 @@ export const ColumnSizing: TableFeature = {
7675
fn: (position) => column_getStart(column, position),
7776
memoDeps: (position) => [
7877
position,
79-
column_getVisibleLeafColumns(column.table, position),
78+
callMemoOrStaticFn(
79+
column.table,
80+
table_getPinnedVisibleLeafColumns,
81+
position
82+
),
8083
column.table.options.state?.columnSizing,
8184
],
8285
},
8386
{
8487
fn: (position) => column_getAfter(column, position),
8588
memoDeps: (position) => [
8689
position,
87-
column_getVisibleLeafColumns(column.table, position),
90+
callMemoOrStaticFn(
91+
column.table,
92+
table_getPinnedVisibleLeafColumns,
93+
position,
94+
),
8895
column.table.options.state?.columnSizing,
8996
],
9097
},

packages/table-core/src/features/column-sizing/ColumnSizing.utils.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import {
22
table_getCenterHeaderGroups,
33
table_getLeftHeaderGroups,
4+
table_getPinnedVisibleLeafColumns,
45
table_getRightHeaderGroups,
56
} from '../column-pinning/ColumnPinning.utils'
67
import { column_getIndex } from '../column-ordering/ColumnOrdering.utils'
7-
import { column_getVisibleLeafColumns } from '../column-visibility/ColumnVisibility.utils'
8+
import { callMemoOrStaticFn } from '../../utils'
9+
import type { ColumnPinningPosition } from '../column-pinning/ColumnPinning.types'
810
import type { CellData, RowData, Updater } from '../../types/type-utils'
911
import type { TableFeatures } from '../../types/TableFeatures'
1012
import type { Table_Internal } from '../../types/Table'
1113
import type { Header } from '../../types/Header'
12-
import type { Column } from '../../types/Column'
13-
import type {
14-
ColumnDef_ColumnSizing,
15-
ColumnSizingState,
16-
} from './ColumnSizing.types'
14+
import type { Column_Internal } from '../../types/Column'
15+
import type { ColumnSizingState } from './ColumnSizing.types'
1716

1817
export function getDefaultColumnSizingState(): ColumnSizingState {
1918
return structuredClone({})
@@ -31,11 +30,7 @@ export function column_getSize<
3130
TFeatures extends TableFeatures,
3231
TData extends RowData,
3332
TValue extends CellData = CellData,
34-
>(
35-
column: Column<TFeatures, TData, TValue> & {
36-
columnDef: ColumnDef_ColumnSizing
37-
},
38-
): number {
33+
>(column: Column_Internal<TFeatures, TData, TValue>): number {
3934
const defaultSizes = getDefaultColumnSizingColumnDef()
4035
const columnSize = column.table.options.state?.columnSizing?.[column.id]
4136

@@ -53,13 +48,18 @@ export function column_getStart<
5348
TData extends RowData,
5449
TValue extends CellData = CellData,
5550
>(
56-
column: Column<TFeatures, TData, TValue> & {
57-
columnDef: ColumnDef_ColumnSizing
58-
},
59-
position?: false | 'left' | 'right' | 'center',
51+
column: Column_Internal<TFeatures, TData, TValue>,
52+
position: ColumnPinningPosition | 'center',
6053
): number {
61-
return column_getVisibleLeafColumns(column.table, position)
62-
.slice(0, column_getIndex(column, position))
54+
const { table } = column
55+
const visibleLeafColumns = callMemoOrStaticFn(
56+
table,
57+
table_getPinnedVisibleLeafColumns,
58+
position,
59+
)
60+
61+
return visibleLeafColumns
62+
.slice(0, callMemoOrStaticFn(column, column_getIndex, position))
6363
.reduce((sum, c) => sum + column_getSize(c), 0)
6464
}
6565

@@ -68,19 +68,26 @@ export function column_getAfter<
6868
TData extends RowData,
6969
TValue extends CellData = CellData,
7070
>(
71-
column: Column<TFeatures, TData, TValue>,
72-
position?: false | 'left' | 'right' | 'center',
71+
column: Column_Internal<TFeatures, TData, TValue>,
72+
position: ColumnPinningPosition | 'center',
7373
): number {
74-
return column_getVisibleLeafColumns(column.table, position)
75-
.slice(column_getIndex(column, position) + 1)
74+
const { table } = column
75+
const visibleLeafColumns = callMemoOrStaticFn(
76+
table,
77+
table_getPinnedVisibleLeafColumns,
78+
position,
79+
)
80+
81+
return visibleLeafColumns
82+
.slice(callMemoOrStaticFn(column, column_getIndex, position) + 1)
7683
.reduce((sum, c) => sum + column_getSize(c), 0)
7784
}
7885

7986
export function column_resetSize<
8087
TFeatures extends TableFeatures,
8188
TData extends RowData,
8289
TValue extends CellData = CellData,
83-
>(column: Column<TFeatures, TData, TValue>) {
90+
>(column: Column_Internal<TFeatures, TData, TValue>) {
8491
table_setColumnSizing(column.table, ({ [column.id]: _, ...rest }) => {
8592
return rest
8693
})

packages/table-core/src/features/column-visibility/ColumnVisibility.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export const ColumnVisibility: TableFeature = {
6868
assignAPIs(column, [
6969
{
7070
fn: () => column_getIsVisible(column),
71+
memoDeps: () => [
72+
column.table.options.columns,
73+
column.table.options.state?.columnVisibility,
74+
column.columns,
75+
],
7176
},
7277
{
7378
fn: () => column_getCanHide(column),

0 commit comments

Comments
 (0)