@@ -28,9 +28,8 @@ import {
2828 Observable ,
2929 Subject ,
3030 Subscription ,
31- throwError ,
3231} from 'rxjs' ;
33- import { debounceTime , filter , map , switchMap , take , tap } from 'rxjs/operators' ;
32+ import { debounceTime , filter , map , take , tap } from 'rxjs/operators' ;
3433
3534const DEFAULT_TYPEAHEAD_DEBOUNCE_INTERVAL_MS = 200 ;
3635
@@ -326,44 +325,36 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
326325 private _setTypeAhead ( debounceInterval : number ) {
327326 this . _typeaheadSubscription . unsubscribe ( ) ;
328327
328+ if (
329+ ( typeof ngDevMode === 'undefined' || ngDevMode ) &&
330+ this . _items . length &&
331+ this . _items . some ( item => typeof item . getLabel !== 'function' )
332+ ) {
333+ throw new Error (
334+ 'TreeKeyManager items in typeahead mode must implement the `getLabel` method.' ,
335+ ) ;
336+ }
337+
329338 // Debounce the presses of non-navigational keys, collect the ones that correspond to letters
330339 // and convert those letters back into a string. Afterwards find the first item that starts
331340 // with that string and select it.
332- this . _typeaheadSubscription = this . _getItems ( )
341+ this . _typeaheadSubscription = this . _letterKeyStream
333342 . pipe (
334- switchMap ( items => {
335- if (
336- ( typeof ngDevMode === 'undefined' || ngDevMode ) &&
337- items . length &&
338- items . some ( item => typeof item . getLabel !== 'function' )
339- ) {
340- return throwError (
341- new Error (
342- 'TreeKeyManager items in typeahead mode must implement the `getLabel` method.' ,
343- ) ,
344- ) ;
345- }
346- return observableOf ( items ) as Observable < Array < T & { getLabel ( ) : string } > > ;
347- } ) ,
348- switchMap ( items => {
349- return this . _letterKeyStream . pipe (
350- tap ( letter => this . _pressedLetters . push ( letter ) ) ,
351- debounceTime ( debounceInterval ) ,
352- filter ( ( ) => this . _pressedLetters . length > 0 ) ,
353- map ( ( ) => [ this . _pressedLetters . join ( '' ) , items ] as const ) ,
354- ) ;
355- } ) ,
343+ tap ( letter => this . _pressedLetters . push ( letter ) ) ,
344+ debounceTime ( debounceInterval ) ,
345+ filter ( ( ) => this . _pressedLetters . length > 0 ) ,
346+ map ( ( ) => this . _pressedLetters . join ( '' ) ) ,
356347 )
357- . subscribe ( ( [ inputString , items ] ) => {
348+ . subscribe ( inputString => {
358349 // Start at 1 because we want to start searching at the item immediately
359350 // following the current active item.
360- for ( let i = 1 ; i < items . length + 1 ; i ++ ) {
361- const index = ( this . _activeItemIndex + i ) % items . length ;
362- const item = items [ index ] ;
351+ for ( let i = 1 ; i < this . _items . length + 1 ; i ++ ) {
352+ const index = ( this . _activeItemIndex + i ) % this . _items . length ;
353+ const item = this . _items [ index ] ;
363354
364355 if (
365356 ! this . _skipPredicateFn ( item ) &&
366- item . getLabel ( ) . toUpperCase ( ) . trim ( ) . indexOf ( inputString ) === 0
357+ item . getLabel ?. ( ) . toUpperCase ( ) . trim ( ) . indexOf ( inputString ) === 0
367358 ) {
368359 this . _setActiveItem ( index ) ;
369360 break ;
0 commit comments