Skip to content

Commit 0cc9f19

Browse files
committed
lazy-init optional instance values
1 parent 73a6b1e commit 0cc9f19

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

packages/table-core/src/features/ColumnFiltering.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RowModel } from '..'
1+
import { getRowProto, RowModel } from '..'
22
import { BuiltInFilterFn, filterFns } from '../filterFns'
33
import {
44
Column,
@@ -362,15 +362,6 @@ export const ColumnFiltering: TableFeature = {
362362
}
363363
},
364364

365-
createRow: <TData extends RowData>(
366-
row: Row<TData>,
367-
_table: Table<TData>
368-
): void => {
369-
// TODO: move to a lazy-initialized proto getters
370-
row.columnFilters = {}
371-
row.columnFiltersMeta = {}
372-
},
373-
374365
createTable: <TData extends RowData>(table: Table<TData>): void => {
375366
table.setColumnFilters = (updater: Updater<ColumnFiltersState>) => {
376367
const leafColumns = table.getAllLeafColumns()
@@ -412,6 +403,23 @@ export const ColumnFiltering: TableFeature = {
412403

413404
return table._getFilteredRowModel()
414405
}
406+
407+
Object.assign(getRowProto(table), {
408+
get columnFilters() {
409+
// Lazy-init the backing cache on the instance so we don't take up memory for rows that don't need it
410+
return ((
411+
this as { _columnFilters?: ColumnFiltersRow<any>['columnFilters'] }
412+
)._columnFilters ??= {})
413+
},
414+
get columnFiltersMeta() {
415+
// Lazy-init the backing cache on the instance so we don't take up memory for rows that don't need it
416+
return ((
417+
this as {
418+
_columnFiltersMeta?: ColumnFiltersRow<any>['columnFiltersMeta']
419+
}
420+
)._columnFiltersMeta ??= {})
421+
},
422+
} as ColumnFiltersRow<any> & Row<any>)
415423
},
416424
}
417425

packages/table-core/src/features/ColumnGrouping.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ export const ColumnGrouping: TableFeature = {
355355
}
356356

357357
Object.assign(getRowProto(table), {
358+
get _groupingValuesCache() {
359+
// Lazy-init the backing cache on the instance so we don't take up memory for rows that don't need it
360+
return ((
361+
this as {
362+
__groupingValuesCache?: GroupingRow['_groupingValuesCache']
363+
}
364+
).__groupingValuesCache ??= {})
365+
},
366+
358367
getIsGrouped() {
359368
return !!this.groupingColumnId
360369
},
@@ -378,14 +387,6 @@ export const ColumnGrouping: TableFeature = {
378387
} as GroupingRow & Row<any>)
379388
},
380389

381-
createRow: <TData extends RowData>(
382-
row: Row<TData>,
383-
table: Table<TData>
384-
): void => {
385-
// TODO: move to a lazy-initialized proto getter
386-
row._groupingValuesCache = {}
387-
},
388-
389390
createCell: <TData extends RowData, TValue>(
390391
cell: Cell<TData, TValue>,
391392
column: Column<TData, TValue>,

0 commit comments

Comments
 (0)