From 89f551b1c80ed71dd27ccad35dc77c1ffc3d89ee Mon Sep 17 00:00:00 2001 From: Kevin Van Cott Date: Thu, 26 Dec 2024 11:38:35 -0600 Subject: [PATCH] use callMemoOrStaticFn for memoized feature apis --- .../src/core/headers/coreHeadersFeature.ts | 8 +-- .../core/headers/coreHeadersFeature.utils.ts | 13 ++-- .../column-pinning/columnPinningFeature.ts | 32 +++++---- .../columnPinningFeature.utils.ts | 70 ++++++++++++------- .../columnSizingFeature.utils.ts | 27 ++++--- .../columnVisibilityFeature.ts | 8 +-- .../columnVisibilityFeature.utils.ts | 21 ++++-- .../row-pagination/rowPaginationFeature.ts | 1 - .../row-pinning/rowPinningFeature.utils.ts | 5 +- packages/table-core/src/utils.ts | 20 ++++-- packages/table-core/tests/unit/utils.test.ts | 50 ++++++------- 11 files changed, 156 insertions(+), 99 deletions(-) diff --git a/packages/table-core/src/core/headers/coreHeadersFeature.ts b/packages/table-core/src/core/headers/coreHeadersFeature.ts index 339ecba4a8..6a256d08ac 100644 --- a/packages/table-core/src/core/headers/coreHeadersFeature.ts +++ b/packages/table-core/src/core/headers/coreHeadersFeature.ts @@ -1,4 +1,4 @@ -import { assignAPIs } from '../../utils' +import { assignAPIs, callMemoOrStaticFn } from '../../utils' import { table_getCenterHeaderGroups, table_getLeftHeaderGroups, @@ -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), ], }, ]) diff --git a/packages/table-core/src/core/headers/coreHeadersFeature.utils.ts b/packages/table-core/src/core/headers/coreHeadersFeature.utils.ts index 8069992337..212deebab4 100644 --- a/packages/table-core/src/core/headers/coreHeadersFeature.utils.ts +++ b/packages/table-core/src/core/headers/coreHeadersFeature.utils.ts @@ -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, @@ -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> const leftColumns = left .map((columnId) => leafColumns.find((d) => d.id === columnId)!) @@ -97,9 +102,9 @@ export function table_getLeafHeaders< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - 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 ?? []), diff --git a/packages/table-core/src/features/column-pinning/columnPinningFeature.ts b/packages/table-core/src/features/column-pinning/columnPinningFeature.ts index dec50f8061..448b217ee9 100644 --- a/packages/table-core/src/features/column-pinning/columnPinningFeature.ts +++ b/packages/table-core/src/features/column-pinning/columnPinningFeature.ts @@ -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, @@ -132,7 +132,7 @@ export const columnPinningFeature: TableFeature<{ fn: () => table_getLeftHeaderGroups(table), memoDeps: () => [ table.getAllColumns(), - table_getVisibleLeafColumns(table), + callMemoOrStaticFn(table, table_getVisibleLeafColumns), table.options.state?.columnPinning?.left, ], }, @@ -140,7 +140,7 @@ export const columnPinningFeature: TableFeature<{ fn: () => table_getCenterHeaderGroups(table), memoDeps: () => [ table.getAllColumns(), - table_getVisibleLeafColumns(table), + callMemoOrStaticFn(table, table_getVisibleLeafColumns), table.options.state?.columnPinning, ], }, @@ -148,48 +148,54 @@ export const columnPinningFeature: TableFeature<{ 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 { diff --git a/packages/table-core/src/features/column-pinning/columnPinningFeature.utils.ts b/packages/table-core/src/features/column-pinning/columnPinningFeature.utils.ts index bb11eedd3b..6d2acb80fc 100644 --- a/packages/table-core/src/features/column-pinning/columnPinningFeature.utils.ts +++ b/packages/table-core/src/features/column-pinning/columnPinningFeature.utils.ts @@ -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' @@ -121,7 +122,7 @@ export function row_getCenterVisibleCells< TFeatures extends TableFeatures, TData extends RowData, >(row: Row) { - const allCells = row_getAllVisibleCells(row) + const allCells = callMemoOrStaticFn(row, row_getAllVisibleCells) const { left, right } = row.table.options.state?.columnPinning ?? getDefaultColumnPinningState() const leftAndRight: Array = [...left, ...right] @@ -131,31 +132,29 @@ export function row_getCenterVisibleCells< export function row_getLeftVisibleCells< TFeatures extends TableFeatures, TData extends RowData, ->(row: Row) { - const allCells = row_getAllVisibleCells(row) +>(row: Row): Array> { + 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> + return cells as any } export function row_getRightVisibleCells< TFeatures extends TableFeatures, TData extends RowData, >(row: Row) { - 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> + return cells as any } // Table APIs @@ -201,7 +200,10 @@ export function table_getLeftHeaderGroups< TData extends RowData, >(table: Table_Internal) { const allColumns = table.getAllColumns() - const leafColumns = table_getVisibleLeafColumns(table) + const leafColumns = callMemoOrStaticFn( + table, + table_getVisibleLeafColumns, + ) as unknown as Array> const { left } = table.options.state?.columnPinning ?? getDefaultColumnPinningState() @@ -217,7 +219,10 @@ export function table_getRightHeaderGroups< TData extends RowData, >(table: Table_Internal) { const allColumns = table.getAllColumns() - const leafColumns = table_getVisibleLeafColumns(table) + const leafColumns = callMemoOrStaticFn( + table, + table_getVisibleLeafColumns, + ) as unknown as Array> const { right } = table.options.state?.columnPinning ?? getDefaultColumnPinningState() @@ -231,9 +236,14 @@ export function table_getRightHeaderGroups< export function table_getCenterHeaderGroups< TFeatures extends TableFeatures, TData extends RowData, ->(table: Table_Internal) { +>( + table: Table_Internal, +): Array> { const allColumns = table.getAllColumns() - let leafColumns = table_getVisibleLeafColumns(table) + let leafColumns = callMemoOrStaticFn( + table, + table_getVisibleLeafColumns, + ) as unknown as Array> const { left, right } = table.options.state?.columnPinning ?? getDefaultColumnPinningState() const leftAndRight: Array = [...left, ...right] @@ -250,7 +260,7 @@ export function table_getLeftFooterGroups< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - const headerGroups = table_getLeftHeaderGroups(table) + const headerGroups = callMemoOrStaticFn(table, table_getLeftHeaderGroups) return [...headerGroups].reverse() } @@ -258,7 +268,7 @@ export function table_getRightFooterGroups< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - const headerGroups = table_getRightHeaderGroups(table) + const headerGroups = callMemoOrStaticFn(table, table_getRightHeaderGroups) return [...headerGroups].reverse() } @@ -266,7 +276,7 @@ export function table_getCenterFooterGroups< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - const headerGroups = table_getCenterHeaderGroups(table) + const headerGroups = callMemoOrStaticFn(table, table_getCenterHeaderGroups) return [...headerGroups].reverse() } @@ -276,7 +286,7 @@ export function table_getLeftFlatHeaders< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - const leftHeaderGroups = table_getLeftHeaderGroups(table) + const leftHeaderGroups = callMemoOrStaticFn(table, table_getLeftHeaderGroups) return leftHeaderGroups .map((headerGroup) => { return headerGroup.headers @@ -288,7 +298,10 @@ export function table_getRightFlatHeaders< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - const rightHeaderGroups = table_getRightHeaderGroups(table) + const rightHeaderGroups = callMemoOrStaticFn( + table, + table_getRightHeaderGroups, + ) return rightHeaderGroups .map((headerGroup) => { return headerGroup.headers @@ -300,7 +313,10 @@ export function table_getCenterFlatHeaders< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - const centerHeaderGroups = table_getCenterHeaderGroups(table) + const centerHeaderGroups = callMemoOrStaticFn( + table, + table_getCenterHeaderGroups, + ) return centerHeaderGroups .map((headerGroup) => { return headerGroup.headers @@ -314,7 +330,7 @@ export function table_getLeftLeafHeaders< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - return table_getLeftFlatHeaders(table).filter( + return callMemoOrStaticFn(table, table_getLeftFlatHeaders).filter( (header) => !header.subHeaders.length, ) } @@ -323,7 +339,7 @@ export function table_getRightLeafHeaders< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - return table_getRightFlatHeaders(table).filter( + return callMemoOrStaticFn(table, table_getRightFlatHeaders).filter( (header) => !header.subHeaders.length, ) } @@ -332,7 +348,7 @@ export function table_getCenterLeafHeaders< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - return table_getCenterFlatHeaders(table).filter( + return callMemoOrStaticFn(table, table_getCenterFlatHeaders).filter( (header) => !header.subHeaders.length, ) } @@ -399,8 +415,8 @@ export function table_getLeftVisibleLeafColumns< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - return table_getLeftLeafColumns(table).filter((column) => - column_getIsVisible(column), + return callMemoOrStaticFn(table, table_getLeftLeafColumns).filter((column) => + callMemoOrStaticFn(column, column_getIsVisible), ) } @@ -408,8 +424,8 @@ export function table_getRightVisibleLeafColumns< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - return table_getRightLeafColumns(table).filter((column) => - column_getIsVisible(column), + return callMemoOrStaticFn(table, table_getRightLeafColumns).filter((column) => + callMemoOrStaticFn(column, column_getIsVisible), ) } @@ -417,8 +433,8 @@ export function table_getCenterVisibleLeafColumns< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - return table_getCenterLeafColumns(table).filter((column) => - column_getIsVisible(column), + return callMemoOrStaticFn(table, table_getCenterLeafColumns).filter( + (column) => callMemoOrStaticFn(column, column_getIsVisible), ) } diff --git a/packages/table-core/src/features/column-sizing/columnSizingFeature.utils.ts b/packages/table-core/src/features/column-sizing/columnSizingFeature.utils.ts index 1804d9dd99..d090031b79 100644 --- a/packages/table-core/src/features/column-sizing/columnSizingFeature.utils.ts +++ b/packages/table-core/src/features/column-sizing/columnSizingFeature.utils.ts @@ -168,9 +168,12 @@ export function table_getLeftTotalSize< TData extends RowData, >(table: Table_Internal) { return ( - table_getLeftHeaderGroups(table)[0]?.headers.reduce((sum, header) => { - return sum + header_getSize(header) - }, 0) ?? 0 + callMemoOrStaticFn(table, table_getLeftHeaderGroups)[0]?.headers.reduce( + (sum, header) => { + return sum + header_getSize(header) + }, + 0, + ) ?? 0 ) } @@ -179,9 +182,12 @@ export function table_getCenterTotalSize< TData extends RowData, >(table: Table_Internal) { return ( - table_getCenterHeaderGroups(table)[0]?.headers.reduce((sum, header) => { - return sum + header_getSize(header) - }, 0) ?? 0 + callMemoOrStaticFn(table, table_getCenterHeaderGroups)[0]?.headers.reduce( + (sum, header) => { + return sum + header_getSize(header) + }, + 0, + ) ?? 0 ) } @@ -190,8 +196,11 @@ export function table_getRightTotalSize< TData extends RowData, >(table: Table_Internal) { return ( - table_getRightHeaderGroups(table)[0]?.headers.reduce((sum, header) => { - return sum + header_getSize(header) - }, 0) ?? 0 + callMemoOrStaticFn(table, table_getRightHeaderGroups)[0]?.headers.reduce( + (sum, header) => { + return sum + header_getSize(header) + }, + 0, + ) ?? 0 ) } diff --git a/packages/table-core/src/features/column-visibility/columnVisibilityFeature.ts b/packages/table-core/src/features/column-visibility/columnVisibilityFeature.ts index a060cb053a..f4a2f4e3f1 100644 --- a/packages/table-core/src/features/column-visibility/columnVisibilityFeature.ts +++ b/packages/table-core/src/features/column-visibility/columnVisibilityFeature.ts @@ -1,4 +1,4 @@ -import { assignAPIs, makeStateUpdater } from '../../utils' +import { assignAPIs, callMemoOrStaticFn, makeStateUpdater } from '../../utils' import { row_getCenterVisibleCells, row_getLeftVisibleCells, @@ -90,9 +90,9 @@ export const columnVisibilityFeature: TableFeature<{ { fn: (left, center, right) => row_getVisibleCells(left, center, right), memoDeps: () => [ - row_getLeftVisibleCells(row), - row_getCenterVisibleCells(row), - row_getRightVisibleCells(row), + callMemoOrStaticFn(row, row_getLeftVisibleCells), + callMemoOrStaticFn(row, row_getCenterVisibleCells), + callMemoOrStaticFn(row, row_getRightVisibleCells), ], }, ]) diff --git a/packages/table-core/src/features/column-visibility/columnVisibilityFeature.utils.ts b/packages/table-core/src/features/column-visibility/columnVisibilityFeature.utils.ts index 36dfc0366c..01cbfe5c9c 100644 --- a/packages/table-core/src/features/column-visibility/columnVisibilityFeature.utils.ts +++ b/packages/table-core/src/features/column-visibility/columnVisibilityFeature.utils.ts @@ -1,3 +1,4 @@ +import { callMemoOrStaticFn } from '../../utils' import type { CellData, RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Table_Internal } from '../../types/Table' @@ -18,7 +19,7 @@ export function column_toggleVisibility< if (column_getCanHide(column)) { table_setColumnVisibility(column.table, (old) => ({ ...old, - [column.id]: visible ?? !column_getIsVisible(column), + [column.id]: visible ?? !callMemoOrStaticFn(column, column_getIsVisible), })) } } @@ -31,7 +32,9 @@ export function column_getIsVisible< const childColumns = column.columns return ( (childColumns.length - ? childColumns.some((c) => column_getIsVisible(c)) + ? childColumns.some((childColumn) => + callMemoOrStaticFn(childColumn, column_getIsVisible), + ) : column.table.options.state?.columnVisibility?.[column.id]) ?? true ) } @@ -64,7 +67,9 @@ export function row_getAllVisibleCells< TFeatures extends TableFeatures, TData extends RowData, >(row: Row) { - return row.getAllCells().filter((cell) => column_getIsVisible(cell.column)) + return row + .getAllCells() + .filter((cell) => callMemoOrStaticFn(cell.column, column_getIsVisible)) } export function row_getVisibleCells< @@ -84,7 +89,7 @@ export function table_getVisibleFlatColumns< >(table: Table_Internal) { return table .getAllFlatColumns() - .filter((column) => column_getIsVisible(column)) + .filter((column) => callMemoOrStaticFn(column, column_getIsVisible)) } export function table_getVisibleLeafColumns< @@ -93,7 +98,7 @@ export function table_getVisibleLeafColumns< >(table: Table_Internal) { return table .getAllLeafColumns() - .filter((column) => column_getIsVisible(column)) + .filter((column) => callMemoOrStaticFn(column, column_getIsVisible)) } export function table_setColumnVisibility< @@ -140,14 +145,16 @@ export function table_getIsAllColumnsVisible< >(table: Table_Internal) { return !table .getAllLeafColumns() - .some((column) => !column_getIsVisible(column)) + .some((column) => !callMemoOrStaticFn(column, column_getIsVisible)) } export function table_getIsSomeColumnsVisible< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { - return table.getAllLeafColumns().some((column) => column_getIsVisible(column)) + return table + .getAllLeafColumns() + .some((column) => callMemoOrStaticFn(column, column_getIsVisible)) } export function table_getToggleAllColumnsVisibilityHandler< diff --git a/packages/table-core/src/features/row-pagination/rowPaginationFeature.ts b/packages/table-core/src/features/row-pagination/rowPaginationFeature.ts index 5499003b57..a7944d6c2a 100644 --- a/packages/table-core/src/features/row-pagination/rowPaginationFeature.ts +++ b/packages/table-core/src/features/row-pagination/rowPaginationFeature.ts @@ -77,7 +77,6 @@ export const rowPaginationFeature: TableFeature<{ }, { fn: () => table_getPageCount(table), - memoDeps: () => [table_getPageCount(table)], }, { fn: (defaultState) => table_resetPageSize(table, defaultState), diff --git a/packages/table-core/src/features/row-pinning/rowPinningFeature.utils.ts b/packages/table-core/src/features/row-pinning/rowPinningFeature.utils.ts index a9c73b4c9b..d21491c194 100644 --- a/packages/table-core/src/features/row-pinning/rowPinningFeature.utils.ts +++ b/packages/table-core/src/features/row-pinning/rowPinningFeature.utils.ts @@ -1,4 +1,5 @@ import { row_getIsAllParentsExpanded } from '../row-expanding/rowExpandingFeature.utils' +import { callMemoOrStaticFn } from '../../utils' import type { RowData, Updater } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Table_Internal } from '../../types/Table' @@ -144,8 +145,8 @@ export function row_getPinnedIndex< const visiblePinnedRowIds = ( position === 'top' - ? table_getTopRows(row.table) - : table_getBottomRows(row.table) + ? callMemoOrStaticFn(row.table, table_getTopRows) + : callMemoOrStaticFn(row.table, table_getBottomRows) ).map(({ id }) => id) return visiblePinnedRowIds.indexOf(row.id) diff --git a/packages/table-core/src/utils.ts b/packages/table-core/src/utils.ts index 1471e76b7c..05e17f56c2 100755 --- a/packages/table-core/src/utils.ts +++ b/packages/table-core/src/utils.ts @@ -108,6 +108,7 @@ interface TableMemoOptions, TDepArgs, TResult> debug?: boolean debugCache?: boolean fnName: string + objectId?: string onAfterUpdate?: () => void } @@ -123,6 +124,7 @@ export function tableMemo, TDepArgs, TResult>({ debug, debugCache, fnName, + objectId, onAfterUpdate, ...memoOptions }: TableMemoOptions) { @@ -130,18 +132,28 @@ export function tableMemo, TDepArgs, TResult>({ let afterCompareTime: number let startCalcTime: number let endCalcTime: number + let isFirstRun = true function logTime(time: number, depsChanged: boolean) { if (isDev) { + const runType = isFirstRun + ? '(1st run)' + : depsChanged + ? '(rerun)' + : '(cache)' + isFirstRun = false + console.info( - `%c⏱ ${pad(`${time.toFixed(time < 1 ? 2 : 1)} ms`, 12)} ${depsChanged ? '(rerun)' : '(cache)'}`, + `%c⏱ ${pad(`${time.toFixed(time < 1 ? 2 : 1)} ms`, 12)} %c${runType}%c ${fnName}%c ${objectId ?? ''}`, `font-size: .6rem; font-weight: bold; ${ depsChanged ? `color: hsl( ${Math.max(0, Math.min(120 - Math.log10(time) * 60, 120))}deg 100% 31%);` : '' } `, - fnName, + 'color: #FF69B4', + 'color: gray', + 'color: #87CEEB', ) } } @@ -242,6 +254,7 @@ export function assignAPIs< memoDeps, fn, fnName, + objectId: obj.id, debug: isDev ? (table.options.debugAll ?? table.options[`debug${debugLevel}`]) : false, @@ -264,7 +277,6 @@ export function callMemoOrStaticFn< ): ReturnType { const { fnKey } = getFunctionNameInfo(staticFn) return ( - ((obj as any)[fnKey] as Function | undefined)?.(...args) ?? - staticFn(obj, ...args) + (obj[fnKey] as Function | undefined)?.(...args) ?? staticFn(obj, ...args) ) } diff --git a/packages/table-core/tests/unit/utils.test.ts b/packages/table-core/tests/unit/utils.test.ts index c599e0c39e..f5b8d08188 100644 --- a/packages/table-core/tests/unit/utils.test.ts +++ b/packages/table-core/tests/unit/utils.test.ts @@ -3,34 +3,36 @@ import { getFunctionNameInfo } from '../../src/utils' // TODO: add unit tests for rest of utils -describe('getFunctionNameInfo', () => { - it('should correctly parse a function with a standard name', () => { - function table_getRowModel() {} - const result = getFunctionNameInfo(table_getRowModel) - expect(result).toEqual({ - fnKey: 'getRowModel', - fnName: 'table.getRowModel', - parentName: 'table', +describe('utils', () => { + describe('getFunctionNameInfo', () => { + it('should correctly parse a function with a standard name', () => { + function table_getRowModel() {} + const result = getFunctionNameInfo(table_getRowModel) + expect(result).toEqual({ + fnKey: 'getRowModel', + fnName: 'table.getRowModel', + parentName: 'table', + }) }) - }) - it('should handle anonymous functions with a name assigned', () => { - const row_getCells = function () {} - const result = getFunctionNameInfo(row_getCells) - expect(result).toEqual({ - fnKey: 'getCells', - fnName: 'row.getCells', - parentName: 'row', + it('should handle anonymous functions with a name assigned', () => { + const row_getCells = function () {} + const result = getFunctionNameInfo(row_getCells) + expect(result).toEqual({ + fnKey: 'getCells', + fnName: 'row.getCells', + parentName: 'row', + }) }) - }) - it('should parse arrow functions with a name', () => { - const column_getIsVisible = () => {} - const result = getFunctionNameInfo(column_getIsVisible) - expect(result).toEqual({ - fnKey: 'getIsVisible', - fnName: 'column.getIsVisible', - parentName: 'column', + it('should parse arrow functions with a name', () => { + const column_getIsVisible = () => {} + const result = getFunctionNameInfo(column_getIsVisible) + expect(result).toEqual({ + fnKey: 'getIsVisible', + fnName: 'column.getIsVisible', + parentName: 'column', + }) }) }) })