Skip to content

Commit

Permalink
feat: add a "disableValueCache" option
Browse files Browse the repository at this point in the history
When using tanstack table with proxy objects as row data, copying cell
values to the cache is unwanted behavior as they are no longer connected
to the row proxy object. In these scenarios i'd like to turn off the value cache.

ref TanStack#4876
  • Loading branch information
jonatan-zint-tfs committed Oct 23, 2023
1 parent a1e9732 commit d5818e5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
12 changes: 11 additions & 1 deletion docs/api/core/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,17 @@ Set this option to true to output column debugging information to the console.
debugRows?: boolean
```

Set this option to true to output row debugging information to the console.
### `disableValueCache`

```tsx
disableValueCache?: boolean
```

Set this option to true to disable caching of cell values. This is especially useful if you are using
proxy objects (like vue does) in your data. That way you can have data updates without the necessity of replacing
the whole data object to indicate updates to your data.

> 鈿狅笍 Disabling your value cache has a performance penalty if your accessor functions are complex.
### `render`

Expand Down
26 changes: 13 additions & 13 deletions packages/table-core/src/core/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const createRow = <TData extends RowData>(
_valuesCache: {},
_uniqueValuesCache: {},
getValue: columnId => {
if (row._valuesCache.hasOwnProperty(columnId)) {
if (!table.options.disableValueCache && row._valuesCache.hasOwnProperty(columnId)) {
return row._valuesCache[columnId]
}

Expand All @@ -119,16 +119,18 @@ export const createRow = <TData extends RowData>(
if (!column?.accessorFn) {
return undefined
}

row._valuesCache[columnId] = column.accessorFn(
const value = column.accessorFn(
row.original as TData,
rowIndex
)
if (!table.options.disableValueCache) {
row._valuesCache[columnId] = value
}

return row._valuesCache[columnId] as any
return value as any
},
getUniqueValues: columnId => {
if (row._uniqueValuesCache.hasOwnProperty(columnId)) {
if (!table.options.disableValueCache && row._uniqueValuesCache.hasOwnProperty(columnId)) {
return row._uniqueValuesCache[columnId]
}

Expand All @@ -138,17 +140,15 @@ export const createRow = <TData extends RowData>(
return undefined
}

if (!column.columnDef.getUniqueValues) {
row._uniqueValuesCache[columnId] = [row.getValue(columnId)]
return row._uniqueValuesCache[columnId]
}

row._uniqueValuesCache[columnId] = column.columnDef.getUniqueValues(
const value = column.columnDef.getUniqueValues ? column.columnDef.getUniqueValues(
row.original as TData,
rowIndex
)
) : [row.getValue(columnId)]

return row._uniqueValuesCache[columnId] as any
if (!table.options.disableValueCache) {
row._uniqueValuesCache[columnId] = value
}
return value as any
},
renderValue: columnId =>
row.getValue(columnId) ?? table.options.renderFallbackValue,
Expand Down
11 changes: 11 additions & 0 deletions packages/table-core/src/core/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ export interface CoreOptions<TData extends RowData> {
* @link [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
defaultColumn?: Partial<ColumnDef<TData, unknown>>

/**
* This option will prevent the cell values to be cached after the accessor function is executed
* once. This is especially useful if you are using a framework like vue where the cell values
* are proxy objects and are updated when the underlying data changes.
* However the performance penalty might be significant if your accessor functions are complex.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/table#disablevaluecache)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/tables)
*/
disableValueCache?: boolean

/**
* This required option is a factory for a function that computes and returns the core row model for the table.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/table#getcorerowmodel)
Expand Down

0 comments on commit d5818e5

Please sign in to comment.