Skip to content

Commit 782cc76

Browse files
andrewseguincrisbeto
authored andcommitted
Revert "perf(cdk/table): Use afterRender hooks (#28354)" (#28461)
This reverts commit 81cb5ac. (cherry picked from commit 14aeb00)
1 parent 7a7cd11 commit 782cc76

File tree

6 files changed

+77
-172
lines changed

6 files changed

+77
-172
lines changed

src/cdk-experimental/column-resize/resize-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ export abstract class ResizeStrategy {
5555

5656
this.styleScheduler.schedule(() => {
5757
tableElement.style.width = coerceCssPixelValue(tableWidth + this._pendingResizeDelta!);
58+
5859
this._pendingResizeDelta = null;
5960
});
6061

6162
this.styleScheduler.scheduleEnd(() => {
6263
this.table.updateStickyColumnStyles();
63-
this.styleScheduler.flushAfterRender();
6464
});
6565
}
6666

src/cdk-experimental/table-scroll-container/table-scroll-container.ts

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,21 @@ export class CdkTableScrollContainer implements StickyPositioningListener, OnDes
7272
}
7373

7474
stickyColumnsUpdated({sizes}: StickyUpdate): void {
75-
if (arrayEquals(this._startSizes, sizes)) {
76-
return;
77-
}
7875
this._startSizes = sizes;
7976
this._updateScrollbar();
8077
}
8178

8279
stickyEndColumnsUpdated({sizes}: StickyUpdate): void {
83-
if (arrayEquals(this._endSizes, sizes)) {
84-
return;
85-
}
8680
this._endSizes = sizes;
8781
this._updateScrollbar();
8882
}
8983

9084
stickyHeaderRowsUpdated({sizes}: StickyUpdate): void {
91-
if (arrayEquals(this._headerSizes, sizes)) {
92-
return;
93-
}
9485
this._headerSizes = sizes;
9586
this._updateScrollbar();
9687
}
9788

9889
stickyFooterRowsUpdated({sizes}: StickyUpdate): void {
99-
console.log('sizes', this._footerSizes, sizes, arrayEquals(this._footerSizes, sizes));
100-
if (arrayEquals(this._footerSizes, sizes)) {
101-
return;
102-
}
10390
this._footerSizes = sizes;
10491
this._updateScrollbar();
10592
}
@@ -143,13 +130,9 @@ export class CdkTableScrollContainer implements StickyPositioningListener, OnDes
143130
/** Updates the stylesheet with the specified scrollbar style. */
144131
private _applyCss(value: string) {
145132
this._clearCss();
133+
146134
const selector = `.${this._uniqueClassName}::-webkit-scrollbar-track`;
147135
this._getStyleSheet().insertRule(`${selector} {margin: ${value}}`, 0);
148-
149-
// Force the scrollbar to paint.
150-
const display = this._elementRef.nativeElement.style.display;
151-
this._elementRef.nativeElement.style.display = 'none';
152-
this._elementRef.nativeElement.style.display = display;
153136
}
154137

155138
private _clearCss() {
@@ -170,20 +153,3 @@ function computeMargin(sizes: (number | null | undefined)[]): number {
170153
}
171154
return margin;
172155
}
173-
174-
function arrayEquals(a1: unknown[], a2: unknown[]) {
175-
if (a1 === a2) {
176-
return true;
177-
}
178-
if (a1.length !== a2.length) {
179-
return false;
180-
}
181-
182-
for (let index = 0; index < a1.length; index++) {
183-
if (a1[index] !== a2[index]) {
184-
return false;
185-
}
186-
}
187-
188-
return true;
189-
}

src/cdk/table/coalesced-style-scheduler.ts

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {
10-
Injectable,
11-
NgZone,
12-
OnDestroy,
13-
InjectionToken,
14-
afterRender,
15-
AfterRenderPhase,
16-
} from '@angular/core';
9+
import {Injectable, NgZone, OnDestroy, InjectionToken} from '@angular/core';
1710
import {from, Subject} from 'rxjs';
1811
import {take, takeUntil} from 'rxjs/operators';
1912

@@ -42,46 +35,7 @@ export class _CoalescedStyleScheduler implements OnDestroy {
4235
private _currentSchedule: _Schedule | null = null;
4336
private readonly _destroyed = new Subject<void>();
4437

45-
private readonly _earlyReadTasks: (() => unknown)[] = [];
46-
private readonly _writeTasks: (() => unknown)[] = [];
47-
private readonly _readTasks: (() => unknown)[] = [];
48-
49-
constructor(private readonly _ngZone: NgZone) {
50-
afterRender(() => flushTasks(this._earlyReadTasks), {phase: AfterRenderPhase.EarlyRead});
51-
afterRender(() => flushTasks(this._writeTasks), {phase: AfterRenderPhase.Write});
52-
afterRender(() => flushTasks(this._readTasks), {phase: AfterRenderPhase.Read});
53-
}
54-
55-
/**
56-
* Like afterNextRender(fn, AfterRenderPhase.EarlyRead), but can be called
57-
* outside of injection context. Runs after current/next CD.
58-
*/
59-
scheduleEarlyRead(task: () => unknown): void {
60-
this._earlyReadTasks.push(task);
61-
}
62-
63-
/**
64-
* Like afterNextRender(fn, AfterRenderPhase.Write), but can be called
65-
* outside of injection context. Runs after current/next CD.
66-
*/
67-
scheduleWrite(task: () => unknown): void {
68-
this._writeTasks.push(task);
69-
}
70-
71-
/**
72-
* Like afterNextRender(fn, AfterRenderPhase.Read), but can be called
73-
* outside of injection context. Runs after current/next CD.
74-
*/
75-
scheduleRead(task: () => unknown): void {
76-
this._readTasks.push(task);
77-
}
78-
79-
/** Greedily triggers pending EarlyRead, Write, and Read tasks, in that order. */
80-
flushAfterRender() {
81-
flushTasks(this._earlyReadTasks);
82-
flushTasks(this._writeTasks);
83-
flushTasks(this._readTasks);
84-
}
38+
constructor(private readonly _ngZone: NgZone) {}
8539

8640
/**
8741
* Schedules the specified task to run at the end of the current VM turn.
@@ -145,14 +99,3 @@ export class _CoalescedStyleScheduler implements OnDestroy {
14599
: this._ngZone.onStable.pipe(take(1));
146100
}
147101
}
148-
149-
/**
150-
* Runs and removes tasks from the passed array in order.
151-
* Tasks appended mid-flight will also be flushed.
152-
*/
153-
function flushTasks(tasks: (() => unknown)[]) {
154-
let task: (() => unknown) | undefined;
155-
while ((task = tasks.shift())) {
156-
task();
157-
}
158-
}

src/cdk/table/sticky-styler.ts

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ export class StickyStyler {
8282
}
8383
}
8484

85-
for (const element of elementsToClear) {
86-
this._removeStickyStyle(element, stickyDirections);
87-
}
85+
// Coalesce with sticky row/column updates (and potentially other changes like column resize).
86+
this._coalescedStyleScheduler.schedule(() => {
87+
for (const element of elementsToClear) {
88+
this._removeStickyStyle(element, stickyDirections);
89+
}
90+
});
8891
}
8992

9093
/**
@@ -110,63 +113,61 @@ export class StickyStyler {
110113
!(stickyStartStates.some(state => state) || stickyEndStates.some(state => state))
111114
) {
112115
if (this._positionListener) {
113-
this._coalescedStyleScheduler.scheduleWrite(() => {
114-
this._positionListener!.stickyColumnsUpdated({sizes: []});
115-
this._positionListener!.stickyEndColumnsUpdated({sizes: []});
116-
});
116+
this._positionListener.stickyColumnsUpdated({sizes: []});
117+
this._positionListener.stickyEndColumnsUpdated({sizes: []});
117118
}
118119

119120
return;
120121
}
121122

122-
this._coalescedStyleScheduler.scheduleEarlyRead(() => {
123+
// Coalesce with sticky row updates (and potentially other changes like column resize).
124+
this._coalescedStyleScheduler.schedule(() => {
123125
const firstRow = rows[0];
124126
const numCells = firstRow.children.length;
125-
const lastStickyStart = stickyStartStates.lastIndexOf(true);
126-
const firstStickyEnd = stickyEndStates.indexOf(true);
127+
const cellWidths: number[] = this._getCellWidths(firstRow, recalculateCellWidths);
127128

128-
const cellWidths = this._getCellWidths(firstRow, recalculateCellWidths);
129129
const startPositions = this._getStickyStartColumnPositions(cellWidths, stickyStartStates);
130130
const endPositions = this._getStickyEndColumnPositions(cellWidths, stickyEndStates);
131131

132-
this._coalescedStyleScheduler.scheduleWrite(() => {
133-
const isRtl = this.direction === 'rtl';
134-
const start = isRtl ? 'right' : 'left';
135-
const end = isRtl ? 'left' : 'right';
136-
137-
for (const row of rows) {
138-
for (let i = 0; i < numCells; i++) {
139-
const cell = row.children[i] as HTMLElement;
140-
if (stickyStartStates[i]) {
141-
this._addStickyStyle(cell, start, startPositions![i], i === lastStickyStart);
142-
}
143-
144-
if (stickyEndStates[i]) {
145-
this._addStickyStyle(cell, end, endPositions![i], i === firstStickyEnd);
146-
}
132+
const lastStickyStart = stickyStartStates.lastIndexOf(true);
133+
const firstStickyEnd = stickyEndStates.indexOf(true);
134+
135+
const isRtl = this.direction === 'rtl';
136+
const start = isRtl ? 'right' : 'left';
137+
const end = isRtl ? 'left' : 'right';
138+
139+
for (const row of rows) {
140+
for (let i = 0; i < numCells; i++) {
141+
const cell = row.children[i] as HTMLElement;
142+
if (stickyStartStates[i]) {
143+
this._addStickyStyle(cell, start, startPositions[i], i === lastStickyStart);
147144
}
148-
}
149145

150-
if (this._positionListener) {
151-
this._positionListener.stickyColumnsUpdated({
152-
sizes:
153-
lastStickyStart === -1
154-
? []
155-
: cellWidths!
156-
.slice(0, lastStickyStart + 1)
157-
.map((width, index) => (stickyStartStates[index] ? width : null)),
158-
});
159-
this._positionListener.stickyEndColumnsUpdated({
160-
sizes:
161-
firstStickyEnd === -1
162-
? []
163-
: cellWidths!
164-
.slice(firstStickyEnd)
165-
.map((width, index) => (stickyEndStates[index + firstStickyEnd] ? width : null))
166-
.reverse(),
167-
});
146+
if (stickyEndStates[i]) {
147+
this._addStickyStyle(cell, end, endPositions[i], i === firstStickyEnd);
148+
}
168149
}
169-
});
150+
}
151+
152+
if (this._positionListener) {
153+
this._positionListener.stickyColumnsUpdated({
154+
sizes:
155+
lastStickyStart === -1
156+
? []
157+
: cellWidths
158+
.slice(0, lastStickyStart + 1)
159+
.map((width, index) => (stickyStartStates[index] ? width : null)),
160+
});
161+
this._positionListener.stickyEndColumnsUpdated({
162+
sizes:
163+
firstStickyEnd === -1
164+
? []
165+
: cellWidths
166+
.slice(firstStickyEnd)
167+
.map((width, index) => (stickyEndStates[index + firstStickyEnd] ? width : null))
168+
.reverse(),
169+
});
170+
}
170171
});
171172
}
172173

@@ -187,7 +188,9 @@ export class StickyStyler {
187188
return;
188189
}
189190

190-
this._coalescedStyleScheduler.scheduleEarlyRead(() => {
191+
// Coalesce with other sticky row updates (top/bottom), sticky columns updates
192+
// (and potentially other changes like column resize).
193+
this._coalescedStyleScheduler.schedule(() => {
191194
// If positioning the rows to the bottom, reverse their order when evaluating the sticky
192195
// position such that the last row stuck will be "bottom: 0px" and so on. Note that the
193196
// sticky states need to be reversed as well.
@@ -216,33 +219,32 @@ export class StickyStyler {
216219
}
217220

218221
const borderedRowIndex = states.lastIndexOf(true);
219-
this._coalescedStyleScheduler.scheduleWrite(() => {
220-
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
221-
if (!states[rowIndex]) {
222-
continue;
223-
}
224222

225-
const offset = stickyOffsets[rowIndex];
226-
const isBorderedRowIndex = rowIndex === borderedRowIndex;
227-
for (const element of elementsToStick[rowIndex]) {
228-
this._addStickyStyle(element, position, offset, isBorderedRowIndex);
229-
}
223+
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
224+
if (!states[rowIndex]) {
225+
continue;
230226
}
231227

232-
if (position === 'top') {
233-
this._positionListener?.stickyHeaderRowsUpdated({
234-
sizes: stickyCellHeights,
235-
offsets: stickyOffsets,
236-
elements: elementsToStick,
237-
});
238-
} else {
239-
this._positionListener?.stickyFooterRowsUpdated({
240-
sizes: stickyCellHeights,
241-
offsets: stickyOffsets,
242-
elements: elementsToStick,
243-
});
228+
const offset = stickyOffsets[rowIndex];
229+
const isBorderedRowIndex = rowIndex === borderedRowIndex;
230+
for (const element of elementsToStick[rowIndex]) {
231+
this._addStickyStyle(element, position, offset, isBorderedRowIndex);
244232
}
245-
});
233+
}
234+
235+
if (position === 'top') {
236+
this._positionListener?.stickyHeaderRowsUpdated({
237+
sizes: stickyCellHeights,
238+
offsets: stickyOffsets,
239+
elements: elementsToStick,
240+
});
241+
} else {
242+
this._positionListener?.stickyFooterRowsUpdated({
243+
sizes: stickyCellHeights,
244+
offsets: stickyOffsets,
245+
elements: elementsToStick,
246+
});
247+
}
246248
});
247249
}
248250

@@ -258,7 +260,7 @@ export class StickyStyler {
258260
}
259261

260262
// Coalesce with other sticky updates (and potentially other changes like column resize).
261-
this._coalescedStyleScheduler.scheduleWrite(() => {
263+
this._coalescedStyleScheduler.schedule(() => {
262264
const tfoot = tableElement.querySelector('tfoot')!;
263265

264266
if (stickyStates.some(state => !state)) {

src/cdk/table/table.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,9 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
727727
if (this._ngZone && NgZone.isInAngularZone()) {
728728
this._ngZone.onStable.pipe(take(1), takeUntil(this._onDestroy)).subscribe(() => {
729729
this.updateStickyColumnStyles();
730-
this._coalescedStyleScheduler.flushAfterRender();
731730
});
732731
} else {
733732
this.updateStickyColumnStyles();
734-
this._coalescedStyleScheduler.flushAfterRender();
735733
}
736734

737735
this.contentChanged.next();

tools/public_api_guard/cdk/table.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,9 @@ export const _COALESCED_STYLE_SCHEDULER: InjectionToken<_CoalescedStyleScheduler
423423
// @public
424424
export class _CoalescedStyleScheduler implements OnDestroy {
425425
constructor(_ngZone: NgZone);
426-
flushAfterRender(): void;
427426
ngOnDestroy(): void;
428427
schedule(task: () => unknown): void;
429-
scheduleEarlyRead(task: () => unknown): void;
430428
scheduleEnd(task: () => unknown): void;
431-
scheduleRead(task: () => unknown): void;
432-
scheduleWrite(task: () => unknown): void;
433429
// (undocumented)
434430
static ɵfac: i0.ɵɵFactoryDeclaration<_CoalescedStyleScheduler, never>;
435431
// (undocumented)

0 commit comments

Comments
 (0)