Skip to content

refactor(input): run autofill monitor outside the NgZone #10367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/cdk/text-field/autofill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
Injectable,
OnDestroy,
OnInit,
Output
Output,
NgZone,
} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {empty as observableEmpty} from 'rxjs/observable/empty';
Expand Down Expand Up @@ -50,7 +51,7 @@ const listenerOptions: any = supportsPassiveEventListeners() ? {passive: true} :
export class AutofillMonitor implements OnDestroy {
private _monitoredElements = new Map<Element, MonitoredElementInfo>();

constructor(private _platform: Platform) {}
constructor(private _platform: Platform, private _ngZone: NgZone) {}

/**
* Monitor for changes in the autofill state of the given input element.
Expand All @@ -63,6 +64,7 @@ export class AutofillMonitor implements OnDestroy {
}

const info = this._monitoredElements.get(element);

if (info) {
return info.subject.asObservable();
}
Expand All @@ -78,8 +80,10 @@ export class AutofillMonitor implements OnDestroy {
}
};

element.addEventListener('animationstart', listener, listenerOptions);
element.classList.add('cdk-text-field-autofill-monitored');
this._ngZone.runOutsideAngular(() => {
element.addEventListener('animationstart', listener, listenerOptions);
element.classList.add('cdk-text-field-autofill-monitored');
});

this._monitoredElements.set(element, {
subject: result,
Expand Down Expand Up @@ -118,6 +122,7 @@ export class AutofillMonitor implements OnDestroy {
selector: '[cdkAutofill]',
})
export class CdkAutofill implements OnDestroy, OnInit {
/** Emits when the autofill state of the element changes. */
@Output() cdkAutofill: EventEmitter<AutofillEvent> = new EventEmitter<AutofillEvent>();

constructor(private _elementRef: ElementRef, private _autofillMonitor: AutofillMonitor) {}
Expand Down