Skip to content

Commit 7ea505b

Browse files
authored
refactor: replace asObservable usages (#20165)
As discussed, replaces usages of the `asObservable` with a cast to `Observable` since the method is a little pointless, because people can easily get around it if they really want to. Also cleans up a few getters that were returning observables.
1 parent 6569041 commit 7ea505b

Some content is hidden

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

51 files changed

+84
-94
lines changed

src/cdk-experimental/column-resize/column-resize-notifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class ColumnResizeNotifierSource {
5656
@Injectable()
5757
export class ColumnResizeNotifier {
5858
/** Emits whenever a column is resized. */
59-
readonly resizeCompleted: Observable<ColumnSize> = this._source.resizeCompleted.asObservable();
59+
readonly resizeCompleted: Observable<ColumnSize> = this._source.resizeCompleted;
6060

6161
constructor(private readonly _source: ColumnResizeNotifierSource) {}
6262

src/cdk-experimental/dialog/dialog-ref.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ export class DialogRef<T, R = any> {
133133

134134
/** Gets an observable that emits when dialog begins opening. */
135135
beforeOpened(): Observable<void> {
136-
return this._containerInstance._beforeEnter.asObservable();
136+
return this._containerInstance._beforeEnter;
137137
}
138138

139139
/** Gets an observable that emits when dialog is finished opening. */
140140
afterOpened(): Observable<void> {
141-
return this._containerInstance._afterEnter.asObservable();
141+
return this._containerInstance._afterEnter;
142142
}
143143

144144
/** Gets an observable that emits when dialog begins closing. */

src/cdk-experimental/popover-edit/edit-ref.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import {EditEventDispatcher} from './edit-event-dispatcher';
2121
export class EditRef<FormValue> implements OnDestroy {
2222
/** Emits the final value of this edit instance before closing. */
2323
private readonly _finalValueSubject = new Subject<FormValue>();
24-
readonly finalValue: Observable<FormValue> = this._finalValueSubject.asObservable();
24+
readonly finalValue: Observable<FormValue> = this._finalValueSubject;
2525

2626
/** Emits when the user tabs out of this edit lens before closing. */
2727
private readonly _blurredSubject = new Subject<void>();
28-
readonly blurred: Observable<void> = this._blurredSubject.asObservable();
28+
readonly blurred: Observable<void> = this._blurredSubject;
2929

3030
/** The value to set the form back to on revert. */
3131
private _revertFormValue: FormValue;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class FocusEscapeNotifier extends FocusTrap {
4646
}
4747

4848
escapes(): Observable<FocusEscapeNotifierDirection> {
49-
return this._escapeSubject.asObservable();
49+
return this._escapeSubject;
5050
}
5151
}
5252

src/cdk-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class ElementDataSource extends DataSource<PeriodicElement> {
253253

254254
/** Connect function called by the table to retrieve one stream containing the data to render. */
255255
connect() {
256-
return this.data.asObservable();
256+
return this.data;
257257
}
258258

259259
disconnect() {}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export class FocusMonitor implements OnDestroy {
247247
cachedInfo.checkChildren = true;
248248
}
249249

250-
return cachedInfo.subject.asObservable();
250+
return cachedInfo.subject;
251251
}
252252

253253
// Create monitored element info.
@@ -259,7 +259,7 @@ export class FocusMonitor implements OnDestroy {
259259
this._elementInfo.set(nativeElement, info);
260260
this._registerGlobalListeners(info);
261261

262-
return info.subject.asObservable();
262+
return info.subject;
263263
}
264264

265265
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ export class FocusTrap {
341341
if (this._ngZone.isStable) {
342342
fn();
343343
} else {
344-
this._ngZone.onStable.asObservable().pipe(take(1)).subscribe(fn);
344+
this._ngZone.onStable.pipe(take(1)).subscribe(fn);
345345
}
346346
}
347347
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2407,7 +2407,7 @@ describe('CdkDrag', () => {
24072407
const itemInstance = fixture.componentInstance.dragItems.toArray()[1];
24082408
const item = itemInstance.element.nativeElement;
24092409
const spy = jasmine.createSpy('dropped spy');
2410-
const subscription = itemInstance.dropped.asObservable().subscribe(spy);
2410+
const subscription = itemInstance.dropped.subscribe(spy);
24112411

24122412
// Do an initial drag and drop sequence.
24132413
dragElementViaMouse(fixture, item, 50, 50);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
251251
// element to be in the proper place in the DOM. This is mostly relevant
252252
// for draggable elements inside portals since they get stamped out in
253253
// their original DOM position and then they get transferred to the portal.
254-
this._ngZone.onStable.asObservable()
254+
this._ngZone.onStable
255255
.pipe(take(1), takeUntil(this._destroyed))
256256
.subscribe(() => {
257257
this._updateRootElement();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export class DragRef<T = any> {
292292
event: MouseEvent | TouchEvent;
293293
distance: Point;
294294
delta: {x: -1 | 0 | 1, y: -1 | 0 | 1};
295-
}> = this._moveEvents.asObservable();
295+
}> = this._moveEvents;
296296

297297
/** Arbitrary data that can be attached to the drag item. */
298298
data: T;

src/cdk/overlay/overlay-directives.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('Overlay directives', () => {
3939
declarations: [ConnectedOverlayDirectiveTest, ConnectedOverlayPropertyInitOrder],
4040
providers: [{provide: Directionality, useFactory: () => dir = {value: 'ltr'}},
4141
{provide: ScrollDispatcher, useFactory: () => ({
42-
scrolled: () => scrolledSubject.asObservable()
42+
scrolled: () => scrolledSubject
4343
})}
4444
],
4545
});

src/cdk/overlay/overlay-ref.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
127127
// before attempting to position it, as the position may depend on the size of the rendered
128128
// content.
129129
this._ngZone.onStable
130-
.asObservable()
131130
.pipe(take(1))
132131
.subscribe(() => {
133132
// The overlay could've been detached before the zone has stabilized.
@@ -258,27 +257,27 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
258257

259258
/** Gets an observable that emits when the backdrop has been clicked. */
260259
backdropClick(): Observable<MouseEvent> {
261-
return this._backdropClick.asObservable();
260+
return this._backdropClick;
262261
}
263262

264263
/** Gets an observable that emits when the overlay has been attached. */
265264
attachments(): Observable<void> {
266-
return this._attachments.asObservable();
265+
return this._attachments;
267266
}
268267

269268
/** Gets an observable that emits when the overlay has been detached. */
270269
detachments(): Observable<void> {
271-
return this._detachments.asObservable();
270+
return this._detachments;
272271
}
273272

274273
/** Gets an observable of keydown events targeted to this overlay. */
275274
keydownEvents(): Observable<KeyboardEvent> {
276-
return this._keydownEvents.asObservable();
275+
return this._keydownEvents;
277276
}
278277

279278
/** Gets an observable of pointer events targeted outside this overlay. */
280279
outsidePointerEvents(): Observable<MouseEvent> {
281-
return this._outsidePointerEvents.asObservable();
280+
return this._outsidePointerEvents;
282281
}
283282

284283
/** Gets the current overlay configuration, which is immutable. */
@@ -510,7 +509,6 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
510509
// might still be animating. This stream helps us avoid interrupting the animation
511510
// by waiting for the pane to become empty.
512511
const subscription = this._ngZone.onStable
513-
.asObservable()
514512
.pipe(takeUntil(merge(this._attachments, this._detachments)))
515513
.subscribe(() => {
516514
// Needs a couple of checks for the pane and host, because

src/cdk/overlay/position/connected-position-strategy.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ export class ConnectedPositionStrategy implements PositionStrategy {
4949
_preferredPositions: ConnectionPositionPair[] = [];
5050

5151
/** Emits an event when the connection point changes. */
52-
get onPositionChange(): Observable<ConnectedOverlayPositionChange> {
53-
return this._positionStrategy.positionChanges;
54-
}
52+
readonly onPositionChange: Observable<ConnectedOverlayPositionChange>;
5553

5654
constructor(
5755
originPos: OriginConnectionPosition, overlayPos: OverlayConnectionPosition,
@@ -68,6 +66,7 @@ export class ConnectedPositionStrategy implements PositionStrategy {
6866
.withViewportMargin(0);
6967

7068
this.withFallbackPosition(originPos, overlayPos);
69+
this.onPositionChange = this._positionStrategy.positionChanges;
7170
}
7271

7372
/** Ordered list of preferred positions, from most to least desirable. */

src/cdk/overlay/position/flexible-connected-position-strategy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
128128
private _previousPushAmount: {x: number, y: number} | null;
129129

130130
/** Observable sequence of position changes. */
131-
positionChanges: Observable<ConnectedOverlayPositionChange> =
132-
this._positionChanges.asObservable();
131+
positionChanges: Observable<ConnectedOverlayPositionChange> = this._positionChanges;
133132

134133
/** Ordered list of preferred positions, from most to least desirable. */
135134
get positions(): ConnectionPositionPair[] {

src/cdk/overlay/scroll/close-scroll-strategy.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('CloseScrollStrategy', () => {
2525
imports: [OverlayModule, PortalModule, OverlayTestModule],
2626
providers: [
2727
{provide: ScrollDispatcher, useFactory: () => ({
28-
scrolled: () => scrolledSubject.asObservable()
28+
scrolled: () => scrolledSubject
2929
})},
3030
{provide: ViewportRuler, useFactory: () => ({
3131
getViewportScrollPosition: () => ({top: scrollPosition})

src/cdk/overlay/scroll/reposition-scroll-strategy.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('RepositionScrollStrategy', () => {
2323
imports: [OverlayModule, PortalModule, OverlayTestModule],
2424
providers: [
2525
{provide: ScrollDispatcher, useFactory: () => ({
26-
scrolled: () => scrolledSubject.asObservable()
26+
scrolled: () => scrolledSubject
2727
})}
2828
]
2929
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
103103
@ViewChild('contentWrapper', {static: true}) _contentWrapper: ElementRef<HTMLElement>;
104104

105105
/** A stream that emits whenever the rendered range changes. */
106-
renderedRangeStream: Observable<ListRange> = this._renderedRangeSubject.asObservable();
106+
renderedRangeStream: Observable<ListRange> = this._renderedRangeSubject;
107107

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

src/cdk/testing/testbed/task-state-zone-interceptor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class TaskStateZoneInterceptor {
3535
this._lastState ? this._getTaskStateFromInternalZoneState(this._lastState) : {stable: true});
3636

3737
/** Public observable that emits whenever the task state changes. */
38-
readonly state: Observable<TaskState> = this._stateSubject.asObservable();
38+
readonly state: Observable<TaskState> = this._stateSubject;
3939

4040
constructor(private _lastState: HasTaskState|null) {}
4141

src/cdk/text-field/autofill.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class AutofillMonitor implements OnDestroy {
7575
const info = this._monitoredElements.get(element);
7676

7777
if (info) {
78-
return info.subject.asObservable();
78+
return info.subject;
7979
}
8080

8181
const result = new Subject<AutofillEvent>();
@@ -107,7 +107,7 @@ export class AutofillMonitor implements OnDestroy {
107107
}
108108
});
109109

110-
return result.asObservable();
110+
return result;
111111
}
112112

113113
/**

src/material-experimental/column-resize/column-resize.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class ElementDataSource extends DataSource<PeriodicElement> {
300300

301301
/** Connect function called by the table to retrieve one stream containing the data to render. */
302302
connect() {
303-
return this.data.asObservable();
303+
return this.data;
304304
}
305305

306306
disconnect() {}

src/material-experimental/mdc-chips/chip-option.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ export class MatChipOption extends MatChip implements AfterContentInit {
197197
// that moves focus not the next item. To work around the issue, we defer marking the chip
198198
// as not focused until the next time the zone stabilizes.
199199
this._ngZone.onStable
200-
.asObservable()
201200
.pipe(take(1))
202201
.subscribe(() => {
203202
this._ngZone.run(() => {

src/material-experimental/mdc-form-field/form-field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
481481
// Note that we have to run outside of the `NgZone` explicitly, in order to avoid
482482
// throwing users into an infinite loop if `zone-patch-rxjs` is included.
483483
this._ngZone.runOutsideAngular(() => {
484-
this._ngZone.onStable.asObservable().pipe(takeUntil(this._destroyed)).subscribe(() => {
484+
this._ngZone.onStable.pipe(takeUntil(this._destroyed)).subscribe(() => {
485485
if (this._needsOutlineLabelOffsetUpdateOnStable) {
486486
this._needsOutlineLabelOffsetUpdateOnStable = false;
487487
this._updateOutlineLabelOffset();

src/material-experimental/mdc-snack-bar/snack-bar-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export class MatSnackBarContainer extends BasePortalOutlet
147147
exit(): Observable<void> {
148148
this._exiting = true;
149149
this._mdcFoundation.close();
150-
return this._onExit.asObservable();
150+
return this._onExit;
151151
}
152152

153153
/** Attach a component portal as content to this snack bar container. */

src/material-experimental/mdc-tabs/tab-header.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('MDC-based MatTabHeader', () => {
4444
],
4545
providers: [
4646
ViewportRuler,
47-
{provide: Directionality, useFactory: () => ({value: dir, change: change.asObservable()})},
47+
{provide: Directionality, useFactory: () => ({value: dir, change: change})},
4848
]
4949
});
5050

src/material-experimental/mdc-tabs/tab-nav-bar/tab-nav-bar.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('MDC-based MatTabNavBar', () => {
3131
providers: [
3232
{provide: MAT_RIPPLE_GLOBAL_OPTIONS, useFactory: () => globalRippleOptions},
3333
{provide: Directionality, useFactory: () =>
34-
({value: dir, change: dirChange.asObservable()})},
34+
({value: dir, change: dirChange})},
3535
]
3636
});
3737

src/material-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class ElementDataSource extends DataSource<PeriodicElement> {
174174

175175
/** Connect function called by the table to retrieve one stream containing the data to render. */
176176
connect() {
177-
return this.data.asObservable();
177+
return this.data;
178178
}
179179

180180
disconnect() {}

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
322322
// If there are any subscribers before `ngAfterViewInit`, the `autocomplete` will be undefined.
323323
// Return a stream that we'll replace with the real one once everything is in place.
324324
return this._zone.onStable
325-
.asObservable()
326325
.pipe(take(1), switchMap(() => this.optionSelections));
327326
}) as Observable<MatOptionSelectionChange>;
328327

@@ -472,7 +471,7 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
472471
* stream every time the option list changes.
473472
*/
474473
private _subscribeToClosingActions(): Subscription {
475-
const firstStable = this._zone.onStable.asObservable().pipe(take(1));
474+
const firstStable = this._zone.onStable.pipe(take(1));
476475
const optionChanges = this.autocomplete.options.changes.pipe(
477476
tap(() => this._positionStrategy.reapplyLastPosition()),
478477
// Defer emitting to the stream until the next tick, because changing

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ describe('MatAutocomplete', () => {
15591559
let spacer = document.createElement('div');
15601560
let fixture = createComponent(SimpleAutocomplete, [{
15611561
provide: ScrollDispatcher,
1562-
useValue: {scrolled: () => scrolledSubject.asObservable()}
1562+
useValue: {scrolled: () => scrolledSubject}
15631563
}]);
15641564

15651565
fixture.detectChanges();
@@ -2242,7 +2242,7 @@ describe('MatAutocomplete', () => {
22422242
const fixture = createComponent(SimpleAutocomplete, [
22432243
{
22442244
provide: ScrollDispatcher,
2245-
useValue: {scrolled: () => scrolledSubject.asObservable()}
2245+
useValue: {scrolled: () => scrolledSubject}
22462246
},
22472247
{
22482248
provide: MAT_AUTOCOMPLETE_SCROLL_STRATEGY,

src/material/bottom-sheet/bottom-sheet-ref.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ export class MatBottomSheetRef<T = any, R = any> {
115115

116116
/** Gets an observable that is notified when the bottom sheet is finished closing. */
117117
afterDismissed(): Observable<R | undefined> {
118-
return this._afterDismissed.asObservable();
118+
return this._afterDismissed;
119119
}
120120

121121
/** Gets an observable that is notified when the bottom sheet has opened and appeared. */
122122
afterOpened(): Observable<void> {
123-
return this._afterOpened.asObservable();
123+
return this._afterOpened;
124124
}
125125

126126
/**

src/material/chips/chip.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,6 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
424424
// that moves focus not the next item. To work around the issue, we defer marking the chip
425425
// as not focused until the next time the zone stabilizes.
426426
this._ngZone.onStable
427-
.asObservable()
428427
.pipe(take(1))
429428
.subscribe(() => {
430429
this._ngZone.run(() => {

src/material/core/datetime/date-adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ export const MAT_DATE_LOCALE_PROVIDER = {provide: MAT_DATE_LOCALE, useExisting:
3232
export abstract class DateAdapter<D> {
3333
/** The locale to use for all dates. */
3434
protected locale: any;
35+
protected _localeChanges = new Subject<void>();
3536

3637
/** A stream that emits when the locale changes. */
37-
get localeChanges(): Observable<void> { return this._localeChanges; }
38-
protected _localeChanges = new Subject<void>();
38+
readonly localeChanges: Observable<void> = this._localeChanges;
3939

4040
/**
4141
* Gets the year component of the given date.

src/material/datepicker/calendar-body.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
193193
/** Focuses the active cell after the microtask queue is empty. */
194194
_focusActiveCell(movePreview = true) {
195195
this._ngZone.runOutsideAngular(() => {
196-
this._ngZone.onStable.asObservable().pipe(take(1)).subscribe(() => {
196+
this._ngZone.onStable.pipe(take(1)).subscribe(() => {
197197
const activeCell: HTMLElement | null =
198198
this._elementRef.nativeElement.querySelector('.mat-calendar-body-active');
199199

0 commit comments

Comments
 (0)