Skip to content

fix(cdk/table): memory leak when no rows are sticky #30461

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

Merged
merged 1 commit into from
Feb 10, 2025
Merged
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
33 changes: 15 additions & 18 deletions src/cdk/table/sticky-styler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class StickyStyler {
? new globalThis.ResizeObserver(entries => this._updateCachedSizes(entries))
: null;
private _updatedStickyColumnsParamsToReplay: UpdateStickyColumnsParams[] = [];
private _stickyColumnsReplayTimeout: number | null = null;
private _stickyColumnsReplayTimeout: ReturnType<typeof setTimeout> | null = null;
private _cachedCellWidths: number[] = [];
private readonly _borderCellCss: Readonly<{[d in StickyDirection]: string}>;
private _destroyed = false;
Expand Down Expand Up @@ -128,24 +128,14 @@ export class StickyStyler {
recalculateCellWidths = true,
replay = true,
) {
if (replay) {
this._updateStickyColumnReplayQueue({
rows: [...rows],
stickyStartStates: [...stickyStartStates],
stickyEndStates: [...stickyEndStates],
});
}

// Don't cache any state if none of the columns are sticky.
if (
!rows.length ||
!this._isBrowser ||
!(stickyStartStates.some(state => state) || stickyEndStates.some(state => state))
) {
if (this._positionListener) {
this._positionListener.stickyColumnsUpdated({sizes: []});
this._positionListener.stickyEndColumnsUpdated({sizes: []});
}

this._positionListener?.stickyColumnsUpdated({sizes: []});
this._positionListener?.stickyEndColumnsUpdated({sizes: []});
return;
}

Expand All @@ -164,6 +154,14 @@ export class StickyStyler {
let startPositions: number[];
let endPositions: number[];

if (replay) {
this._updateStickyColumnReplayQueue({
rows: [...rows],
stickyStartStates: [...stickyStartStates],
stickyEndStates: [...stickyEndStates],
});
}

this._afterNextRender({
earlyRead: () => {
cellWidths = this._getCellWidths(firstRow, recalculateCellWidths);
Expand Down Expand Up @@ -321,6 +319,7 @@ export class StickyStyler {
clearTimeout(this._stickyColumnsReplayTimeout);
}

this._resizeObserver?.disconnect();
this._destroyed = true;
}

Expand Down Expand Up @@ -493,11 +492,9 @@ export class StickyStyler {
this._removeFromStickyColumnReplayQueue(params.rows);

// No need to replay if a flush is pending.
if (this._stickyColumnsReplayTimeout) {
return;
if (!this._stickyColumnsReplayTimeout) {
this._updatedStickyColumnsParamsToReplay.push(params);
}

this._updatedStickyColumnsParamsToReplay.push(params);
}

/** Remove updates for the specified rows from the queue. */
Expand Down
Loading