Skip to content

Commit

Permalink
use callMemoOrStaticFn for memoized feature apis
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Dec 26, 2024
1 parent f46960d commit 89f551b
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 99 deletions.
8 changes: 4 additions & 4 deletions packages/table-core/src/core/headers/coreHeadersFeature.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assignAPIs } from '../../utils'
import { assignAPIs, callMemoOrStaticFn } from '../../utils'
import {
table_getCenterHeaderGroups,
table_getLeftHeaderGroups,
Expand Down Expand Up @@ -56,9 +56,9 @@ export const coreHeadersFeature: TableFeature<{
{
fn: () => table_getLeafHeaders(table),
memoDeps: () => [
table_getLeftHeaderGroups(table),
table_getCenterHeaderGroups(table),
table_getRightHeaderGroups(table),
callMemoOrStaticFn(table, table_getLeftHeaderGroups),
callMemoOrStaticFn(table, table_getCenterHeaderGroups),
callMemoOrStaticFn(table, table_getRightHeaderGroups),
],
},
])
Expand Down
13 changes: 9 additions & 4 deletions packages/table-core/src/core/headers/coreHeadersFeature.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {
table_getRightHeaderGroups,
} from '../../features/column-pinning/columnPinningFeature.utils'
import { table_getVisibleLeafColumns } from '../../features/column-visibility/columnVisibilityFeature.utils'
import { callMemoOrStaticFn } from '../../utils'
import { buildHeaderGroups } from './buildHeaderGroups'
import type { Table_Internal } from '../../types/Table'
import type { Header } from '../../types/Header'
import type { RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
import type { Header_Header } from './coreHeadersFeature.types'
import type { Column } from '../../types/Column'

export function header_getLeafHeaders<
TFeatures extends TableFeatures,
Expand Down Expand Up @@ -50,7 +52,10 @@ export function table_getHeaderGroups<
const { left, right } =
table.options.state?.columnPinning ?? getDefaultColumnPinningState()
const allColumns = table.getAllColumns()
const leafColumns = table_getVisibleLeafColumns(table)
const leafColumns = callMemoOrStaticFn(
table,
table_getVisibleLeafColumns,
) as unknown as Array<Column<TFeatures, TData, unknown>>

const leftColumns = left
.map((columnId) => leafColumns.find((d) => d.id === columnId)!)
Expand Down Expand Up @@ -97,9 +102,9 @@ export function table_getLeafHeaders<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
const left = table_getLeftHeaderGroups(table)
const center = table_getCenterHeaderGroups(table)
const right = table_getRightHeaderGroups(table)
const left = callMemoOrStaticFn(table, table_getLeftHeaderGroups)
const center = callMemoOrStaticFn(table, table_getCenterHeaderGroups)
const right = callMemoOrStaticFn(table, table_getRightHeaderGroups)

return [
...(left[0]?.headers ?? []),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assignAPIs, makeStateUpdater } from '../../utils'
import { assignAPIs, callMemoOrStaticFn, makeStateUpdater } from '../../utils'
import { table_getVisibleLeafColumns } from '../column-visibility/columnVisibilityFeature.utils'
import {
column_getCanPin,
Expand Down Expand Up @@ -132,64 +132,70 @@ export const columnPinningFeature: TableFeature<{
fn: () => table_getLeftHeaderGroups(table),
memoDeps: () => [
table.getAllColumns(),
table_getVisibleLeafColumns(table),
callMemoOrStaticFn(table, table_getVisibleLeafColumns),
table.options.state?.columnPinning?.left,
],
},
{
fn: () => table_getCenterHeaderGroups(table),
memoDeps: () => [
table.getAllColumns(),
table_getVisibleLeafColumns(table),
callMemoOrStaticFn(table, table_getVisibleLeafColumns),
table.options.state?.columnPinning,
],
},
{
fn: () => table_getRightHeaderGroups(table),
memoDeps: () => [
table.getAllColumns(),
table_getVisibleLeafColumns(table),
callMemoOrStaticFn(table, table_getVisibleLeafColumns),
table.options.state?.columnPinning?.right,
],
},
// footer groups
{
fn: () => table_getLeftFooterGroups(table),
memoDeps: () => [table_getLeftHeaderGroups(table)],
memoDeps: () => [callMemoOrStaticFn(table, table_getLeftHeaderGroups)],
},
{
fn: () => table_getCenterFooterGroups(table),
memoDeps: () => [table_getCenterHeaderGroups(table)],
memoDeps: () => [
callMemoOrStaticFn(table, table_getCenterHeaderGroups),
],
},
{
fn: () => table_getRightFooterGroups(table),
memoDeps: () => [table_getRightHeaderGroups(table)],
memoDeps: () => [callMemoOrStaticFn(table, table_getRightHeaderGroups)],
},
// flat headers
{
fn: () => table_getLeftFlatHeaders(table),
memoDeps: () => [table_getLeftHeaderGroups(table)],
memoDeps: () => [callMemoOrStaticFn(table, table_getLeftHeaderGroups)],
},
{
fn: () => table_getRightFlatHeaders(table),
memoDeps: () => [table_getRightHeaderGroups(table)],
memoDeps: () => [callMemoOrStaticFn(table, table_getRightHeaderGroups)],
},
{
fn: () => table_getCenterFlatHeaders(table),
memoDeps: () => [table_getCenterHeaderGroups(table)],
memoDeps: () => [
callMemoOrStaticFn(table, table_getCenterHeaderGroups),
],
},
// leaf headers
{
fn: () => table_getLeftLeafHeaders(table),
memoDeps: () => [table_getLeftHeaderGroups(table)],
memoDeps: () => [callMemoOrStaticFn(table, table_getLeftHeaderGroups)],
},
{
fn: () => table_getRightLeafHeaders(table),
memoDeps: () => [table_getRightHeaderGroups(table)],
memoDeps: () => [callMemoOrStaticFn(table, table_getRightHeaderGroups)],
},
{
fn: () => table_getCenterLeafHeaders(table),
memoDeps: () => [table_getCenterHeaderGroups(table)],
memoDeps: () => [
callMemoOrStaticFn(table, table_getCenterHeaderGroups),
],
},
// leaf columns
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '../column-visibility/columnVisibilityFeature.utils'
import { buildHeaderGroups } from '../../core/headers/buildHeaderGroups'
import { callMemoOrStaticFn } from '../../utils'
import type { HeaderGroup } from '../../types/HeaderGroup'
import type { Cell } from '../../types/Cell'
import type { Row } from '../../types/Row'
import type { CellData, RowData, Updater } from '../../types/type-utils'
Expand Down Expand Up @@ -121,7 +122,7 @@ export function row_getCenterVisibleCells<
TFeatures extends TableFeatures,
TData extends RowData,
>(row: Row<TFeatures, TData>) {
const allCells = row_getAllVisibleCells(row)
const allCells = callMemoOrStaticFn(row, row_getAllVisibleCells)
const { left, right } =
row.table.options.state?.columnPinning ?? getDefaultColumnPinningState()
const leftAndRight: Array<string> = [...left, ...right]
Expand All @@ -131,31 +132,29 @@ export function row_getCenterVisibleCells<
export function row_getLeftVisibleCells<
TFeatures extends TableFeatures,
TData extends RowData,
>(row: Row<TFeatures, TData>) {
const allCells = row_getAllVisibleCells(row)
>(row: Row<TFeatures, TData>): Array<Cell<TFeatures, TData, unknown>> {
const allCells = callMemoOrStaticFn(row, row_getAllVisibleCells)
const { left } =
row.table.options.state?.columnPinning ?? getDefaultColumnPinningState()
const cells = left
.map((columnId) => allCells.find((cell) => cell.column.id === columnId)!)
.filter(Boolean)
.map((d) => ({ ...d, position: 'left' }))

return cells as Array<Cell<TFeatures, TData>>
return cells as any
}

export function row_getRightVisibleCells<
TFeatures extends TableFeatures,
TData extends RowData,
>(row: Row<TFeatures, TData>) {
const allCells = row_getAllVisibleCells(row)
const allCells = callMemoOrStaticFn(row, row_getAllVisibleCells)
const { right } =
row.table.options.state?.columnPinning ?? getDefaultColumnPinningState()
const cells = right
.map((columnId) => allCells.find((cell) => cell.column.id === columnId)!)
.filter(Boolean)
.map((d) => ({ ...d, position: 'right' }))

return cells as Array<Cell<TFeatures, TData>>
return cells as any
}

// Table APIs
Expand Down Expand Up @@ -201,7 +200,10 @@ export function table_getLeftHeaderGroups<
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
const allColumns = table.getAllColumns()
const leafColumns = table_getVisibleLeafColumns(table)
const leafColumns = callMemoOrStaticFn(
table,
table_getVisibleLeafColumns,
) as unknown as Array<Column<TFeatures, TData, unknown>>
const { left } =
table.options.state?.columnPinning ?? getDefaultColumnPinningState()

Expand All @@ -217,7 +219,10 @@ export function table_getRightHeaderGroups<
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
const allColumns = table.getAllColumns()
const leafColumns = table_getVisibleLeafColumns(table)
const leafColumns = callMemoOrStaticFn(
table,
table_getVisibleLeafColumns,
) as unknown as Array<Column<TFeatures, TData, unknown>>
const { right } =
table.options.state?.columnPinning ?? getDefaultColumnPinningState()

Expand All @@ -231,9 +236,14 @@ export function table_getRightHeaderGroups<
export function table_getCenterHeaderGroups<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
>(
table: Table_Internal<TFeatures, TData>,
): Array<HeaderGroup<TFeatures, TData>> {
const allColumns = table.getAllColumns()
let leafColumns = table_getVisibleLeafColumns(table)
let leafColumns = callMemoOrStaticFn(
table,
table_getVisibleLeafColumns,
) as unknown as Array<Column<TFeatures, TData, unknown>>
const { left, right } =
table.options.state?.columnPinning ?? getDefaultColumnPinningState()
const leftAndRight: Array<string> = [...left, ...right]
Expand All @@ -250,23 +260,23 @@ export function table_getLeftFooterGroups<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
const headerGroups = table_getLeftHeaderGroups(table)
const headerGroups = callMemoOrStaticFn(table, table_getLeftHeaderGroups)
return [...headerGroups].reverse()
}

export function table_getRightFooterGroups<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
const headerGroups = table_getRightHeaderGroups(table)
const headerGroups = callMemoOrStaticFn(table, table_getRightHeaderGroups)
return [...headerGroups].reverse()
}

export function table_getCenterFooterGroups<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
const headerGroups = table_getCenterHeaderGroups(table)
const headerGroups = callMemoOrStaticFn(table, table_getCenterHeaderGroups)
return [...headerGroups].reverse()
}

Expand All @@ -276,7 +286,7 @@ export function table_getLeftFlatHeaders<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
const leftHeaderGroups = table_getLeftHeaderGroups(table)
const leftHeaderGroups = callMemoOrStaticFn(table, table_getLeftHeaderGroups)
return leftHeaderGroups
.map((headerGroup) => {
return headerGroup.headers
Expand All @@ -288,7 +298,10 @@ export function table_getRightFlatHeaders<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
const rightHeaderGroups = table_getRightHeaderGroups(table)
const rightHeaderGroups = callMemoOrStaticFn(
table,
table_getRightHeaderGroups,
)
return rightHeaderGroups
.map((headerGroup) => {
return headerGroup.headers
Expand All @@ -300,7 +313,10 @@ export function table_getCenterFlatHeaders<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
const centerHeaderGroups = table_getCenterHeaderGroups(table)
const centerHeaderGroups = callMemoOrStaticFn(
table,
table_getCenterHeaderGroups,
)
return centerHeaderGroups
.map((headerGroup) => {
return headerGroup.headers
Expand All @@ -314,7 +330,7 @@ export function table_getLeftLeafHeaders<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
return table_getLeftFlatHeaders(table).filter(
return callMemoOrStaticFn(table, table_getLeftFlatHeaders).filter(
(header) => !header.subHeaders.length,
)
}
Expand All @@ -323,7 +339,7 @@ export function table_getRightLeafHeaders<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
return table_getRightFlatHeaders(table).filter(
return callMemoOrStaticFn(table, table_getRightFlatHeaders).filter(
(header) => !header.subHeaders.length,
)
}
Expand All @@ -332,7 +348,7 @@ export function table_getCenterLeafHeaders<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
return table_getCenterFlatHeaders(table).filter(
return callMemoOrStaticFn(table, table_getCenterFlatHeaders).filter(
(header) => !header.subHeaders.length,
)
}
Expand Down Expand Up @@ -399,26 +415,26 @@ export function table_getLeftVisibleLeafColumns<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
return table_getLeftLeafColumns(table).filter((column) =>
column_getIsVisible(column),
return callMemoOrStaticFn(table, table_getLeftLeafColumns).filter((column) =>
callMemoOrStaticFn(column, column_getIsVisible),
)
}

export function table_getRightVisibleLeafColumns<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
return table_getRightLeafColumns(table).filter((column) =>
column_getIsVisible(column),
return callMemoOrStaticFn(table, table_getRightLeafColumns).filter((column) =>
callMemoOrStaticFn(column, column_getIsVisible),
)
}

export function table_getCenterVisibleLeafColumns<
TFeatures extends TableFeatures,
TData extends RowData,
>(table: Table_Internal<TFeatures, TData>) {
return table_getCenterLeafColumns(table).filter((column) =>
column_getIsVisible(column),
return callMemoOrStaticFn(table, table_getCenterLeafColumns).filter(
(column) => callMemoOrStaticFn(column, column_getIsVisible),
)
}

Expand Down
Loading

0 comments on commit 89f551b

Please sign in to comment.