Skip to content

Commit 350479e

Browse files
committed
addressed some comments
1 parent 446f9e2 commit 350479e

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

src/lib/core/style/add-focus-classes.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Directive, Injectable, Optional, SkipSelf} from '@angular/core';
33

44
/** Singleton that allows all instances of CdkAddFocusClasses to share document event listeners. */
55
@Injectable()
6-
export class CdkFocusCauseDetector {
6+
export class FocusOriginMonitor {
77
/** Whether a keydown event has just occurred. */
88
get keydownOccurred() { return this._keydownOccurred; }
99
private _keydownOccurred = false;
@@ -12,14 +12,16 @@ export class CdkFocusCauseDetector {
1212
private _mousedownOccurred = false;
1313

1414
constructor() {
15+
// Listen to keydown and mousedown in the capture phase so we can detect them even if the user
16+
// stops propagation.
1517
document.addEventListener('keydown', () => {
1618
this._keydownOccurred = true;
17-
setTimeout(() => this._keydownOccurred = false, 0);
19+
Promise.resolve().then(() => this._keydownOccurred = false);
1820
}, true);
1921

2022
document.addEventListener('mousedown', () => {
2123
this._mousedownOccurred = true;
22-
setTimeout(() => this._mousedownOccurred = false, 0);
24+
Promise.resolve().then(() => this._mousedownOccurred = false);
2325
}, true);
2426
}
2527
}
@@ -50,7 +52,7 @@ export class CdkAddFocusClasses {
5052
/** Whether the has been programmatically focused. */
5153
programmaticallyFocused = false;
5254

53-
constructor(private _focusCauseDetector: CdkFocusCauseDetector) {}
55+
constructor(private _focusCauseDetector: FocusOriginMonitor) {}
5456

5557
/** Handles focus event on the element. */
5658
_onFocus() {
@@ -67,14 +69,14 @@ export class CdkAddFocusClasses {
6769

6870

6971
export function FOCUS_CAUSE_DETECTOR_PROVIDER_FACTORY(
70-
parentDispatcher: CdkFocusCauseDetector) {
71-
return parentDispatcher || new CdkFocusCauseDetector();
72+
parentDispatcher: FocusOriginMonitor) {
73+
return parentDispatcher || new FocusOriginMonitor();
7274
}
7375

7476

7577
export const FOCUS_CAUSE_DETECTOR_PROVIDER = {
76-
// If there is already a CdkFocusCauseDetector available, use that. Otherwise, provide a new one.
77-
provide: CdkFocusCauseDetector,
78-
deps: [[new Optional(), new SkipSelf(), CdkFocusCauseDetector]],
78+
// If there is already a FocusOriginMonitor available, use that. Otherwise, provide a new one.
79+
provide: FocusOriginMonitor,
80+
deps: [[new Optional(), new SkipSelf(), FocusOriginMonitor]],
7981
useFactory: FOCUS_CAUSE_DETECTOR_PROVIDER_FACTORY
8082
};

src/lib/core/style/perf_results

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
results using code in schedule_async_perf.html and timestamp_perf.html,
2+
just holding down the 'd' key:
3+
4+
schedule async results:
5+
3501.774999999998
6+
3030.1300000000047
7+
3032.8799999999974
8+
3024.5250000000087
9+
3043.399999999994
10+
3040.6100000000006
11+
3035.854999999996
12+
3033.0149999999994
13+
3037.0150000000067
14+
3037.554999999993
15+
16+
timestamp results:
17+
3509.304999999993
18+
3042.4100000000035
19+
3034.8600000000006
20+
3032.175000000003
21+
3030.3900000000067
22+
3030.520000000004
23+
3038.3300000000017
24+
3035.9850000000006
25+
3033.899999999994
26+
3041.965000000011
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
<head>
3+
<title>Schedule Async</title>
4+
</head>
5+
<body>
6+
<script>
7+
const NUM_EVENTS = 100;
8+
let count = 0;
9+
let start = 0;
10+
let x = false;
11+
console.log('schedule async results:');
12+
document.addEventListener('keydown', () => {
13+
if (count == 0) {
14+
start = performance.now();
15+
} else if (count == NUM_EVENTS) {
16+
console.log(performance.now() - start);
17+
count = 0;
18+
return;
19+
}
20+
count++;
21+
x = true;
22+
Promise.resolve().then(() => x = false);
23+
}, true);
24+
</script>
25+
</body>
26+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<html>
2+
<head>
3+
<title>Timestamp</title>
4+
</head>
5+
<body>
6+
<script>
7+
const NUM_EVENTS = 100;
8+
let count = 0;
9+
let start = 0;
10+
let x = 0;
11+
console.log('timestamp results:');
12+
document.addEventListener('keydown', () => {
13+
if (count == 0) {
14+
start = performance.now();
15+
} else if (count == NUM_EVENTS) {
16+
console.log(performance.now() - start);
17+
count = 0;
18+
return;
19+
}
20+
count++;
21+
x = Date.now();
22+
}, true);
23+
</script>
24+
</body>
25+
</html>

0 commit comments

Comments
 (0)