Skip to content

Commit

Permalink
refactor(multiple): fix initializers using constructor members (#29588)
Browse files Browse the repository at this point in the history
Fixes the cases where the initializers of properties were referencing members initialized in the constructor. This was preventing compatibility with `useDefineForClassFields`.
  • Loading branch information
crisbeto authored Aug 15, 2024
1 parent 1abb484 commit 5de822d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/cdk-experimental/column-resize/column-resize-notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Injectable} from '@angular/core';
import {inject, Injectable} from '@angular/core';
import {Observable, Subject} from 'rxjs';

/** Indicates the width of a column. */
Expand Down Expand Up @@ -55,11 +55,11 @@ export class ColumnResizeNotifierSource {
/** Service for triggering column resizes imperatively or being notified of them. */
@Injectable()
export class ColumnResizeNotifier {
private readonly _source = inject(ColumnResizeNotifierSource);

/** Emits whenever a column is resized. */
readonly resizeCompleted: Observable<ColumnSize> = this._source.resizeCompleted;

constructor(private readonly _source: ColumnResizeNotifierSource) {}

/** Instantly resizes the specified column. */
resize(columnId: string, size: number): void {
this._source.triggerResize.next({
Expand Down
11 changes: 5 additions & 6 deletions src/cdk/a11y/interactivity-checker/interactivity-checker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {Platform} from '@angular/cdk/platform';
import {PLATFORM_ID} from '@angular/core';
import {inject} from '@angular/core/testing';
import {TestBed} from '@angular/core/testing';
import {InteractivityChecker, IsFocusableConfig} from './interactivity-checker';

describe('InteractivityChecker', () => {
let platform: Platform;
let testContainerElement: HTMLElement;
let checker: InteractivityChecker;

beforeEach(inject([PLATFORM_ID], (platformId: Object) => {
beforeEach(() => {
testContainerElement = document.createElement('div');
document.body.appendChild(testContainerElement);
platform = new Platform(platformId);
checker = new InteractivityChecker(platform);
}));
platform = TestBed.inject(Platform);
checker = TestBed.inject(InteractivityChecker);
});

afterEach(() => {
testContainerElement.remove();
Expand Down
9 changes: 7 additions & 2 deletions src/cdk/platform/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Inject, Injectable, PLATFORM_ID} from '@angular/core';
import {inject, Injectable, PLATFORM_ID} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';

// Whether the current platform supports the V8 Break Iterator. The V8 check
Expand All @@ -30,6 +30,8 @@ try {
*/
@Injectable({providedIn: 'root'})
export class Platform {
private _platformId = inject(PLATFORM_ID);

// We want to use the Angular platform check because if the Document is shimmed
// without the navigator, the following checks will fail. This is preferred because
// sometimes the Document may be shimmed without the user's knowledge or intention
Expand Down Expand Up @@ -84,5 +86,8 @@ export class Platform {
/** Whether the current browser is Safari. */
SAFARI: boolean = this.isBrowser && /safari/i.test(navigator.userAgent) && this.WEBKIT;

constructor(@Inject(PLATFORM_ID) private _platformId: Object) {}
/** Backwards-compatible constructor. */
constructor(..._args: unknown[]);

constructor() {}
}
6 changes: 5 additions & 1 deletion src/cdk/testing/testbed/task-state-zone-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type PatchedProxyZone = ProxyZone & {
* This serves as a workaround for https://github.com/angular/angular/issues/32896.
*/
export class TaskStateZoneInterceptor {
private _lastState: HasTaskState | null = null;

/** Subject that can be used to emit a new state change. */
private readonly _stateSubject = new BehaviorSubject<TaskState>(
this._lastState ? this._getTaskStateFromInternalZoneState(this._lastState) : {stable: true},
Expand All @@ -38,7 +40,9 @@ export class TaskStateZoneInterceptor {
/** Public observable that emits whenever the task state changes. */
readonly state: Observable<TaskState> = this._stateSubject;

constructor(private _lastState: HasTaskState | null) {}
constructor(lastState: HasTaskState | null) {
this._lastState = lastState;
}

/** This will be called whenever the task state changes in the intercepted zone. */
onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) {
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/selection/row-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ import {Input, Directive} from '@angular/core';
})
export class MatRowSelection<T> extends CdkRowSelection<T> {
/** The value that is associated with the row */
@Input('matRowSelectionValue') override value: T;
@Input('matRowSelectionValue') override value: T = undefined!;
}
5 changes: 4 additions & 1 deletion src/material/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Optional,
ViewEncapsulation,
ANIMATION_MODULE_TYPE,
inject,
} from '@angular/core';
import {MatDialogConfig} from './dialog-config';
import {CdkDialogContainer} from '@angular/cdk/dialog';
Expand Down Expand Up @@ -72,6 +73,8 @@ export const CLOSE_ANIMATION_DURATION = 75;
},
})
export class MatDialogContainer extends CdkDialogContainer<MatDialogConfig> implements OnDestroy {
private _animationMode = inject(ANIMATION_MODULE_TYPE, {optional: true});

/** Emits when an animation state changes. */
_animationStateChanged = new EventEmitter<LegacyDialogAnimationEvent>();

Expand Down Expand Up @@ -102,7 +105,7 @@ export class MatDialogContainer extends CdkDialogContainer<MatDialogConfig> impl
interactivityChecker: InteractivityChecker,
ngZone: NgZone,
overlayRef: OverlayRef,
@Optional() @Inject(ANIMATION_MODULE_TYPE) private _animationMode?: string,
@Optional() @Inject(ANIMATION_MODULE_TYPE) _unusedAnimationMode?: string,
focusMonitor?: FocusMonitor,
) {
super(
Expand Down
8 changes: 5 additions & 3 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ export class MatSelect
ControlValueAccessor,
MatFormFieldControl<any>
{
protected _defaultOptions = inject(MAT_SELECT_CONFIG, {optional: true});

/** All of the defined select options. */
@ContentChildren(MatOption, {descendants: true}) options: QueryList<MatOption>;

Expand Down Expand Up @@ -607,7 +609,7 @@ export class MatSelect
@Attribute('tabindex') tabIndex: string,
@Inject(MAT_SELECT_SCROLL_STRATEGY) scrollStrategyFactory: any,
private _liveAnnouncer: LiveAnnouncer,
@Optional() @Inject(MAT_SELECT_CONFIG) protected _defaultOptions?: MatSelectConfig,
@Optional() @Inject(MAT_SELECT_CONFIG) _unusedDefaultOptions?: unknown,
) {
if (this.ngControl) {
// Note: we provide the value accessor through here, instead of
Expand All @@ -617,8 +619,8 @@ export class MatSelect

// Note that we only want to set this when the defaults pass it in, otherwise it should
// stay as `undefined` so that it falls back to the default in the key manager.
if (_defaultOptions?.typeaheadDebounceInterval != null) {
this.typeaheadDebounceInterval = _defaultOptions.typeaheadDebounceInterval;
if (this._defaultOptions?.typeaheadDebounceInterval != null) {
this.typeaheadDebounceInterval = this._defaultOptions.typeaheadDebounceInterval;
}

this._errorStateTracker = new _ErrorStateTracker(
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/cdk/platform.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function normalizePassiveListenerOptions(options: AddEventListenerOptions

// @public
export class Platform {
constructor(_platformId: Object);
constructor(..._args: unknown[]);
ANDROID: boolean;
BLINK: boolean;
EDGE: boolean;
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/dialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class MatDialogConfig<D = any> {

// @public (undocumented)
export class MatDialogContainer extends CdkDialogContainer<MatDialogConfig> implements OnDestroy {
constructor(elementRef: ElementRef, focusTrapFactory: FocusTrapFactory, _document: any, dialogConfig: MatDialogConfig, interactivityChecker: InteractivityChecker, ngZone: NgZone, overlayRef: OverlayRef, _animationMode?: string | undefined, focusMonitor?: FocusMonitor);
constructor(elementRef: ElementRef, focusTrapFactory: FocusTrapFactory, _document: any, dialogConfig: MatDialogConfig, interactivityChecker: InteractivityChecker, ngZone: NgZone, overlayRef: OverlayRef, _unusedAnimationMode?: string, focusMonitor?: FocusMonitor);
protected _actionSectionCount: number;
_animationsEnabled: boolean;
_animationStateChanged: EventEmitter<LegacyDialogAnimationEvent>;
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/material/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export { MatPrefix }
// @public (undocumented)
export class MatSelect implements AfterContentInit, OnChanges, OnDestroy, OnInit, DoCheck, ControlValueAccessor, MatFormFieldControl<any> {
constructor(_viewportRuler: ViewportRuler, _changeDetectorRef: ChangeDetectorRef,
_unusedNgZone: NgZone, defaultErrorStateMatcher: ErrorStateMatcher, _elementRef: ElementRef, _dir: Directionality, parentForm: NgForm, parentFormGroup: FormGroupDirective, _parentFormField: MatFormField, ngControl: NgControl, tabIndex: string, scrollStrategyFactory: any, _liveAnnouncer: LiveAnnouncer, _defaultOptions?: MatSelectConfig | undefined);
_unusedNgZone: NgZone, defaultErrorStateMatcher: ErrorStateMatcher, _elementRef: ElementRef, _dir: Directionality, parentForm: NgForm, parentFormGroup: FormGroupDirective, _parentFormField: MatFormField, ngControl: NgControl, tabIndex: string, scrollStrategyFactory: any, _liveAnnouncer: LiveAnnouncer, _unusedDefaultOptions?: unknown);
ariaLabel: string;
ariaLabelledby: string;
protected _canOpen(): boolean;
Expand All @@ -100,7 +100,7 @@ export class MatSelect implements AfterContentInit, OnChanges, OnDestroy, OnInit
controlType: string;
customTrigger: MatSelectTrigger;
// (undocumented)
protected _defaultOptions?: MatSelectConfig | undefined;
protected _defaultOptions: MatSelectConfig | null;
protected readonly _destroy: Subject<void>;
readonly disableAutomaticLabeling = true;
disabled: boolean;
Expand Down

0 comments on commit 5de822d

Please sign in to comment.