11import { Inject , Injectable , isDevMode , OnDestroy , Optional } from '@angular/core' ;
22import { Params } from '@angular/router' ;
33import { EMPTY , from , Observable , Subject } from 'rxjs' ;
4- import { catchError , concatMap , debounceTime , map , startWith , switchMap , takeUntil , tap } from 'rxjs/operators' ;
4+ import { catchError , concatMap , debounceTime , filter , map , startWith , switchMap , takeUntil , tap } from 'rxjs/operators' ;
55import { isMissing , isPresent , NOP } from '../util' ;
66import { Unpack } from '../types' ;
77import { QueryParamGroup } from '../model/query-param-group' ;
@@ -98,9 +98,13 @@ export class QueryParamGroupService implements OnDestroy {
9898 * Registers a {@link QueryParamNameDirective} directive.
9999 */
100100 public registerQueryParamDirective ( directive : QueryParamNameDirective ) : void {
101- const queryParam : QueryParam < any > = this . queryParamGroup . get ( directive . name ) ;
101+ // Capture the name here, particularly for the queue below to avoid re-evaluating
102+ // it as it might change over time.
103+ const queryParamName = directive . name ;
104+
105+ const queryParam : QueryParam < any > = this . queryParamGroup . get ( queryParamName ) ;
102106 if ( ! queryParam ) {
103- throw new Error ( `Could not find query param with name ${ directive . name } . Did you forget to add it to your QueryParamGroup?` ) ;
107+ throw new Error ( `Could not find query param with name ${ queryParamName } . Did you forget to add it to your QueryParamGroup?` ) ;
104108 }
105109 if ( ! directive . valueAccessor ) {
106110 throw new Error ( `No value accessor found for the form control. Please make sure to implement ControlValueAccessor on this component.` ) ;
@@ -113,14 +117,17 @@ export class QueryParamGroupService implements OnDestroy {
113117 // Proxy updates from the view to debounce them (if needed).
114118 const debouncedQueue$ = new Subject < any > ( ) ;
115119 debouncedQueue$ . pipe (
120+ // Do not synchronize while the param is detached from the group
121+ filter ( ( ) => ! ! this . queryParamGroup . get ( queryParamName ) ) ,
122+
116123 isPresent ( queryParam . debounceTime ) ? debounceTime ( queryParam . debounceTime ) : tap ( ) ,
117124 map ( ( newValue : any ) => this . getParamsForValue ( queryParam , newValue ) ) ,
118125 takeUntil ( this . destroy$ ) ,
119126 ) . subscribe ( params => this . enqueueNavigation ( new NavigationData ( params ) ) ) ;
120127
121128 directive . valueAccessor . registerOnChange ( ( newValue : any ) => debouncedQueue$ . next ( newValue ) ) ;
122129
123- this . directives . set ( directive . name , [ ...( this . directives . get ( directive . name ) || [ ] ) , directive ] ) ;
130+ this . directives . set ( queryParamName , [ ...( this . directives . get ( queryParamName ) || [ ] ) , directive ] ) ;
124131 }
125132
126133 /**
@@ -181,6 +188,10 @@ export class QueryParamGroupService implements OnDestroy {
181188
182189 private setupParamChangeListener ( queryParamName : string ) : void {
183190 const queryParam : QueryParam < any > = this . queryParamGroup . get ( queryParamName ) ;
191+ if ( ! queryParam ) {
192+ throw new Error ( `No param in group found for name ${ queryParamName } ` ) ;
193+ }
194+
184195 queryParam . _registerOnChange ( ( newValue : any ) =>
185196 this . enqueueNavigation ( new NavigationData ( this . getParamsForValue ( queryParam , newValue ) , true ) )
186197 ) ;
0 commit comments