Skip to content

Commit

Permalink
fix a bug in lazy-init
Browse files Browse the repository at this point in the history
The way I was setting up lazy-initialized props wasn't working since Object.assign() was invoking the prop getter, bypassing the lazy initialization.
  • Loading branch information
mleibman-db committed Feb 26, 2025
1 parent 3620a00 commit aa518e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
33 changes: 19 additions & 14 deletions packages/table-core/src/features/ColumnFiltering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,22 +404,27 @@ 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 ??= {})
// Lazy-init the backing caches on the instance so we don't take up memory for rows that don't need it
Object.defineProperties(getRowProto(table), {
columnFilters: {
get() {
return (this._columnFilters ??= {})
},
set(value) {
this._columnFilters = value
},
enumerable: true,
},
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 ??= {})
columnFiltersMeta: {
get() {
return (this._columnFiltersMeta ??= {})
},
set(value) {
this._columnFiltersMeta = value
},
enumerable: true,
},
} as ColumnFiltersRow<any> & Row<any>)
})
},
}

Expand Down
13 changes: 6 additions & 7 deletions packages/table-core/src/features/ColumnGrouping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,15 @@ export const ColumnGrouping: TableFeature = {
return table._getGroupedRowModel()
}

Object.assign(getRowProto(table), {
get _groupingValuesCache() {
Object.defineProperty(getRowProto(table), '_groupingValuesCache', {
get() {
// 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 ??= {})
return (this.__groupingValuesCache ??= {})
},
enumerable: true,
})

Object.assign(getRowProto(table), {
getIsGrouped() {
return !!this.groupingColumnId
},
Expand Down

0 comments on commit aa518e9

Please sign in to comment.