Skip to content

Commit dc43727

Browse files
committed
refactor: make all event emitters readonly
1 parent a5cad10 commit dc43727

File tree

69 files changed

+247
-277
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+247
-277
lines changed

guides/bidirectionality.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class MyCustomComponent {
2828
this.dir = directionality.value;
2929

3030
directionality.change.subscribe(() => {
31-
this.dir = directionality.value;
31+
this.dir = directionality.value;
3232
});
3333
}
3434
}

src/cdk-experimental/popover-edit/lens-directives.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
8484
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
8585
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
8686
// can move this back into `host`.
87-
// tslint:disable:no-host-decorator-in-concrete
87+
// tslint:disable-next-line:no-host-decorator-in-concrete
8888
@HostListener('ngSubmit')
8989
handleFormSubmit(): void {
9090
if (this.ignoreSubmitUnlessValid && !this.editRef.isValid()) { return; }
@@ -107,7 +107,7 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
107107
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
108108
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
109109
// can move this back into `host`.
110-
// tslint:disable:no-host-decorator-in-concrete
110+
// tslint:disable-next-line:no-host-decorator-in-concrete
111111
@HostListener('document:click', ['$event'])
112112
handlePossibleClickOut(evt: Event): void {
113113
if (closest(evt.target, EDIT_PANE_SELECTOR)) { return; }
@@ -128,7 +128,7 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
128128
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
129129
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
130130
// can move this back into `host`.
131-
// tslint:disable:no-host-decorator-in-concrete
131+
// tslint:disable-next-line:no-host-decorator-in-concrete
132132
@HostListener('keydown', ['$event'])
133133
_handleKeydown(event: KeyboardEvent) {
134134
if (event.key === 'Escape' && !hasModifierKey(event)) {
@@ -167,7 +167,7 @@ export class CdkEditRevert<FormValue> {
167167
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
168168
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
169169
// can move this back into `host`.
170-
// tslint:disable:no-host-decorator-in-concrete
170+
// tslint:disable-next-line:no-host-decorator-in-concrete
171171
@HostListener('click')
172172
revertEdit(): void {
173173
this.editRef.reset();
@@ -191,7 +191,7 @@ export class CdkEditClose<FormValue> {
191191
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
192192
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
193193
// can move this back into `host`.
194-
// tslint:disable:no-host-decorator-in-concrete
194+
// tslint:disable-next-line:no-host-decorator-in-concrete
195195
@HostListener('click')
196196
closeEdit(): void {
197197
// Note that we use `click` here, rather than a keyboard event, because some screen readers

src/cdk-experimental/popover-edit/table-directives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ export class CdkEditOpen {
478478
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
479479
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
480480
// can move this back into `host`.
481-
// tslint:disable:no-host-decorator-in-concrete
481+
// tslint:disable-next-line:no-host-decorator-in-concrete
482482
@HostListener('click', ['$event'])
483483
openEdit(evt: Event): void {
484484
this.editEventDispatcher.editing.next(closest(this.elementRef.nativeElement!, CELL_SELECTOR));

src/cdk/a11y/focus-monitor/focus-monitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export class FocusMonitor implements OnDestroy {
438438
})
439439
export class CdkMonitorFocus implements OnDestroy {
440440
private _monitorSubscription: Subscription;
441-
@Output() cdkFocusChange = new EventEmitter<FocusOrigin>();
441+
@Output() readonly cdkFocusChange = new EventEmitter<FocusOrigin>();
442442

443443
constructor(private _elementRef: ElementRef<HTMLElement>, private _focusMonitor: FocusMonitor) {
444444
this._monitorSubscription = this._focusMonitor.monitor(
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
### LiveAnnouncer
22
`LiveAnnouncer` is used to announce messages for screen-reader users using an `aria-live` region.
33
See [the W3C's WAI-ARIA](https://www.w3.org/TR/wai-aria/states_and_properties#aria-live)
4-
for more information on aria-live regions.
4+
for more information on aria-live regions.
55

66
#### Methods
77

88
##### `announce(message: string, politeness?: 'off' | 'polite' | 'assertive'): void`
9-
Announce the given message via aria-live region. The politeness argument determines the
9+
Announce the given message via aria-live region. The politeness argument determines the
1010
`aria-live` attribute on the announcer element, defaulting to 'polite'.
1111

1212
#### Examples
@@ -18,9 +18,8 @@ The LiveAnnouncer is injected into a component:
1818
})
1919
export class MyComponent {
2020

21-
constructor(liveAnnouncer: LiveAnnouncer) {
22-
liveAnnouncer.announce("Hey Google");
23-
}
24-
21+
constructor(liveAnnouncer: LiveAnnouncer) {
22+
liveAnnouncer.announce("Hey Google");
23+
}
2524
}
2625
```

src/cdk/accordion/accordion-item.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ export class CdkAccordionItem implements OnDestroy {
4141
/** Subscription to openAll/closeAll events. */
4242
private _openCloseAllSubscription = Subscription.EMPTY;
4343
/** Event emitted every time the AccordionItem is closed. */
44-
@Output() closed: EventEmitter<void> = new EventEmitter<void>();
44+
@Output() readonly closed = new EventEmitter<void>();
4545
/** Event emitted every time the AccordionItem is opened. */
46-
@Output() opened: EventEmitter<void> = new EventEmitter<void>();
46+
@Output() readonly opened = new EventEmitter<void>();
4747
/** Event emitted when the AccordionItem is destroyed. */
48-
@Output() destroyed: EventEmitter<void> = new EventEmitter<void>();
48+
@Output() readonly destroyed = new EventEmitter<void>();
4949

5050
/**
5151
* Emits whenever the expanded state of the accordion changes.
5252
* Primarily used to facilitate two-way binding.
5353
* @docs-private
5454
*/
55-
@Output() expandedChange: EventEmitter<boolean> = new EventEmitter<boolean>();
55+
@Output() readonly expandedChange = new EventEmitter<boolean>();
5656

5757
/** The unique AccordionItem id. */
5858
readonly id: string = `cdk-accordion-child-${nextId++}`;

src/cdk/bidi/dir.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class Dir implements Directionality, AfterContentInit, OnDestroy {
4040
_rawDir: string;
4141

4242
/** Event emitted when the direction changes. */
43-
@Output('dirChange') change = new EventEmitter<Direction>();
43+
@Output('dirChange') readonly change = new EventEmitter<Direction>();
4444

4545
/** @docs-private */
4646
@Input()

src/cdk/clipboard/copy-to-clipboard.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const COPY_CONTENT = 'copy content';
1212
})
1313
class CopyToClipboardHost {
1414
@Input() content = '';
15-
@Output() copied = new EventEmitter<boolean>();
15+
@Output() readonly copied = new EventEmitter<boolean>();
1616
}
1717

1818
describe('CdkCopyToClipboard', () => {

src/cdk/clipboard/copy-to-clipboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class CdkCopyToClipboard {
2828
* Emits when some text is copied to the clipboard. The
2929
* emitted value indicates whether copying was successful.
3030
*/
31-
@Output() copied = new EventEmitter<boolean>();
31+
@Output() readonly copied = new EventEmitter<boolean>();
3232

3333
constructor(private readonly _clipboard: Clipboard) {}
3434

src/cdk/drag-drop/directives/drag.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function CDK_DRAG_CONFIG_FACTORY(): DragRefConfig {
7979
providers: [{provide: CDK_DRAG_PARENT, useExisting: CdkDrag}]
8080
})
8181
export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
82-
private _destroyed = new Subject<void>();
82+
private readonly _destroyed = new Subject<void>();
8383

8484
/** Reference to the underlying drag instance. */
8585
_dragRef: DragRef<CdkDrag<T>>;
@@ -149,32 +149,29 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
149149
@Input('cdkDragPreviewClass') previewClass: string | string[];
150150

151151
/** Emits when the user starts dragging the item. */
152-
@Output('cdkDragStarted') started: EventEmitter<CdkDragStart> = new EventEmitter<CdkDragStart>();
152+
@Output('cdkDragStarted') readonly started = new EventEmitter<CdkDragStart>();
153153

154154
/** Emits when the user has released a drag item, before any animations have started. */
155-
@Output('cdkDragReleased') released: EventEmitter<CdkDragRelease> =
156-
new EventEmitter<CdkDragRelease>();
155+
@Output('cdkDragReleased') readonly released = new EventEmitter<CdkDragRelease>();
157156

158157
/** Emits when the user stops dragging an item in the container. */
159-
@Output('cdkDragEnded') ended: EventEmitter<CdkDragEnd> = new EventEmitter<CdkDragEnd>();
158+
@Output('cdkDragEnded') readonly ended = new EventEmitter<CdkDragEnd>();
160159

161160
/** Emits when the user has moved the item into a new container. */
162-
@Output('cdkDragEntered') entered: EventEmitter<CdkDragEnter<any>> =
163-
new EventEmitter<CdkDragEnter<any>>();
161+
@Output('cdkDragEntered') readonly entered = new EventEmitter<CdkDragEnter<any>>();
164162

165163
/** Emits when the user removes the item its container by dragging it into another container. */
166-
@Output('cdkDragExited') exited: EventEmitter<CdkDragExit<any>> =
167-
new EventEmitter<CdkDragExit<any>>();
164+
@Output('cdkDragExited') readonly exited = new EventEmitter<CdkDragExit<any>>();
168165

169166
/** Emits when the user drops the item inside a container. */
170-
@Output('cdkDragDropped') dropped: EventEmitter<CdkDragDrop<any>> =
171-
new EventEmitter<CdkDragDrop<any>>();
167+
@Output('cdkDragDropped') readonly dropped = new EventEmitter<CdkDragDrop<any>>();
172168

173169
/**
174170
* Emits as the user is dragging the item. Use with caution,
175171
* because this event will fire for every pixel that the user has dragged.
176172
*/
177-
@Output('cdkDragMoved') moved: Observable<CdkDragMove<T>> =
173+
@Output('cdkDragMoved')
174+
readonly moved =
178175
new Observable((observer: Observer<CdkDragMove<T>>) => {
179176
const subscription = this._dragRef.moved.pipe(map(movedEvent => ({
180177
source: this,

src/cdk/drag-drop/directives/drop-list.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export interface CdkDropListInternal extends CdkDropList {}
6060
})
6161
export class CdkDropList<T = any> implements AfterContentInit, OnDestroy {
6262
/** Emits when the list has been destroyed. */
63-
private _destroyed = new Subject<void>();
63+
private readonly _destroyed = new Subject<void>();
6464

6565
/** Keeps track of the drop lists that are currently on the page. */
6666
private static _dropLists: CdkDropList[] = [];
@@ -120,25 +120,21 @@ export class CdkDropList<T = any> implements AfterContentInit, OnDestroy {
120120
autoScrollDisabled: boolean = false;
121121

122122
/** Emits when the user drops an item inside the container. */
123-
@Output('cdkDropListDropped')
124-
dropped: EventEmitter<CdkDragDrop<T, any>> = new EventEmitter<CdkDragDrop<T, any>>();
123+
@Output('cdkDropListDropped') readonly dropped = new EventEmitter<CdkDragDrop<T, any>>();
125124

126125
/**
127126
* Emits when the user has moved a new drag item into this container.
128127
*/
129-
@Output('cdkDropListEntered')
130-
entered: EventEmitter<CdkDragEnter<T>> = new EventEmitter<CdkDragEnter<T>>();
128+
@Output('cdkDropListEntered') readonly entered = new EventEmitter<CdkDragEnter<T>>();
131129

132130
/**
133131
* Emits when the user removes an item from the container
134132
* by dragging it into another container.
135133
*/
136-
@Output('cdkDropListExited')
137-
exited: EventEmitter<CdkDragExit<T>> = new EventEmitter<CdkDragExit<T>>();
134+
@Output('cdkDropListExited') readonly exited = new EventEmitter<CdkDragExit<T>>();
138135

139136
/** Emits as the user is swapping items while actively dragging. */
140-
@Output('cdkDropListSorted')
141-
sorted: EventEmitter<CdkDragSortEvent<T>> = new EventEmitter<CdkDragSortEvent<T>>();
137+
@Output('cdkDropListSorted') readonly sorted = new EventEmitter<CdkDragSortEvent<T>>();
142138

143139
constructor(
144140
/** Element that the drop list is attached to. */

src/cdk/observers/observe-content.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ContentObserver implements OnDestroy {
4040
/** Keeps track of the existing MutationObservers so they can be reused. */
4141
private _observedElements = new Map<Element, {
4242
observer: MutationObserver | null,
43-
stream: Subject<MutationRecord[]>,
43+
readonly stream: Subject<MutationRecord[]>,
4444
count: number
4545
}>();
4646

@@ -135,7 +135,7 @@ export class ContentObserver implements OnDestroy {
135135
})
136136
export class CdkObserveContent implements AfterContentInit, OnDestroy {
137137
/** Event emitted for each change in the element's content. */
138-
@Output('cdkObserveContent') event = new EventEmitter<MutationRecord[]>();
138+
@Output('cdkObserveContent') readonly event = new EventEmitter<MutationRecord[]>();
139139

140140
/**
141141
* Whether observing content is disabled. This option can be used

src/cdk/overlay/overlay-directives.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,19 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
199199
set push(value: boolean) { this._push = coerceBooleanProperty(value); }
200200

201201
/** Event emitted when the backdrop is clicked. */
202-
@Output() backdropClick = new EventEmitter<MouseEvent>();
202+
@Output() readonly backdropClick = new EventEmitter<MouseEvent>();
203203

204204
/** Event emitted when the position has changed. */
205-
@Output() positionChange = new EventEmitter<ConnectedOverlayPositionChange>();
205+
@Output() readonly positionChange = new EventEmitter<ConnectedOverlayPositionChange>();
206206

207207
/** Event emitted when the overlay has been attached. */
208-
@Output() attach = new EventEmitter<void>();
208+
@Output() readonly attach = new EventEmitter<void>();
209209

210210
/** Event emitted when the overlay has been detached. */
211-
@Output() detach = new EventEmitter<void>();
211+
@Output() readonly detach = new EventEmitter<void>();
212212

213213
/** Emits when there are keyboard events that are targeted at the overlay. */
214-
@Output() overlayKeydown = new EventEmitter<KeyboardEvent>();
214+
@Output() readonly overlayKeydown = new EventEmitter<KeyboardEvent>();
215215

216216
// TODO(jelbourn): inputs for size, scroll behavior, animation, etc.
217217

src/cdk/portal/portal-directives.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
118118
}
119119

120120
/** Emits when a portal is attached to the outlet. */
121-
@Output() attached: EventEmitter<CdkPortalOutletAttachedRef> =
122-
new EventEmitter<CdkPortalOutletAttachedRef>();
121+
@Output() readonly attached = new EventEmitter<CdkPortalOutletAttachedRef>();
123122

124123
/** Component or view reference that is attached to the portal. */
125124
get attachedRef(): CdkPortalOutletAttachedRef {

src/cdk/scrolling/virtual-scroll-viewport.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ const SCROLL_SCHEDULER =
6464
})
6565
export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, OnDestroy {
6666
/** Emits when the viewport is detached from a CdkVirtualForOf. */
67-
private _detachedSubject = new Subject<void>();
67+
private readonly _detachedSubject = new Subject<void>();
6868

6969
/** Emits when the rendered range changes. */
70-
private _renderedRangeSubject = new Subject<ListRange>();
70+
private readonly _renderedRangeSubject = new Subject<ListRange>();
7171

7272
/** The direction the viewport scrolls. */
7373
@Input()
@@ -87,16 +87,16 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
8787
// depending on how the strategy calculates the scrolled index, it may come at a cost to
8888
// performance.
8989
/** Emits when the index of the first element visible in the viewport changes. */
90-
@Output() scrolledIndexChange: Observable<number> =
91-
new Observable((observer: Observer<number>) =>
92-
this._scrollStrategy.scrolledIndexChange.subscribe(index =>
93-
Promise.resolve().then(() => this.ngZone.run(() => observer.next(index)))));
90+
@Output()
91+
readonly scrolledIndexChange = new Observable(
92+
(observer: Observer<number>) => this._scrollStrategy.scrolledIndexChange.subscribe(
93+
index => Promise.resolve().then(() => this.ngZone.run(() => observer.next(index)))));
9494

9595
/** The element that wraps the rendered content. */
9696
@ViewChild('contentWrapper', {static: true}) _contentWrapper: ElementRef<HTMLElement>;
9797

9898
/** A stream that emits whenever the rendered range changes. */
99-
renderedRangeStream: Observable<ListRange> = this._renderedRangeSubject.asObservable();
99+
readonly renderedRangeStream = this._renderedRangeSubject.asObservable();
100100

101101
/**
102102
* The total size of all content (in pixels), including content that is not currently rendered.

src/cdk/stepper/stepper.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export class CdkStep implements OnChanges {
243243
})
244244
export class CdkStepper implements AfterViewInit, OnDestroy {
245245
/** Emits when the component is destroyed. */
246-
protected _destroyed = new Subject<void>();
246+
protected readonly _destroyed = new Subject<void>();
247247

248248
/** Used for managing keyboard focus. */
249249
private _keyManager: FocusKeyManager<FocusableOption>;
@@ -324,8 +324,7 @@ export class CdkStepper implements AfterViewInit, OnDestroy {
324324
}
325325

326326
/** Event emitted when the selected step has changed. */
327-
@Output()
328-
selectionChange: EventEmitter<StepperSelectionEvent> = new EventEmitter<StepperSelectionEvent>();
327+
@Output() readonly selectionChange = new EventEmitter<StepperSelectionEvent>();
329328

330329
/** Used to track unique ID for each stepper component. */
331330
_groupId: number;
@@ -550,14 +549,14 @@ interface AbstractControlLike {
550549
pristine: boolean;
551550
root: AbstractControlLike;
552551
status: string;
553-
statusChanges: Observable<any>;
552+
readonly statusChanges: Observable<any>;
554553
touched: boolean;
555554
untouched: boolean;
556555
updateOn: any;
557556
valid: boolean;
558557
validator: ((control: any) => any) | null;
559558
value: any;
560-
valueChanges: Observable<any>;
559+
readonly valueChanges: Observable<any>;
561560
clearAsyncValidators(): void;
562561
clearValidators(): void;
563562
disable(opts?: any): void;

src/cdk/testing/private/mock-ng-zone.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {EventEmitter, Injectable, NgZone} from '@angular/core';
1818
*/
1919
@Injectable()
2020
export class MockNgZone extends NgZone {
21-
onStable: EventEmitter<any> = new EventEmitter(false);
21+
onStable = new EventEmitter<any>();
2222

2323
constructor() {
2424
super({enableLongStackTrace: false});

src/cdk/text-field/autofill.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export type AutofillEvent = {
3232

3333
/** Used to track info about currently monitored elements. */
3434
type MonitoredElementInfo = {
35-
subject: Subject<AutofillEvent>;
36-
unlisten: () => void;
35+
readonly subject: Subject<AutofillEvent>; unlisten: () => void;
3736
};
3837

3938

@@ -147,7 +146,7 @@ export class AutofillMonitor implements OnDestroy {
147146
})
148147
export class CdkAutofill implements OnDestroy, OnInit {
149148
/** Emits when the autofill state of the element changes. */
150-
@Output() cdkAutofill: EventEmitter<AutofillEvent> = new EventEmitter<AutofillEvent>();
149+
@Output() readonly cdkAutofill = new EventEmitter<AutofillEvent>();
151150

152151
constructor(private _elementRef: ElementRef<HTMLElement>,
153152
private _autofillMonitor: AutofillMonitor) {}

src/cdk/tree/tree.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('CdkTree', () => {
3636
let dataSource: FakeDataSource;
3737
let treeElement: HTMLElement;
3838
let tree: CdkTree<TestData>;
39-
let dir: {value: Direction, change: EventEmitter<Direction>};
39+
let dir: {value: Direction, readonly change: EventEmitter<Direction>};
4040

4141
function configureCdkTreeTestingModule(declarations: Type<any>[]) {
4242
TestBed.configureTestingModule({

0 commit comments

Comments
 (0)