@@ -10,8 +10,8 @@ import {ALT, CONTROL, META, SHIFT} from '@angular/cdk/keycodes';
10
10
import { Inject , Injectable , InjectionToken , OnDestroy , Optional , NgZone } from '@angular/core' ;
11
11
import { normalizePassiveListenerOptions , Platform } from '@angular/cdk/platform' ;
12
12
import { DOCUMENT } from '@angular/common' ;
13
- import { Observable , ReplaySubject } from 'rxjs' ;
14
- import { distinctUntilChanged } from 'rxjs/operators' ;
13
+ import { BehaviorSubject , Observable } from 'rxjs' ;
14
+ import { distinctUntilChanged , skip } from 'rxjs/operators' ;
15
15
import {
16
16
isFakeMousedownFromScreenReader ,
17
17
isFakeTouchstartFromScreenReader ,
@@ -91,14 +91,11 @@ export class InputModalityDetector implements OnDestroy {
91
91
92
92
/** Returns the most recently detected input modality. */
93
93
get inputModality ( ) : InputModality {
94
- return this . _inputModality ;
94
+ return this . _inputModality . value ;
95
95
}
96
96
97
- /** The underlying ReplaySubject that emits input modality changes. */
98
- private readonly _change = new ReplaySubject < InputModality > ( 1 ) ;
99
-
100
- /** The current / last detected input modality by this service. */
101
- private _inputModality : InputModality = null ;
97
+ /** The underlying BehaviorSubject that emits whenever an input modality is detected. */
98
+ private readonly _inputModality = new BehaviorSubject < InputModality > ( null ) ;
102
99
103
100
/** Options for this InputModalityDetector. */
104
101
private readonly _options : InputModalityDetectorOptions ;
@@ -118,7 +115,7 @@ export class InputModalityDetector implements OnDestroy {
118
115
// modality to keyboard.
119
116
if ( this . _options ?. ignoreKeys ?. some ( keyCode => keyCode === event . keyCode ) ) { return ; }
120
117
121
- this . _updateInputModality ( 'keyboard' ) ;
118
+ this . _inputModality . next ( 'keyboard' ) ;
122
119
}
123
120
124
121
/**
@@ -133,7 +130,7 @@ export class InputModalityDetector implements OnDestroy {
133
130
// after the previous touch event.
134
131
if ( Date . now ( ) - this . _lastTouchMs < TOUCH_BUFFER_MS ) { return ; }
135
132
136
- this . _updateInputModality ( 'mouse' ) ;
133
+ this . _inputModality . next ( 'mouse' ) ;
137
134
}
138
135
139
136
/**
@@ -147,27 +144,27 @@ export class InputModalityDetector implements OnDestroy {
147
144
// triggered via mouse vs touch.
148
145
this . _lastTouchMs = Date . now ( ) ;
149
146
150
- this . _updateInputModality ( 'touch' ) ;
147
+ this . _inputModality . next ( 'touch' ) ;
151
148
}
152
149
153
150
constructor (
154
- private readonly _platform : Platform ,
155
- ngZone : NgZone ,
156
- @Inject ( DOCUMENT ) document : Document ,
157
- @Optional ( )
158
- @Inject ( INPUT_MODALITY_DETECTOR_OPTIONS )
159
- options ?: InputModalityDetectorOptions ,
151
+ private readonly _platform : Platform ,
152
+ ngZone : NgZone ,
153
+ @Inject ( DOCUMENT ) document : Document ,
154
+ @Optional ( ) @Inject ( INPUT_MODALITY_DETECTOR_OPTIONS )
155
+ options ?: InputModalityDetectorOptions ,
160
156
) {
161
- // If we're not in a browser, this service should do nothing, as there's no relevant input
162
- // modality to detect.
163
- if ( ! _platform . isBrowser ) { return ; }
164
-
165
157
this . _options = {
166
158
...INPUT_MODALITY_DETECTOR_DEFAULT_OPTIONS ,
167
159
...options ,
168
160
} ;
169
161
170
- this . inputModalityChange = this . _change . pipe ( distinctUntilChanged ( ) ) ;
162
+ // Only emit if the input modality changes, and skip the first emission as it's null.
163
+ this . inputModalityChange = this . _inputModality . pipe ( distinctUntilChanged ( ) , skip ( 1 ) ) ;
164
+
165
+ // If we're not in a browser, this service should do nothing, as there's no relevant input
166
+ // modality to detect.
167
+ if ( ! _platform . isBrowser ) { return ; }
171
168
172
169
// Add the event listeners used to detect the user's input modality.
173
170
ngZone . runOutsideAngular ( ( ) => {
@@ -184,13 +181,4 @@ export class InputModalityDetector implements OnDestroy {
184
181
document . removeEventListener ( 'mousedown' , this . _onMousedown , modalityEventListenerOptions ) ;
185
182
document . removeEventListener ( 'touchstart' , this . _onTouchstart , modalityEventListenerOptions ) ;
186
183
}
187
-
188
- /**
189
- * Update the input modality detected by the service.
190
- * @param inputModality The newly detected input modality.
191
- */
192
- private _updateInputModality ( inputModality : InputModality ) : void {
193
- this . _change . next ( inputModality ) ;
194
- this . _inputModality = inputModality ;
195
- }
196
184
}
0 commit comments