Skip to content

Commit 890ee3f

Browse files
committed
fix(cdk/table): resolve breaking constructor changes
In #19964 and #19750 some breaking constructor changes were made which eventually got released as a part of a patch branch which doesn't follow our breaking changes policy. These changes make the new constructor parameters optional and add fallbacks to the old behavior. Fixes #20422.
1 parent f33210c commit 890ee3f

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

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

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

9-
import {Injectable, NgZone, OnDestroy} from '@angular/core';
9+
import {Injectable, NgZone, OnDestroy, InjectionToken} from '@angular/core';
1010
import {from, Subject} from 'rxjs';
1111
import {take, takeUntil} from 'rxjs/operators';
1212

@@ -18,6 +18,10 @@ export class _Schedule {
1818
endTasks: (() => unknown)[] = [];
1919
}
2020

21+
/** Injection token used to provide a coalesced style scheduler. */
22+
export const _COALESCED_STYLE_SCHEDULER =
23+
new InjectionToken<_CoalescedStyleScheduler>('_COALESCED_STYLE_SCHEDULER');
24+
2125
/**
2226
* Allows grouping up CSSDom mutations after the current execution context.
2327
* This can significantly improve performance when separate consecutive functions are

src/cdk/table/sticky-styler.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export class StickyStyler {
4242
constructor(private _isNativeHtmlTable: boolean,
4343
private _stickCellCss: string,
4444
public direction: Direction,
45-
private _coalescedStyleScheduler: _CoalescedStyleScheduler,
45+
/**
46+
* @deprecated `_coalescedStyleScheduler` parameter to become required.
47+
* @breaking-change 11.0.0
48+
*/
49+
private _coalescedStyleScheduler?: _CoalescedStyleScheduler,
4650
private _isBrowser = true,
4751
private readonly _needsPositionStickyOnElement = true) { }
4852

@@ -68,7 +72,7 @@ export class StickyStyler {
6872
}
6973

7074
// Coalesce with sticky row/column updates (and potentially other changes like column resize).
71-
this._coalescedStyleScheduler.schedule(() => {
75+
this._scheduleStyleChanges(() => {
7276
for (const element of elementsToClear) {
7377
this._removeStickyStyle(element, stickyDirections);
7478
}
@@ -99,7 +103,7 @@ export class StickyStyler {
99103
const endPositions = this._getStickyEndColumnPositions(cellWidths, stickyEndStates);
100104

101105
// Coalesce with sticky row updates (and potentially other changes like column resize).
102-
this._coalescedStyleScheduler.schedule(() => {
106+
this._scheduleStyleChanges(() => {
103107
const isRtl = this.direction === 'rtl';
104108
const start = isRtl ? 'right' : 'left';
105109
const end = isRtl ? 'left' : 'right';
@@ -163,7 +167,7 @@ export class StickyStyler {
163167

164168
// Coalesce with other sticky row updates (top/bottom), sticky columns updates
165169
// (and potentially other changes like column resize).
166-
this._coalescedStyleScheduler.schedule(() => {
170+
this._scheduleStyleChanges(() => {
167171
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
168172
if (!states[rowIndex]) {
169173
continue;
@@ -191,7 +195,7 @@ export class StickyStyler {
191195
const tfoot = tableElement.querySelector('tfoot')!;
192196

193197
// Coalesce with other sticky updates (and potentially other changes like column resize).
194-
this._coalescedStyleScheduler.schedule(() => {
198+
this._scheduleStyleChanges(() => {
195199
if (stickyStates.some(state => !state)) {
196200
this._removeStickyStyle(tfoot, ['bottom']);
197201
} else {
@@ -323,4 +327,17 @@ export class StickyStyler {
323327

324328
return positions;
325329
}
330+
331+
/**
332+
* Schedules styles to be applied when the style scheduler deems appropriate.
333+
* @breaking-change 11.0.0 This method can be removed in favor of calling
334+
* `CoalescedStyleScheduler.schedule` directly once the scheduler is a required parameter.
335+
*/
336+
private _scheduleStyleChanges(changes: () => void) {
337+
if (this._coalescedStyleScheduler) {
338+
this._coalescedStyleScheduler.schedule(changes);
339+
} else {
340+
changes();
341+
}
342+
}
326343
}

src/cdk/table/table.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import {
5757
} from 'rxjs';
5858
import {takeUntil} from 'rxjs/operators';
5959
import {CdkColumnDef} from './cell';
60-
import {_CoalescedStyleScheduler} from './coalesced-style-scheduler';
60+
import {_CoalescedStyleScheduler, _COALESCED_STYLE_SCHEDULER} from './coalesced-style-scheduler';
6161
import {
6262
BaseRowDef,
6363
CdkCellOutlet,
@@ -199,7 +199,7 @@ export interface RenderRow<T> {
199199
providers: [
200200
{provide: CDK_TABLE, useExisting: CdkTable},
201201
{provide: _VIEW_REPEATER_STRATEGY, useClass: _DisposeViewRepeaterStrategy},
202-
_CoalescedStyleScheduler,
202+
{provide: _COALESCED_STYLE_SCHEDULER, useClass: _CoalescedStyleScheduler},
203203
]
204204
})
205205
export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDestroy, OnInit {
@@ -443,14 +443,18 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
443443
constructor(
444444
protected readonly _differs: IterableDiffers,
445445
protected readonly _changeDetectorRef: ChangeDetectorRef,
446-
protected readonly _coalescedStyleScheduler: _CoalescedStyleScheduler,
447446
protected readonly _elementRef: ElementRef, @Attribute('role') role: string,
448447
@Optional() protected readonly _dir: Directionality, @Inject(DOCUMENT) _document: any,
449448
private _platform: Platform,
450-
// Optional for backwards compatibility, but a view repeater strategy will always
451-
// be provided.
449+
450+
/**
451+
* @deprecated `_coalescedStyleScheduler` and `_viewRepeater` parameters to become required.
452+
* @breaking-change 11.0.0
453+
*/
452454
@Optional() @Inject(_VIEW_REPEATER_STRATEGY)
453-
protected readonly _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>) {
455+
protected readonly _viewRepeater?: _ViewRepeater<T, RenderRow<T>, RowContext<T>>,
456+
@Optional() @Inject(_COALESCED_STYLE_SCHEDULER)
457+
protected readonly _coalescedStyleScheduler?: _CoalescedStyleScheduler) {
454458
if (!role) {
455459
this._elementRef.nativeElement.setAttribute('role', 'grid');
456460
}
@@ -549,11 +553,14 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
549553
return;
550554
}
551555
const viewContainer = this._rowOutlet.viewContainer;
552-
this._viewRepeater.applyChanges(
556+
557+
// @breaking-change 11.0.0 Remove null check for `_viewRepeater`
558+
// once it's a required parameter in the constructor.
559+
this._viewRepeater?.applyChanges(
553560
changes,
554561
viewContainer,
555562
(record: IterableChangeRecord<RenderRow<T>>,
556-
adjustedPreviousIndex: number|null,
563+
_adjustedPreviousIndex: number|null,
557564
currentIndex: number|null) => this._getEmbeddedViewArgs(record.item, currentIndex!),
558565
(record) => record.item.data,
559566
(change: _ViewRepeaterItemChange<RenderRow<T>, RowContext<T>>) => {

tools/public_api_guard/cdk/table.d.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export declare const _COALESCED_STYLE_SCHEDULER: InjectionToken<_CoalescedStyleScheduler>;
2+
13
export declare class _CoalescedStyleScheduler implements OnDestroy {
24
constructor(_ngZone: NgZone);
35
ngOnDestroy(): void;
@@ -186,7 +188,7 @@ export declare class CdkRowDef<T> extends BaseRowDef {
186188

187189
export declare class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDestroy, OnInit {
188190
protected readonly _changeDetectorRef: ChangeDetectorRef;
189-
protected readonly _coalescedStyleScheduler: _CoalescedStyleScheduler;
191+
protected readonly _coalescedStyleScheduler?: _CoalescedStyleScheduler | undefined;
190192
_contentColumnDefs: QueryList<CdkColumnDef>;
191193
_contentFooterRowDefs: QueryList<CdkFooterRowDef>;
192194
_contentHeaderRowDefs: QueryList<CdkHeaderRowDef>;
@@ -201,7 +203,7 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
201203
_noDataRow: CdkNoDataRow;
202204
_noDataRowOutlet: NoDataRowOutlet;
203205
_rowOutlet: DataRowOutlet;
204-
protected readonly _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>;
206+
protected readonly _viewRepeater?: _ViewRepeater<T, RenderRow<T>, RowContext<T>> | undefined;
205207
get dataSource(): CdkTableDataSourceInput<T>;
206208
set dataSource(dataSource: CdkTableDataSourceInput<T>);
207209
get multiTemplateDataRows(): boolean;
@@ -214,7 +216,8 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
214216
start: number;
215217
end: number;
216218
}>;
217-
constructor(_differs: IterableDiffers, _changeDetectorRef: ChangeDetectorRef, _coalescedStyleScheduler: _CoalescedStyleScheduler, _elementRef: ElementRef, role: string, _dir: Directionality, _document: any, _platform: Platform, _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>);
219+
constructor(_differs: IterableDiffers, _changeDetectorRef: ChangeDetectorRef, _elementRef: ElementRef, role: string, _dir: Directionality, _document: any, _platform: Platform,
220+
_viewRepeater?: _ViewRepeater<T, RenderRow<T>, RowContext<T>> | undefined, _coalescedStyleScheduler?: _CoalescedStyleScheduler | undefined);
218221
_getRenderedRows(rowOutlet: RowOutlet): HTMLElement[];
219222
_getRowDefs(data: T, dataIndex: number): CdkRowDef<T>[];
220223
addColumnDef(columnDef: CdkColumnDef): void;
@@ -234,7 +237,7 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
234237
updateStickyHeaderRowStyles(): void;
235238
static ngAcceptInputType_multiTemplateDataRows: BooleanInput;
236239
static ɵcmp: i0.ɵɵComponentDefWithMeta<CdkTable<any>, "cdk-table, table[cdk-table]", ["cdkTable"], { "trackBy": "trackBy"; "dataSource": "dataSource"; "multiTemplateDataRows": "multiTemplateDataRows"; }, {}, ["_noDataRow", "_contentColumnDefs", "_contentRowDefs", "_contentHeaderRowDefs", "_contentFooterRowDefs"], ["caption", "colgroup, col"]>;
237-
static ɵfac: i0.ɵɵFactoryDef<CdkTable<any>, [null, null, null, null, { attribute: "role"; }, { optional: true; }, null, null, { optional: true; }]>;
240+
static ɵfac: i0.ɵɵFactoryDef<CdkTable<any>, [null, null, null, { attribute: "role"; }, { optional: true; }, null, null, { optional: true; }, { optional: true; }]>;
238241
}
239242

240243
export declare class CdkTableModule {
@@ -319,7 +322,8 @@ export declare type StickyDirection = 'top' | 'bottom' | 'left' | 'right';
319322

320323
export declare class StickyStyler {
321324
direction: Direction;
322-
constructor(_isNativeHtmlTable: boolean, _stickCellCss: string, direction: Direction, _coalescedStyleScheduler: _CoalescedStyleScheduler, _isBrowser?: boolean, _needsPositionStickyOnElement?: boolean);
325+
constructor(_isNativeHtmlTable: boolean, _stickCellCss: string, direction: Direction,
326+
_coalescedStyleScheduler?: _CoalescedStyleScheduler | undefined, _isBrowser?: boolean, _needsPositionStickyOnElement?: boolean);
323327
_addStickyStyle(element: HTMLElement, dir: StickyDirection, dirValue: number): void;
324328
_getCalculatedZIndex(element: HTMLElement): string;
325329
_getCellWidths(row: HTMLElement): number[];

0 commit comments

Comments
 (0)