Skip to content

Commit e5ac2e4

Browse files
committed
refactor: use delay instead of switchMap
1 parent 43e163b commit e5ac2e4

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,
@@ -359,9 +359,8 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
359359
this.autocomplete._setScrollTop(optionOffset);
360360
} else if (optionOffset + AUTOCOMPLETE_OPTION_HEIGHT > panelTop + AUTOCOMPLETE_PANEL_HEIGHT) {
361361
// Scroll down to reveal selected option scrolled below the panel bottom
362-
const newScrollTop =
363-
Math.max(0, optionOffset - AUTOCOMPLETE_PANEL_HEIGHT + AUTOCOMPLETE_OPTION_HEIGHT);
364-
this.autocomplete._setScrollTop(newScrollTop);
362+
const newScrollTop = optionOffset - AUTOCOMPLETE_PANEL_HEIGHT + AUTOCOMPLETE_OPTION_HEIGHT;
363+
this.autocomplete._setScrollTop(Math.max(0, newScrollTop));
365364
}
366365
}
367366

@@ -371,13 +370,12 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
371370
*/
372371
private _subscribeToClosingActions(): Subscription {
373372
const firstStable = first.call(this._zone.onStable);
374-
const optionChanges = switchMap.call(this.autocomplete.options.changes, () => {
375-
this._positionStrategy.recalculateLastPosition();
376-
373+
const optionChanges = RxChain.from(this.autocomplete.options.changes)
374+
.call(doOperator, () => this._positionStrategy.recalculateLastPosition())
377375
// Defer emitting to the stream until the next tick, because changing
378376
// bindings in here will cause "changed after checked" errors.
379-
return Promise.resolve();
380-
});
377+
.call(delay, 0)
378+
.result();
381379

382380
// When the zone is stable initially, and when the option list changes...
383381
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
@@ -509,6 +509,8 @@ describe('MdAutocomplete', () => {
509509
tick();
510510

511511
fixture.detectChanges();
512+
tick();
513+
512514
expect(input.value).toEqual('', `Expected input value to be empty after reset.`);
513515
}));
514516

@@ -1439,7 +1441,9 @@ describe('MdAutocomplete', () => {
14391441
fixture.detectChanges();
14401442
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focusin');
14411443
tick(1000);
1444+
14421445
fixture.detectChanges();
1446+
tick();
14431447

14441448
Promise.resolve().then(() => {
14451449
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)