Skip to content

Commit 89f551b

Browse files
committed
use callMemoOrStaticFn for memoized feature apis
1 parent f46960d commit 89f551b

File tree

11 files changed

+156
-99
lines changed

11 files changed

+156
-99
lines changed

packages/table-core/src/core/headers/coreHeadersFeature.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assignAPIs } from '../../utils'
1+
import { assignAPIs, callMemoOrStaticFn } from '../../utils'
22
import {
33
table_getCenterHeaderGroups,
44
table_getLeftHeaderGroups,
@@ -56,9 +56,9 @@ export const coreHeadersFeature: TableFeature<{
5656
{
5757
fn: () => table_getLeafHeaders(table),
5858
memoDeps: () => [
59-
table_getLeftHeaderGroups(table),
60-
table_getCenterHeaderGroups(table),
61-
table_getRightHeaderGroups(table),
59+
callMemoOrStaticFn(table, table_getLeftHeaderGroups),
60+
callMemoOrStaticFn(table, table_getCenterHeaderGroups),
61+
callMemoOrStaticFn(table, table_getRightHeaderGroups),
6262
],
6363
},
6464
])

packages/table-core/src/core/headers/coreHeadersFeature.utils.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import {
55
table_getRightHeaderGroups,
66
} from '../../features/column-pinning/columnPinningFeature.utils'
77
import { table_getVisibleLeafColumns } from '../../features/column-visibility/columnVisibilityFeature.utils'
8+
import { callMemoOrStaticFn } from '../../utils'
89
import { buildHeaderGroups } from './buildHeaderGroups'
910
import type { Table_Internal } from '../../types/Table'
1011
import type { Header } from '../../types/Header'
1112
import type { RowData } from '../../types/type-utils'
1213
import type { TableFeatures } from '../../types/TableFeatures'
1314
import type { Header_Header } from './coreHeadersFeature.types'
15+
import type { Column } from '../../types/Column'
1416

1517
export function header_getLeafHeaders<
1618
TFeatures extends TableFeatures,
@@ -50,7 +52,10 @@ export function table_getHeaderGroups<
5052
const { left, right } =
5153
table.options.state?.columnPinning ?? getDefaultColumnPinningState()
5254
const allColumns = table.getAllColumns()
53-
const leafColumns = table_getVisibleLeafColumns(table)
55+
const leafColumns = callMemoOrStaticFn(
56+
table,
57+
table_getVisibleLeafColumns,
58+
) as unknown as Array<Column<TFeatures, TData, unknown>>
5459

5560
const leftColumns = left
5661
.map((columnId) => leafColumns.find((d) => d.id === columnId)!)
@@ -97,9 +102,9 @@ export function table_getLeafHeaders<
97102
TFeatures extends TableFeatures,
98103
TData extends RowData,
99104
>(table: Table_Internal<TFeatures, TData>) {
100-
const left = table_getLeftHeaderGroups(table)
101-
const center = table_getCenterHeaderGroups(table)
102-
const right = table_getRightHeaderGroups(table)
105+
const left = callMemoOrStaticFn(table, table_getLeftHeaderGroups)
106+
const center = callMemoOrStaticFn(table, table_getCenterHeaderGroups)
107+
const right = callMemoOrStaticFn(table, table_getRightHeaderGroups)
103108

104109
return [
105110
...(left[0]?.headers ?? []),

packages/table-core/src/features/column-pinning/columnPinningFeature.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assignAPIs, makeStateUpdater } from '../../utils'
1+
import { assignAPIs, callMemoOrStaticFn, makeStateUpdater } from '../../utils'
22
import { table_getVisibleLeafColumns } from '../column-visibility/columnVisibilityFeature.utils'
33
import {
44
column_getCanPin,
@@ -132,64 +132,70 @@ export const columnPinningFeature: TableFeature<{
132132
fn: () => table_getLeftHeaderGroups(table),
133133
memoDeps: () => [
134134
table.getAllColumns(),
135-
table_getVisibleLeafColumns(table),
135+
callMemoOrStaticFn(table, table_getVisibleLeafColumns),
136136
table.options.state?.columnPinning?.left,
137137
],
138138
},
139139
{
140140
fn: () => table_getCenterHeaderGroups(table),
141141
memoDeps: () => [
142142
table.getAllColumns(),
143-
table_getVisibleLeafColumns(table),
143+
callMemoOrStaticFn(table, table_getVisibleLeafColumns),
144144
table.options.state?.columnPinning,
145145
],
146146
},
147147
{
148148
fn: () => table_getRightHeaderGroups(table),
149149
memoDeps: () => [
150150
table.getAllColumns(),
151-
table_getVisibleLeafColumns(table),
151+
callMemoOrStaticFn(table, table_getVisibleLeafColumns),
152152
table.options.state?.columnPinning?.right,
153153
],
154154
},
155155
// footer groups
156156
{
157157
fn: () => table_getLeftFooterGroups(table),
158-
memoDeps: () => [table_getLeftHeaderGroups(table)],
158+
memoDeps: () => [callMemoOrStaticFn(table, table_getLeftHeaderGroups)],
159159
},
160160
{
161161
fn: () => table_getCenterFooterGroups(table),
162-
memoDeps: () => [table_getCenterHeaderGroups(table)],
162+
memoDeps: () => [
163+
callMemoOrStaticFn(table, table_getCenterHeaderGroups),
164+
],
163165
},
164166
{
165167
fn: () => table_getRightFooterGroups(table),
166-
memoDeps: () => [table_getRightHeaderGroups(table)],
168+
memoDeps: () => [callMemoOrStaticFn(table, table_getRightHeaderGroups)],
167169
},
168170
// flat headers
169171
{
170172
fn: () => table_getLeftFlatHeaders(table),
171-
memoDeps: () => [table_getLeftHeaderGroups(table)],
173+
memoDeps: () => [callMemoOrStaticFn(table, table_getLeftHeaderGroups)],
172174
},
173175
{
174176
fn: () => table_getRightFlatHeaders(table),
175-
memoDeps: () => [table_getRightHeaderGroups(table)],
177+
memoDeps: () => [callMemoOrStaticFn(table, table_getRightHeaderGroups)],
176178
},
177179
{
178180
fn: () => table_getCenterFlatHeaders(table),
179-
memoDeps: () => [table_getCenterHeaderGroups(table)],
181+
memoDeps: () => [
182+
callMemoOrStaticFn(table, table_getCenterHeaderGroups),
183+
],
180184
},
181185
// leaf headers
182186
{
183187
fn: () => table_getLeftLeafHeaders(table),
184-
memoDeps: () => [table_getLeftHeaderGroups(table)],
188+
memoDeps: () => [callMemoOrStaticFn(table, table_getLeftHeaderGroups)],
185189
},
186190
{
187191
fn: () => table_getRightLeafHeaders(table),
188-
memoDeps: () => [table_getRightHeaderGroups(table)],
192+
memoDeps: () => [callMemoOrStaticFn(table, table_getRightHeaderGroups)],
189193
},
190194
{
191195
fn: () => table_getCenterLeafHeaders(table),
192-
memoDeps: () => [table_getCenterHeaderGroups(table)],
196+
memoDeps: () => [
197+
callMemoOrStaticFn(table, table_getCenterHeaderGroups),
198+
],
193199
},
194200
// leaf columns
195201
{

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

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '../column-visibility/columnVisibilityFeature.utils'
66
import { buildHeaderGroups } from '../../core/headers/buildHeaderGroups'
77
import { callMemoOrStaticFn } from '../../utils'
8+
import type { HeaderGroup } from '../../types/HeaderGroup'
89
import type { Cell } from '../../types/Cell'
910
import type { Row } from '../../types/Row'
1011
import type { CellData, RowData, Updater } from '../../types/type-utils'
@@ -121,7 +122,7 @@ export function row_getCenterVisibleCells<
121122
TFeatures extends TableFeatures,
122123
TData extends RowData,
123124
>(row: Row<TFeatures, TData>) {
124-
const allCells = row_getAllVisibleCells(row)
125+
const allCells = callMemoOrStaticFn(row, row_getAllVisibleCells)
125126
const { left, right } =
126127
row.table.options.state?.columnPinning ?? getDefaultColumnPinningState()
127128
const leftAndRight: Array<string> = [...left, ...right]
@@ -131,31 +132,29 @@ export function row_getCenterVisibleCells<
131132
export function row_getLeftVisibleCells<
132133
TFeatures extends TableFeatures,
133134
TData extends RowData,
134-
>(row: Row<TFeatures, TData>) {
135-
const allCells = row_getAllVisibleCells(row)
135+
>(row: Row<TFeatures, TData>): Array<Cell<TFeatures, TData, unknown>> {
136+
const allCells = callMemoOrStaticFn(row, row_getAllVisibleCells)
136137
const { left } =
137138
row.table.options.state?.columnPinning ?? getDefaultColumnPinningState()
138139
const cells = left
139140
.map((columnId) => allCells.find((cell) => cell.column.id === columnId)!)
140141
.filter(Boolean)
141142
.map((d) => ({ ...d, position: 'left' }))
142-
143-
return cells as Array<Cell<TFeatures, TData>>
143+
return cells as any
144144
}
145145

146146
export function row_getRightVisibleCells<
147147
TFeatures extends TableFeatures,
148148
TData extends RowData,
149149
>(row: Row<TFeatures, TData>) {
150-
const allCells = row_getAllVisibleCells(row)
150+
const allCells = callMemoOrStaticFn(row, row_getAllVisibleCells)
151151
const { right } =
152152
row.table.options.state?.columnPinning ?? getDefaultColumnPinningState()
153153
const cells = right
154154
.map((columnId) => allCells.find((cell) => cell.column.id === columnId)!)
155155
.filter(Boolean)
156156
.map((d) => ({ ...d, position: 'right' }))
157-
158-
return cells as Array<Cell<TFeatures, TData>>
157+
return cells as any
159158
}
160159

161160
// Table APIs
@@ -201,7 +200,10 @@ export function table_getLeftHeaderGroups<
201200
TData extends RowData,
202201
>(table: Table_Internal<TFeatures, TData>) {
203202
const allColumns = table.getAllColumns()
204-
const leafColumns = table_getVisibleLeafColumns(table)
203+
const leafColumns = callMemoOrStaticFn(
204+
table,
205+
table_getVisibleLeafColumns,
206+
) as unknown as Array<Column<TFeatures, TData, unknown>>
205207
const { left } =
206208
table.options.state?.columnPinning ?? getDefaultColumnPinningState()
207209

@@ -217,7 +219,10 @@ export function table_getRightHeaderGroups<
217219
TData extends RowData,
218220
>(table: Table_Internal<TFeatures, TData>) {
219221
const allColumns = table.getAllColumns()
220-
const leafColumns = table_getVisibleLeafColumns(table)
222+
const leafColumns = callMemoOrStaticFn(
223+
table,
224+
table_getVisibleLeafColumns,
225+
) as unknown as Array<Column<TFeatures, TData, unknown>>
221226
const { right } =
222227
table.options.state?.columnPinning ?? getDefaultColumnPinningState()
223228

@@ -231,9 +236,14 @@ export function table_getRightHeaderGroups<
231236
export function table_getCenterHeaderGroups<
232237
TFeatures extends TableFeatures,
233238
TData extends RowData,
234-
>(table: Table_Internal<TFeatures, TData>) {
239+
>(
240+
table: Table_Internal<TFeatures, TData>,
241+
): Array<HeaderGroup<TFeatures, TData>> {
235242
const allColumns = table.getAllColumns()
236-
let leafColumns = table_getVisibleLeafColumns(table)
243+
let leafColumns = callMemoOrStaticFn(
244+
table,
245+
table_getVisibleLeafColumns,
246+
) as unknown as Array<Column<TFeatures, TData, unknown>>
237247
const { left, right } =
238248
table.options.state?.columnPinning ?? getDefaultColumnPinningState()
239249
const leftAndRight: Array<string> = [...left, ...right]
@@ -250,23 +260,23 @@ export function table_getLeftFooterGroups<
250260
TFeatures extends TableFeatures,
251261
TData extends RowData,
252262
>(table: Table_Internal<TFeatures, TData>) {
253-
const headerGroups = table_getLeftHeaderGroups(table)
263+
const headerGroups = callMemoOrStaticFn(table, table_getLeftHeaderGroups)
254264
return [...headerGroups].reverse()
255265
}
256266

257267
export function table_getRightFooterGroups<
258268
TFeatures extends TableFeatures,
259269
TData extends RowData,
260270
>(table: Table_Internal<TFeatures, TData>) {
261-
const headerGroups = table_getRightHeaderGroups(table)
271+
const headerGroups = callMemoOrStaticFn(table, table_getRightHeaderGroups)
262272
return [...headerGroups].reverse()
263273
}
264274

265275
export function table_getCenterFooterGroups<
266276
TFeatures extends TableFeatures,
267277
TData extends RowData,
268278
>(table: Table_Internal<TFeatures, TData>) {
269-
const headerGroups = table_getCenterHeaderGroups(table)
279+
const headerGroups = callMemoOrStaticFn(table, table_getCenterHeaderGroups)
270280
return [...headerGroups].reverse()
271281
}
272282

@@ -276,7 +286,7 @@ export function table_getLeftFlatHeaders<
276286
TFeatures extends TableFeatures,
277287
TData extends RowData,
278288
>(table: Table_Internal<TFeatures, TData>) {
279-
const leftHeaderGroups = table_getLeftHeaderGroups(table)
289+
const leftHeaderGroups = callMemoOrStaticFn(table, table_getLeftHeaderGroups)
280290
return leftHeaderGroups
281291
.map((headerGroup) => {
282292
return headerGroup.headers
@@ -288,7 +298,10 @@ export function table_getRightFlatHeaders<
288298
TFeatures extends TableFeatures,
289299
TData extends RowData,
290300
>(table: Table_Internal<TFeatures, TData>) {
291-
const rightHeaderGroups = table_getRightHeaderGroups(table)
301+
const rightHeaderGroups = callMemoOrStaticFn(
302+
table,
303+
table_getRightHeaderGroups,
304+
)
292305
return rightHeaderGroups
293306
.map((headerGroup) => {
294307
return headerGroup.headers
@@ -300,7 +313,10 @@ export function table_getCenterFlatHeaders<
300313
TFeatures extends TableFeatures,
301314
TData extends RowData,
302315
>(table: Table_Internal<TFeatures, TData>) {
303-
const centerHeaderGroups = table_getCenterHeaderGroups(table)
316+
const centerHeaderGroups = callMemoOrStaticFn(
317+
table,
318+
table_getCenterHeaderGroups,
319+
)
304320
return centerHeaderGroups
305321
.map((headerGroup) => {
306322
return headerGroup.headers
@@ -314,7 +330,7 @@ export function table_getLeftLeafHeaders<
314330
TFeatures extends TableFeatures,
315331
TData extends RowData,
316332
>(table: Table_Internal<TFeatures, TData>) {
317-
return table_getLeftFlatHeaders(table).filter(
333+
return callMemoOrStaticFn(table, table_getLeftFlatHeaders).filter(
318334
(header) => !header.subHeaders.length,
319335
)
320336
}
@@ -323,7 +339,7 @@ export function table_getRightLeafHeaders<
323339
TFeatures extends TableFeatures,
324340
TData extends RowData,
325341
>(table: Table_Internal<TFeatures, TData>) {
326-
return table_getRightFlatHeaders(table).filter(
342+
return callMemoOrStaticFn(table, table_getRightFlatHeaders).filter(
327343
(header) => !header.subHeaders.length,
328344
)
329345
}
@@ -332,7 +348,7 @@ export function table_getCenterLeafHeaders<
332348
TFeatures extends TableFeatures,
333349
TData extends RowData,
334350
>(table: Table_Internal<TFeatures, TData>) {
335-
return table_getCenterFlatHeaders(table).filter(
351+
return callMemoOrStaticFn(table, table_getCenterFlatHeaders).filter(
336352
(header) => !header.subHeaders.length,
337353
)
338354
}
@@ -399,26 +415,26 @@ export function table_getLeftVisibleLeafColumns<
399415
TFeatures extends TableFeatures,
400416
TData extends RowData,
401417
>(table: Table_Internal<TFeatures, TData>) {
402-
return table_getLeftLeafColumns(table).filter((column) =>
403-
column_getIsVisible(column),
418+
return callMemoOrStaticFn(table, table_getLeftLeafColumns).filter((column) =>
419+
callMemoOrStaticFn(column, column_getIsVisible),
404420
)
405421
}
406422

407423
export function table_getRightVisibleLeafColumns<
408424
TFeatures extends TableFeatures,
409425
TData extends RowData,
410426
>(table: Table_Internal<TFeatures, TData>) {
411-
return table_getRightLeafColumns(table).filter((column) =>
412-
column_getIsVisible(column),
427+
return callMemoOrStaticFn(table, table_getRightLeafColumns).filter((column) =>
428+
callMemoOrStaticFn(column, column_getIsVisible),
413429
)
414430
}
415431

416432
export function table_getCenterVisibleLeafColumns<
417433
TFeatures extends TableFeatures,
418434
TData extends RowData,
419435
>(table: Table_Internal<TFeatures, TData>) {
420-
return table_getCenterLeafColumns(table).filter((column) =>
421-
column_getIsVisible(column),
436+
return callMemoOrStaticFn(table, table_getCenterLeafColumns).filter(
437+
(column) => callMemoOrStaticFn(column, column_getIsVisible),
422438
)
423439
}
424440

0 commit comments

Comments
 (0)