Skip to content

Commit de700f1

Browse files
committed
refactor: use delay instead of switchMap
1 parent 0d2885f commit de700f1

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

src/cdk/rxjs/rx-operators.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {startWith as startWithOperator} from 'rxjs/operator/startWith';
2222
import {debounceTime as debounceTimeOperator} from 'rxjs/operator/debounceTime';
2323
import {auditTime as auditTimeOperator} from 'rxjs/operator/auditTime';
2424
import {takeUntil as takeUntilOperator} from 'rxjs/operator/takeUntil';
25+
import {delay as delayOperator} from 'rxjs/operator/delay';
2526

2627
/**
2728
* Represents a strongly-typed chain of RxJS operators.
@@ -71,6 +72,9 @@ export interface StrictRxChain<T> {
7172

7273
call(operator: takeUntilOperatorType<T>, notifier: Observable<any>): StrictRxChain<T>;
7374

75+
call(operator: delayOperatorType<T>, delay: number | Date, scheduler?: IScheduler):
76+
StrictRxChain<T>;
77+
7478
subscribe(fn: (t: T) => void): Subscription;
7579

7680
result(): Observable<T>;
@@ -89,6 +93,7 @@ export class StartWithBrand { private _; }
8993
export class DebounceTimeBrand { private _; }
9094
export class AuditTimeBrand { private _; }
9195
export class TakeUntilBrand { private _; }
96+
export class DelayBrand { private _; }
9297

9398

9499
export type finallyOperatorType<T> = typeof _finallyOperator & FinallyBrand;
@@ -103,6 +108,7 @@ export type startWithOperatorType<T> = typeof startWithOperator & StartWithBrand
103108
export type debounceTimeOperatorType<T> = typeof debounceTimeOperator & DebounceTimeBrand;
104109
export type auditTimeOperatorType<T> = typeof auditTimeOperator & AuditTimeBrand;
105110
export type takeUntilOperatorType<T> = typeof takeUntilOperator & TakeUntilBrand;
111+
export type delayOperatorType<T> = typeof delayOperator & DelayBrand;
106112

107113
// We add `Function` to the type intersection to make this nomically different from
108114
// `finallyOperatorType` while still being structurally the same. Without this, TypeScript tries to
@@ -122,3 +128,4 @@ export const debounceTime =
122128
debounceTimeOperator as typeof debounceTimeOperator & DebounceTimeBrand & Function;
123129
export const auditTime = auditTimeOperator as typeof auditTimeOperator & AuditTimeBrand & Function;
124130
export const takeUntil = takeUntilOperator as typeof takeUntilOperator & TakeUntilBrand & Function;
131+
export const delay = delayOperator as typeof delayOperator & DelayBrand & Function;

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
2424
import {DOCUMENT} from '@angular/platform-browser';
2525
import {Directionality} from '@angular/cdk/bidi';
26-
import {filter, first, RxChain, switchMap} from '@angular/cdk/rxjs';
26+
import {filter, first, RxChain, switchMap, delay, doOperator} from '@angular/cdk/rxjs';
2727
import {
2828
ConnectedPositionStrategy,
2929
Overlay,
@@ -364,9 +364,8 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
364364
this.autocomplete._setScrollTop(optionOffset);
365365
} else if (optionOffset + AUTOCOMPLETE_OPTION_HEIGHT > panelTop + AUTOCOMPLETE_PANEL_HEIGHT) {
366366
// Scroll down to reveal selected option scrolled below the panel bottom
367-
const newScrollTop =
368-
Math.max(0, optionOffset - AUTOCOMPLETE_PANEL_HEIGHT + AUTOCOMPLETE_OPTION_HEIGHT);
369-
this.autocomplete._setScrollTop(newScrollTop);
367+
const newScrollTop = optionOffset - AUTOCOMPLETE_PANEL_HEIGHT + AUTOCOMPLETE_OPTION_HEIGHT;
368+
this.autocomplete._setScrollTop(Math.max(0, newScrollTop));
370369
}
371370
}
372371

@@ -376,13 +375,12 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
376375
*/
377376
private _subscribeToClosingActions(): Subscription {
378377
const firstStable = first.call(this._zone.onStable);
379-
const optionChanges = switchMap.call(this.autocomplete.options.changes, () => {
380-
this._positionStrategy.recalculateLastPosition();
381-
378+
const optionChanges = RxChain.from(this.autocomplete.options.changes)
379+
.call(doOperator, () => this._positionStrategy.recalculateLastPosition())
382380
// Defer emitting to the stream until the next tick, because changing
383381
// bindings in here will cause "changed after checked" errors.
384-
return Promise.resolve();
385-
});
382+
.call(delay, 0)
383+
.result();
386384

387385
// When the zone is stable initially, and when the option list changes...
388386
return RxChain.from(merge(firstStable, optionChanges))

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ describe('MdAutocomplete', () => {
495495
tick();
496496

497497
fixture.detectChanges();
498+
tick();
499+
498500
expect(input.value).toEqual('', `Expected input value to be empty after reset.`);
499501
}));
500502

@@ -1352,7 +1354,9 @@ describe('MdAutocomplete', () => {
13521354
fixture.detectChanges();
13531355
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focusin');
13541356
tick(1000);
1357+
13551358
fixture.detectChanges();
1359+
tick();
13561360

13571361
Promise.resolve().then(() => {
13581362
let panel = overlayContainerElement.querySelector('.mat-autocomplete-panel') as HTMLElement;

tools/package-tools/rollup-globals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const rollupGlobals = {
7171
'rxjs/operator/switchMap': 'Rx.Observable.prototype',
7272
'rxjs/operator/takeUntil': 'Rx.Observable.prototype',
7373
'rxjs/operator/toPromise': 'Rx.Observable.prototype',
74+
'rxjs/operator/delay': 'Rx.Observable.prototype',
7475

7576
'rxjs/add/observable/merge': 'Rx.Observable',
7677
'rxjs/add/observable/fromEvent': 'Rx.Observable',

0 commit comments

Comments
 (0)