Skip to content

Commit e318de6

Browse files
crisbetojelbourn
authored andcommitted
refactor: initialize to empty subscriptions (#6769)
Initializes most subscriptions to empty ones, avoiding the need to null-check them before unsubscribing.
1 parent f375f92 commit e318de6

File tree

13 files changed

+50
-112
lines changed

13 files changed

+50
-112
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
2929
private _activeItem: T;
3030
private _wrap = false;
3131
private _letterKeyStream = new Subject<string>();
32-
private _typeaheadSubscription: Subscription;
32+
private _typeaheadSubscription = Subscription.EMPTY;
3333

3434
// Buffer for the letters that the user has pressed when the typeahead option is turned on.
3535
private _pressedLetters: string[] = [];
@@ -60,9 +60,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
6060
throw Error('ListKeyManager items in typeahead mode must implement the `getLabel` method.');
6161
}
6262

63-
if (this._typeaheadSubscription) {
64-
this._typeaheadSubscription.unsubscribe();
65-
}
63+
this._typeaheadSubscription.unsubscribe();
6664

6765
// Debounce the presses of non-navigational keys, collect the ones that correspond to letters
6866
// and convert those letters back into a string. Afterwards find the first item that starts

src/cdk/overlay/overlay-directives.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ export class ConnectedOverlayDirective implements OnDestroy, OnChanges {
9595
private _overlayRef: OverlayRef;
9696
private _templatePortal: TemplatePortal<any>;
9797
private _hasBackdrop = false;
98-
private _backdropSubscription: Subscription | null;
99-
private _positionSubscription: Subscription;
98+
private _backdropSubscription = Subscription.EMPTY;
99+
private _positionSubscription = Subscription.EMPTY;
100100
private _offsetX: number = 0;
101101
private _offsetY: number = 0;
102102
private _position: ConnectedPositionStrategy;
103-
private _escapeListener: Function;
103+
private _escapeListener = () => {};
104104

105105
/** Origin for the connected overlay. */
106106
@Input('cdkConnectedOverlayOrigin') origin: OverlayOrigin;
@@ -360,14 +360,8 @@ export class ConnectedOverlayDirective implements OnDestroy, OnChanges {
360360
this.detach.emit();
361361
}
362362

363-
if (this._backdropSubscription) {
364-
this._backdropSubscription.unsubscribe();
365-
this._backdropSubscription = null;
366-
}
367-
368-
if (this._escapeListener) {
369-
this._escapeListener();
370-
}
363+
this._backdropSubscription.unsubscribe();
364+
this._escapeListener();
371365
}
372366

373367
/** Destroys the overlay created by this directive. */
@@ -376,17 +370,9 @@ export class ConnectedOverlayDirective implements OnDestroy, OnChanges {
376370
this._overlayRef.dispose();
377371
}
378372

379-
if (this._backdropSubscription) {
380-
this._backdropSubscription.unsubscribe();
381-
}
382-
383-
if (this._positionSubscription) {
384-
this._positionSubscription.unsubscribe();
385-
}
386-
387-
if (this._escapeListener) {
388-
this._escapeListener();
389-
}
373+
this._backdropSubscription.unsubscribe();
374+
this._positionSubscription.unsubscribe();
375+
this._escapeListener();
390376
}
391377

392378
/** Sets the event listener that closes the overlay when pressing Escape. */

src/lib/chips/chip-list.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
6666
protected _chipSet: WeakMap<MdChip, boolean> = new WeakMap();
6767

6868
/** Subscription to tabbing out from the chip list. */
69-
private _tabOutSubscription: Subscription;
69+
private _tabOutSubscription = Subscription.EMPTY;
7070

7171
/** Whether or not the chip is selectable. */
7272
protected _selectable: boolean = true;
@@ -126,9 +126,7 @@ export class MdChipList implements AfterContentInit, OnDestroy {
126126
}
127127

128128
ngOnDestroy(): void {
129-
if (this._tabOutSubscription) {
130-
this._tabOutSubscription.unsubscribe();
131-
}
129+
this._tabOutSubscription.unsubscribe();
132130
}
133131

134132
/**

src/lib/datepicker/datepicker-input.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
171171

172172
private _validatorOnChange = () => {};
173173

174-
private _datepickerSubscription: Subscription;
174+
private _datepickerSubscription = Subscription.EMPTY;
175175

176176
/** The form control validator for whether the input parses. */
177177
private _parseValidator: ValidatorFn = (): ValidationErrors | null => {
@@ -235,9 +235,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
235235
}
236236

237237
ngOnDestroy() {
238-
if (this._datepickerSubscription) {
239-
this._datepickerSubscription.unsubscribe();
240-
}
238+
this._datepickerSubscription.unsubscribe();
241239
}
242240

243241
registerOnValidatorChange(fn: () => void): void {

src/lib/datepicker/datepicker.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class MdDatepicker<D> implements OnDestroy {
200200
/** The element that was focused before the datepicker was opened. */
201201
private _focusedElementBeforeOpen: HTMLElement | null = null;
202202

203-
private _inputSubscription: Subscription;
203+
private _inputSubscription = Subscription.EMPTY;
204204

205205
constructor(private _dialog: MdDialog,
206206
private _overlay: Overlay,
@@ -217,12 +217,11 @@ export class MdDatepicker<D> implements OnDestroy {
217217

218218
ngOnDestroy() {
219219
this.close();
220+
this._inputSubscription.unsubscribe();
221+
220222
if (this._popupRef) {
221223
this._popupRef.dispose();
222224
}
223-
if (this._inputSubscription) {
224-
this._inputSubscription.unsubscribe();
225-
}
226225
}
227226

228227
/** Selects the given date */

src/lib/expansion/expansion-panel-header.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import {Subscription} from 'rxjs/Subscription';
7272
],
7373
})
7474
export class MdExpansionPanelHeader implements OnDestroy {
75-
private _parentChangeSubscription: Subscription | null = null;
75+
private _parentChangeSubscription = Subscription.EMPTY;
7676

7777
constructor(
7878
@Host() public panel: MdExpansionPanel,
@@ -135,11 +135,7 @@ export class MdExpansionPanelHeader implements OnDestroy {
135135
}
136136

137137
ngOnDestroy() {
138-
if (this._parentChangeSubscription) {
139-
this._parentChangeSubscription.unsubscribe();
140-
this._parentChangeSubscription = null;
141-
}
142-
138+
this._parentChangeSubscription.unsubscribe();
143139
this._focusOriginMonitor.stopMonitoring(this._element.nativeElement);
144140
}
145141
}

src/lib/list/selection-list.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ export class MdSelectionList extends _MdSelectionListMixinBase
196196
_tabIndex = 0;
197197

198198
/** Subscription to all list options' onFocus events */
199-
private _optionFocusSubscription: Subscription;
199+
private _optionFocusSubscription = Subscription.EMPTY;
200200

201201
/** Subscription to all list options' destroy events */
202-
private _optionDestroyStream: Subscription;
202+
private _optionDestroyStream = Subscription.EMPTY;
203203

204204
/** The FocusKeyManager which handles focus. */
205205
_keyManager: FocusKeyManager<MdListOption>;
@@ -234,13 +234,8 @@ export class MdSelectionList extends _MdSelectionListMixinBase
234234
}
235235

236236
ngOnDestroy(): void {
237-
if (this._optionDestroyStream) {
238-
this._optionDestroyStream.unsubscribe();
239-
}
240-
241-
if (this._optionFocusSubscription) {
242-
this._optionFocusSubscription.unsubscribe();
243-
}
237+
this._optionDestroyStream.unsubscribe();
238+
this._optionFocusSubscription.unsubscribe();
244239
}
245240

246241
focus() {

src/lib/menu/menu-directive.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
7474
private _previousElevation: string;
7575

7676
/** Subscription to tab events on the menu panel */
77-
private _tabSubscription: Subscription;
77+
private _tabSubscription = Subscription.EMPTY;
7878

7979
/** Config object to be passed into the menu's ngClass */
8080
_classList: any = {};
@@ -150,10 +150,7 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
150150
}
151151

152152
ngOnDestroy() {
153-
if (this._tabSubscription) {
154-
this._tabSubscription.unsubscribe();
155-
}
156-
153+
this._tabSubscription.unsubscribe();
157154
this.close.emit();
158155
this.close.complete();
159156
}

src/lib/menu/menu-trigger.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
8989
private _portal: TemplatePortal<any>;
9090
private _overlayRef: OverlayRef | null = null;
9191
private _menuOpen: boolean = false;
92-
private _closeSubscription: Subscription;
93-
private _positionSubscription: Subscription;
94-
private _hoverSubscription: Subscription;
92+
private _closeSubscription = Subscription.EMPTY;
93+
private _positionSubscription = Subscription.EMPTY;
94+
private _hoverSubscription = Subscription.EMPTY;
9595

9696
// Tracking input type is necessary so it's possible to only auto-focus
9797
// the first item of the list when the menu is opened via the keyboard
@@ -392,13 +392,9 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
392392

393393
/** Cleans up the active subscriptions. */
394394
private _cleanUpSubscriptions(): void {
395-
[
396-
this._closeSubscription,
397-
this._positionSubscription,
398-
this._hoverSubscription
399-
]
400-
.filter(subscription => !!subscription)
401-
.forEach(subscription => subscription.unsubscribe());
395+
this._closeSubscription.unsubscribe();
396+
this._positionSubscription.unsubscribe();
397+
this._hoverSubscription.unsubscribe();
402398
}
403399

404400
/** Returns a stream that emits whenever an action that should close the menu occurs. */

src/lib/select/select.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
205205
private _panelOpen = false;
206206

207207
/** Subscriptions to option events. */
208-
private _optionSubscription: Subscription | null;
208+
private _optionSubscription = Subscription.EMPTY;
209209

210210
/** Subscription to changes in the option list. */
211-
private _changeSubscription: Subscription;
211+
private _changeSubscription = Subscription.EMPTY;
212212

213213
/** Subscription to tab events while overlay is focused. */
214-
private _tabSubscription: Subscription;
214+
private _tabSubscription = Subscription.EMPTY;
215215

216216
/** Whether filling out the select is required in the form. */
217217
private _required: boolean = false;
@@ -466,14 +466,8 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
466466

467467
ngOnDestroy() {
468468
this._dropSubscriptions();
469-
470-
if (this._changeSubscription) {
471-
this._changeSubscription.unsubscribe();
472-
}
473-
474-
if (this._tabSubscription) {
475-
this._tabSubscription.unsubscribe();
476-
}
469+
this._changeSubscription.unsubscribe();
470+
this._tabSubscription.unsubscribe();
477471
}
478472

479473
/** Toggles the overlay panel open or closed. */
@@ -852,10 +846,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
852846

853847
/** Unsubscribes from all option subscriptions. */
854848
private _dropSubscriptions(): void {
855-
if (this._optionSubscription) {
856-
this._optionSubscription.unsubscribe();
857-
this._optionSubscription = null;
858-
}
849+
this._optionSubscription.unsubscribe();
859850
}
860851

861852
/** Emits change event to set the model value. */

0 commit comments

Comments
 (0)