Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reduce memory leak by remove memo function #5514

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions packages/table-core/src/core/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,12 @@ export const createRow = <TData extends RowData>(
}
return parentRows.reverse()
},
getAllCells: memo(
() => [table.getAllLeafColumns()],
leafColumns => {
return leafColumns.map(column => {
return createCell(table, row as Row<TData>, column, column.id)
})
},
getMemoOptions(table.options, 'debugRows', 'getAllCells')
),
getAllCells: () => {
const leafColumns = table.getAllLeafColumns()
return leafColumns.map(column => {
return createCell(table, row as Row<TData>, column, column.id)
})
},

_getAllCellsByColumnId: memo(
() => [row.getAllCells()],
Expand Down
59 changes: 28 additions & 31 deletions packages/table-core/src/features/ColumnPinning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,43 +240,40 @@ export const ColumnPinning: TableFeature = {
row: Row<TData>,
table: Table<TData>
): void => {
row.getCenterVisibleCells = memo(
() => [
row.getCenterVisibleCells = () => {
const [allCells, left, right] = [
row._getAllVisibleCells(),
table.getState().columnPinning.left,
table.getState().columnPinning.right,
],
(allCells, left, right) => {
const leftAndRight: string[] = [...(left ?? []), ...(right ?? [])]
]
const leftAndRight: string[] = [...(left ?? []), ...(right ?? [])]

return allCells.filter(d => !leftAndRight.includes(d.column.id))
},
getMemoOptions(table.options, 'debugRows', 'getCenterVisibleCells')
)
row.getLeftVisibleCells = memo(
() => [row._getAllVisibleCells(), table.getState().columnPinning.left],
(allCells, left) => {
const cells = (left ?? [])
.map(columnId => allCells.find(cell => cell.column.id === columnId)!)
.filter(Boolean)
.map(d => ({ ...d, position: 'left' }) as Cell<TData, unknown>)
return allCells.filter(d => !leftAndRight.includes(d.column.id))
}
row.getLeftVisibleCells = () => {
const [allCells, left] = [
row._getAllVisibleCells(),
table.getState().columnPinning.left,
]
const cells = (left ?? [])
.map(columnId => allCells.find(cell => cell.column.id === columnId)!)
.filter(Boolean)
.map(d => ({ ...d, position: 'left' }) as Cell<TData, unknown>)

return cells
},
getMemoOptions(table.options, 'debugRows', 'getLeftVisibleCells')
)
row.getRightVisibleCells = memo(
() => [row._getAllVisibleCells(), table.getState().columnPinning.right],
(allCells, right) => {
const cells = (right ?? [])
.map(columnId => allCells.find(cell => cell.column.id === columnId)!)
.filter(Boolean)
.map(d => ({ ...d, position: 'right' }) as Cell<TData, unknown>)
return cells
}
row.getRightVisibleCells = () => {
const [allCells, right] = [
row._getAllVisibleCells(),
table.getState().columnPinning.right,
]
const cells = (right ?? [])
.map(columnId => allCells.find(cell => cell.column.id === columnId)!)
.filter(Boolean)
.map(d => ({ ...d, position: 'right' }) as Cell<TData, unknown>)

return cells
},
getMemoOptions(table.options, 'debugRows', 'getRightVisibleCells')
)
return cells
}
},

createTable: <TData extends RowData>(table: Table<TData>): void => {
Expand Down
27 changes: 11 additions & 16 deletions packages/table-core/src/features/ColumnVisibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,17 @@ export const ColumnVisibility: TableFeature = {
row: Row<TData>,
table: Table<TData>
): void => {
row._getAllVisibleCells = memo(
() => [row.getAllCells(), table.getState().columnVisibility],
cells => {
return cells.filter(cell => cell.column.getIsVisible())
},
getMemoOptions(table.options, 'debugRows', '_getAllVisibleCells')
)
row.getVisibleCells = memo(
() => [
row.getLeftVisibleCells(),
row.getCenterVisibleCells(),
row.getRightVisibleCells(),
],
(left, center, right) => [...left, ...center, ...right],
getMemoOptions(table.options, 'debugRows', 'getVisibleCells')
)
row._getAllVisibleCells = () => {
const cells = row.getAllCells()
return cells.filter(cell => cell.column.getIsVisible())
}
row.getVisibleCells = () => {
return [
...row.getLeftVisibleCells(),
...row.getCenterVisibleCells(),
...row.getRightVisibleCells(),
]
}
},

createTable: <TData extends RowData>(table: Table<TData>): void => {
Expand Down