Skip to content

Commit

Permalink
lazy-init optional instance values
Browse files Browse the repository at this point in the history
  • Loading branch information
mleibman-db committed Feb 21, 2025
1 parent 73a6b1e commit 0cc9f19
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
28 changes: 18 additions & 10 deletions packages/table-core/src/features/ColumnFiltering.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RowModel } from '..'
import { getRowProto, RowModel } from '..'
import { BuiltInFilterFn, filterFns } from '../filterFns'
import {
Column,
Expand Down Expand Up @@ -362,15 +362,6 @@ export const ColumnFiltering: TableFeature = {
}
},

createRow: <TData extends RowData>(
row: Row<TData>,
_table: Table<TData>
): void => {
// TODO: move to a lazy-initialized proto getters
row.columnFilters = {}
row.columnFiltersMeta = {}
},

createTable: <TData extends RowData>(table: Table<TData>): void => {
table.setColumnFilters = (updater: Updater<ColumnFiltersState>) => {
const leafColumns = table.getAllLeafColumns()
Expand Down Expand Up @@ -412,6 +403,23 @@ export const ColumnFiltering: TableFeature = {

return table._getFilteredRowModel()
}

Object.assign(getRowProto(table), {
get columnFilters() {
// Lazy-init the backing cache on the instance so we don't take up memory for rows that don't need it
return ((
this as { _columnFilters?: ColumnFiltersRow<any>['columnFilters'] }
)._columnFilters ??= {})
},
get columnFiltersMeta() {
// Lazy-init the backing cache on the instance so we don't take up memory for rows that don't need it
return ((
this as {
_columnFiltersMeta?: ColumnFiltersRow<any>['columnFiltersMeta']
}
)._columnFiltersMeta ??= {})
},
} as ColumnFiltersRow<any> & Row<any>)
},
}

Expand Down
17 changes: 9 additions & 8 deletions packages/table-core/src/features/ColumnGrouping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ export const ColumnGrouping: TableFeature = {
}

Object.assign(getRowProto(table), {
get _groupingValuesCache() {
// Lazy-init the backing cache on the instance so we don't take up memory for rows that don't need it
return ((
this as {
__groupingValuesCache?: GroupingRow['_groupingValuesCache']
}
).__groupingValuesCache ??= {})
},

getIsGrouped() {
return !!this.groupingColumnId
},
Expand All @@ -378,14 +387,6 @@ export const ColumnGrouping: TableFeature = {
} as GroupingRow & Row<any>)
},

createRow: <TData extends RowData>(
row: Row<TData>,
table: Table<TData>
): void => {
// TODO: move to a lazy-initialized proto getter
row._groupingValuesCache = {}
},

createCell: <TData extends RowData, TValue>(
cell: Cell<TData, TValue>,
column: Column<TData, TValue>,
Expand Down

0 comments on commit 0cc9f19

Please sign in to comment.