Skip to content

Commit 5b762a5

Browse files
committed
refactor: make all event emitters readonly
1 parent 25ce323 commit 5b762a5

File tree

156 files changed

+634
-687
lines changed

Some content is hidden

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

156 files changed

+634
-687
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/dialog/dialog-container.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,19 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
104104
@ViewChild(CdkPortalOutlet, {static: true}) _portalHost: CdkPortalOutlet;
105105

106106
/** A subject emitting before the dialog enters the view. */
107-
_beforeEnter: Subject<void> = new Subject();
107+
readonly _beforeEnter = new Subject<void>();
108108

109109
/** A subject emitting after the dialog enters the view. */
110-
_afterEnter: Subject<void> = new Subject();
110+
readonly _afterEnter = new Subject<void>();
111111

112112
/** A subject emitting before the dialog exits the view. */
113-
_beforeExit: Subject<void> = new Subject();
113+
readonly _beforeExit = new Subject<void>();
114114

115115
/** A subject emitting after the dialog exits the view. */
116-
_afterExit: Subject<void> = new Subject();
116+
readonly _afterExit = new Subject<void>();
117117

118118
/** Stream of animation `done` events. */
119-
_animationDone = new Subject<AnimationEvent>();
119+
readonly _animationDone = new Subject<AnimationEvent>();
120120

121121
constructor(
122122
private _elementRef: ElementRef<HTMLElement>,

src/cdk-experimental/dialog/dialog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class Dialog implements OnDestroy {
5454
_getAfterAllClosed(): Observable<void> {
5555
return this._parentDialog ? this._parentDialog.afterAllClosed : this._afterAllClosedBase;
5656
}
57-
_afterAllClosedBase = new Subject<void>();
57+
readonly _afterAllClosedBase = new Subject<void>();
5858

5959
// TODO(jelbourn): tighten the type on the right-hand side of this expression.
6060
afterAllClosed: Observable<void> = defer(() => this.openDialogs.length ?
@@ -64,7 +64,7 @@ export class Dialog implements OnDestroy {
6464
get afterOpened(): Subject<DialogRef<any>> {
6565
return this._parentDialog ? this._parentDialog.afterOpened : this._afterOpened;
6666
}
67-
_afterOpened: Subject<DialogRef<any>> = new Subject();
67+
readonly _afterOpened = new Subject<DialogRef<any>>();
6868

6969
/** Stream that emits when a dialog is opened. */
7070
get openDialogs(): DialogRef<any>[] {

src/cdk-experimental/listbox/listbox.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class CdkOption<T = unknown> implements ListKeyManagerOption, Highlightab
8787
this._value = value;
8888
}
8989

90-
@Output() readonly selectionChange: EventEmitter<OptionSelectionChangeEvent<T>> =
90+
@Output() readonly selectionChange =
9191
new EventEmitter<OptionSelectionChangeEvent<T>>();
9292

9393
constructor(private readonly _elementRef: ElementRef,
@@ -228,7 +228,7 @@ export class CdkListbox<T> implements AfterContentInit, OnDestroy, OnInit, Contr
228228

229229
@ContentChildren(CdkOption, {descendants: true}) _options: QueryList<CdkOption<T>>;
230230

231-
@Output() readonly selectionChange: EventEmitter<ListboxSelectionChangeEvent<T>> =
231+
@Output() readonly selectionChange =
232232
new EventEmitter<ListboxSelectionChangeEvent<T>>();
233233

234234
/**

src/cdk-experimental/menu/context-menu.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ export class CdkContextMenuTrigger implements OnDestroy {
118118
private _menuPanel: CdkMenuPanel;
119119

120120
/** Emits when the attached menu is requested to open. */
121-
@Output('cdkContextMenuOpened') readonly opened: EventEmitter<void> = new EventEmitter();
121+
@Output('cdkContextMenuOpened') readonly opened = new EventEmitter<void>();
122122

123123
/** Emits when the attached menu is requested to close. */
124-
@Output('cdkContextMenuClosed') readonly closed: EventEmitter<void> = new EventEmitter();
124+
@Output('cdkContextMenuClosed') readonly closed = new EventEmitter<void>();
125125

126126
/** Whether the context menu should be disabled. */
127127
@Input('cdkContextMenuDisabled')
@@ -140,7 +140,7 @@ export class CdkContextMenuTrigger implements OnDestroy {
140140
private _panelContent: TemplatePortal;
141141

142142
/** Emits when the element is destroyed. */
143-
private readonly _destroyed: Subject<void> = new Subject();
143+
private readonly _destroyed = new Subject<void>();
144144

145145
/** Reference to the document. */
146146
private readonly _document: Document;

src/cdk-experimental/menu/menu-bar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class CdkMenuBar extends CdkMenuGroup implements Menu, AfterContentInit,
7272
private _mouseFocusChanged: Observable<CdkMenuItem>;
7373

7474
/** Emits when the MenuBar is destroyed. */
75-
private readonly _destroyed: Subject<void> = new Subject();
75+
private readonly _destroyed = new Subject<void>();
7676

7777
/** All child MenuItem elements nested in this MenuBar. */
7878
@ContentChildren(CdkMenuItem, {descendants: true})

src/cdk-experimental/menu/menu-group.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ import {CdkMenuItem} from './menu-item';
3535
})
3636
export class CdkMenuGroup implements AfterContentInit, OnDestroy {
3737
/** Emits the element when checkbox or radiobutton state changed */
38-
@Output() readonly change: EventEmitter<CdkMenuItem> = new EventEmitter();
38+
@Output() readonly change = new EventEmitter<CdkMenuItem>();
3939

4040
/** List of menuitemcheckbox or menuitemradio elements which reside in this group */
4141
@ContentChildren(CdkMenuItemSelectable, {descendants: true})
4242
private readonly _selectableItems: QueryList<CdkMenuItemSelectable>;
4343

4444
/** Emits when the _selectableItems QueryList triggers a change */
45-
private readonly _selectableChanges: EventEmitter<void> = new EventEmitter();
45+
private readonly _selectableChanges = new EventEmitter<void>();
4646

4747
ngAfterContentInit() {
4848
this._registerMenuSelectionListeners();

src/cdk-experimental/menu/menu-item-selectable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let nextId = 0;
2020
@Directive()
2121
export abstract class CdkMenuItemSelectable extends CdkMenuItem {
2222
/** Event emitted when the selectable item is clicked */
23-
@Output() clicked: EventEmitter<CdkMenuItemSelectable> = new EventEmitter();
23+
@Output() readonly clicked = new EventEmitter<CdkMenuItemSelectable>();
2424

2525
/** Whether the element is checked */
2626
@Input()

src/cdk-experimental/menu/menu-item-trigger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export class CdkMenuItemTrigger implements OnDestroy {
7070
private _menuPanel?: CdkMenuPanel;
7171

7272
/** Emits when the attached menu is requested to open */
73-
@Output('cdkMenuOpened') readonly opened: EventEmitter<void> = new EventEmitter();
73+
@Output('cdkMenuOpened') readonly opened = new EventEmitter<void>();
7474

7575
/** Emits when the attached menu is requested to close */
76-
@Output('cdkMenuClosed') readonly closed: EventEmitter<void> = new EventEmitter();
76+
@Output('cdkMenuClosed') readonly closed = new EventEmitter<void>();
7777

7878
/** A reference to the overlay which manages the triggered menu */
7979
private _overlayRef: OverlayRef | null = null;

src/cdk-experimental/menu/menu-item.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ export class CdkMenuItem implements FocusableOption, FocusableElement, OnDestroy
6969
* If this MenuItem is a regular MenuItem, outputs when it is triggered by a keyboard or mouse
7070
* event.
7171
*/
72-
@Output('cdkMenuItemTriggered') triggered: EventEmitter<void> = new EventEmitter();
72+
@Output('cdkMenuItemTriggered') readonly triggered = new EventEmitter<void>();
7373

7474
/** Emits when the menu item is destroyed. */
75-
private readonly _destroyed: Subject<void> = new Subject();
75+
private readonly _destroyed = new Subject<void>();
7676

7777
constructor(
7878
readonly _elementRef: ElementRef<HTMLElement>,

src/cdk-experimental/menu/menu-stack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export class MenuStack {
3434
private readonly _elements: MenuStackItem[] = [];
3535

3636
/** Emits the element which was popped off of the stack when requested by a closer. */
37-
private readonly _close: Subject<MenuStackItem> = new Subject();
37+
private readonly _close = new Subject<MenuStackItem>();
3838

3939
/** Emits once the MenuStack has become empty after popping off elements. */
40-
private readonly _empty: Subject<FocusNext> = new Subject();
40+
private readonly _empty = new Subject<FocusNext>();
4141

4242
/** Observable which emits the MenuStackItem which has been requested to close. */
4343
readonly closed: Observable<MenuStackItem> = this._close;

src/cdk-experimental/menu/menu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class CdkMenu extends CdkMenuGroup implements Menu, AfterContentInit, OnI
6969
@Input('cdkMenuOrientation') orientation: 'horizontal' | 'vertical' = 'vertical';
7070

7171
/** Event emitted when the menu is closed. */
72-
@Output() readonly closed: EventEmitter<void | 'click' | 'tab' | 'escape'> = new EventEmitter();
72+
@Output() readonly closed = new EventEmitter<void | 'click' | 'tab' | 'escape'>();
7373

7474
// We provide a default MenuStack implementation in case the menu is an inline menu.
7575
// For Menus part of a MenuBar nested within a MenuPanel this will be overwritten

src/cdk-experimental/popover-edit/focus-escape-notifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const enum FocusEscapeNotifierDirection {
2222
* focus leaves the region.
2323
*/
2424
export class FocusEscapeNotifier extends FocusTrap {
25-
private _escapeSubject = new Subject<FocusEscapeNotifierDirection>();
25+
private readonly _escapeSubject = new Subject<FocusEscapeNotifierDirection>();
2626

2727
constructor(
2828
element: HTMLElement,

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

Lines changed: 6 additions & 8 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();
@@ -192,10 +192,8 @@ export class CdkEditClose<FormValue> {
192192
// In Ivy the `host` metadata will be merged, whereas in ViewEngine it is overridden. In order
193193
// to avoid double event listeners, we need to use `HostListener`. Once Ivy is the default, we
194194
// can move this back into `host`.
195-
// tslint:disable:no-host-decorator-in-concrete
196-
@HostListener('click')
197-
@HostListener('keydown.enter')
198-
@HostListener('keydown.space')
195+
// tslint:disable-next-line:no-host-decorator-in-concrete
196+
@HostListener('click') @HostListener('keydown.enter') @HostListener('keydown.space')
199197
closeEdit(): void {
200198
// Note that we use `click` here, rather than a keyboard event, because some screen readers
201199
// will emit a fake click event instead of an enter keyboard event on buttons. For the keyboard

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

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const FOCUS_MONITOR_DEFAULT_OPTIONS =
6868

6969
type MonitoredElementInfo = {
7070
checkChildren: boolean,
71-
subject: Subject<FocusOrigin>,
71+
readonly subject: Subject<FocusOrigin>,
7272
rootNode: HTMLElement|Document
7373
};
7474

@@ -565,7 +565,7 @@ function getTarget(event: Event): HTMLElement|null {
565565
})
566566
export class CdkMonitorFocus implements AfterViewInit, OnDestroy {
567567
private _monitorSubscription: Subscription;
568-
@Output() cdkFocusChange = new EventEmitter<FocusOrigin>();
568+
@Output() readonly cdkFocusChange = new EventEmitter<FocusOrigin>();
569569

570570
constructor(private _elementRef: ElementRef<HTMLElement>, private _focusMonitor: FocusMonitor) {}
571571

src/cdk/a11y/key-manager/list-key-manager.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FakeHighlightable {
2626
}
2727

2828
class FakeQueryList<T> extends QueryList<T> {
29-
changes = new Subject<FakeQueryList<T>>();
29+
readonly changes = new Subject<FakeQueryList<T>>();
3030
items: T[];
3131
get length() { return this.items.length; }
3232
set length(_) { /* Empty setter for base class constructor */ }

src/cdk/a11y/key-manager/list-key-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
4444
private _activeItemIndex = -1;
4545
private _activeItem: T | null = null;
4646
private _wrap = false;
47-
private _letterKeyStream = new Subject<string>();
47+
private readonly _letterKeyStream = new Subject<string>();
4848
private _typeaheadSubscription = Subscription.EMPTY;
4949
private _vertical = true;
5050
private _horizontal: 'ltr' | 'rtl' | null;
@@ -82,10 +82,10 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
8282
* Stream that emits any time the TAB key is pressed, so components can react
8383
* when focus is shifted off of the list.
8484
*/
85-
tabOut: Subject<void> = new Subject<void>();
85+
readonly tabOut = new Subject<void>();
8686

8787
/** Stream that emits whenever the active item of the list manager changes. */
88-
change = new Subject<number>();
88+
readonly change = new Subject<number>();
8989

9090
/**
9191
* Sets the predicate function that determines which items should be skipped by the
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
@@ -42,18 +42,18 @@ export class CdkAccordionItem implements OnDestroy {
4242
/** Subscription to openAll/closeAll events. */
4343
private _openCloseAllSubscription = Subscription.EMPTY;
4444
/** Event emitted every time the AccordionItem is closed. */
45-
@Output() closed: EventEmitter<void> = new EventEmitter<void>();
45+
@Output() readonly closed = new EventEmitter<void>();
4646
/** Event emitted every time the AccordionItem is opened. */
47-
@Output() opened: EventEmitter<void> = new EventEmitter<void>();
47+
@Output() readonly opened = new EventEmitter<void>();
4848
/** Event emitted when the AccordionItem is destroyed. */
49-
@Output() destroyed: EventEmitter<void> = new EventEmitter<void>();
49+
@Output() readonly destroyed = new EventEmitter<void>();
5050

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

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

src/cdk/accordion/accordion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class CdkAccordion implements OnDestroy, OnChanges {
3333
readonly _stateChanges = new Subject<SimpleChanges>();
3434

3535
/** Stream that emits true/false when openAll/closeAll is triggered. */
36-
readonly _openCloseAllActions: Subject<boolean> = new Subject<boolean>();
36+
readonly _openCloseAllActions = new Subject<boolean>();
3737

3838
/** A readonly id value to use for unique selection coordination. */
3939
readonly id = `cdk-accordion-${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.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class CdkCopyToClipboard implements OnDestroy {
5454
* Emits when some text is copied to the clipboard. The
5555
* emitted value indicates whether copying was successful.
5656
*/
57-
@Output('cdkCopyToClipboardCopied') copied = new EventEmitter<boolean>();
57+
@Output('cdkCopyToClipboardCopied') readonly copied = new EventEmitter<boolean>();
5858

5959
/** Copies that are currently being attempted. */
6060
private _pending = new Set<PendingCopy>();

src/cdk/collections/selection-model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class SelectionModel<T> {
3434
}
3535

3636
/** Event emitted when the value has changed. */
37-
changed: Subject<SelectionChange<T>> = new Subject();
37+
readonly changed = new Subject<SelectionChange<T>>();
3838

3939
constructor(
4040
private _multiple = false,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class CdkDragHandle implements OnDestroy {
4141
_parentDrag: {} | undefined;
4242

4343
/** Emits when the state of the handle has changed. */
44-
_stateChanges = new Subject<CdkDragHandle>();
44+
readonly _stateChanges = new Subject<CdkDragHandle>();
4545

4646
/** Whether starting to drag through this handle is disabled. */
4747
@Input('cdkDragHandleDisabled')

0 commit comments

Comments
 (0)