11import { Inject , Injectable , isDevMode , OnDestroy , Optional } from '@angular/core' ;
22import { Params } from '@angular/router' ;
3- import { EMPTY , from , Observable , Subject , zip } from 'rxjs' ;
3+ import { EMPTY , forkJoin , from , Observable , Subject , zip } from 'rxjs' ;
44import {
55 catchError ,
66 concatMap ,
@@ -15,7 +15,7 @@ import {
1515} from 'rxjs/operators' ;
1616import { compareParamMaps , filterParamMap , isMissing , isPresent , NOP } from '../util' ;
1717import { QueryParamGroup } from '../model/query-param-group' ;
18- import { MultiQueryParam , QueryParam , PartitionedQueryParam } from '../model/query-param' ;
18+ import { MultiQueryParam , PartitionedQueryParam , QueryParam } from '../model/query-param' ;
1919import { NGQP_ROUTER_ADAPTER , NGQP_ROUTER_OPTIONS , RouterAdapter , RouterOptions } from '../router-adapter/router-adapter.interface' ;
2020import { QueryParamAccessor } from './query-param-accessor.interface' ;
2121
@@ -226,32 +226,49 @@ export class QueryParamGroupService implements OnDestroy {
226226 return compareParamMaps ( filterParamMap ( previousMap , keys ) , filterParamMap ( currentMap , keys ) ) ;
227227 } ) ,
228228 ) ) ,
229- takeUntil ( this . destroy$ ) ,
230- ) . subscribe ( queryParamMap => {
231- const synthetic = this . isSyntheticNavigation ( ) ;
232- const groupValue : Record < string , unknown > = { } ;
233-
234- Object . keys ( this . getQueryParamGroup ( ) . queryParams ) . forEach ( queryParamName => {
235- const partitionedQueryParam = this . getQueryParamAsPartition ( queryParamName ) ;
236- const newValues = partitionedQueryParam . queryParams . map ( queryParam => isMultiQueryParam < unknown > ( queryParam )
237- ? queryParam . deserializeValue ( queryParamMap . getAll ( queryParam . urlParam ) )
238- : queryParam . deserializeValue ( queryParamMap . get ( queryParam . urlParam ) )
229+ switchMap ( queryParamMap => {
230+ // We need to capture this right here since this is only set during the on-going navigation.
231+ const synthetic = this . isSyntheticNavigation ( ) ;
232+ const queryParamNames = Object . keys ( this . getQueryParamGroup ( ) . queryParams ) ;
233+
234+ return forkJoin < Record < string , unknown > > ( ...queryParamNames
235+ . map ( queryParamName => {
236+ const partitionedQueryParam = this . getQueryParamAsPartition ( queryParamName ) ;
237+
238+ return forkJoin < unknown > ( ...partitionedQueryParam . queryParams
239+ . map ( queryParam => isMultiQueryParam < unknown > ( queryParam )
240+ ? queryParam . deserializeValue ( queryParamMap . getAll ( queryParam . urlParam ) )
241+ : queryParam . deserializeValue ( queryParamMap . get ( queryParam . urlParam ) )
242+ )
243+ ) . pipe (
244+ map ( newValues => partitionedQueryParam . reduce ( newValues ) ) ,
245+ tap ( newValue => {
246+ const directives = this . directives . get ( queryParamName ) ;
247+ if ( directives ) {
248+ directives . forEach ( directive => directive . valueAccessor . writeValue ( newValue ) ) ;
249+ }
250+ } ) ,
251+ map ( newValue => {
252+ return { [ queryParamName ] : newValue } ;
253+ } ) ,
254+ takeUntil ( this . destroy$ ) ,
255+ ) ;
256+ } )
257+ ) . pipe (
258+ map ( ( values : Record < string , unknown > [ ] ) => values . reduce ( ( groupValue , value ) => {
259+ return {
260+ ...groupValue ,
261+ ...value ,
262+ } ;
263+ } , { } ) ) ,
264+ tap ( groupValue => this . getQueryParamGroup ( ) . setValue ( groupValue , {
265+ emitEvent : ! synthetic ,
266+ emitModelToViewChange : false ,
267+ } ) ) ,
239268 ) ;
240- const newValue = partitionedQueryParam . reduce ( newValues ) ;
241-
242- const directives = this . directives . get ( queryParamName ) ;
243- if ( directives ) {
244- directives . forEach ( directive => directive . valueAccessor . writeValue ( newValue ) ) ;
245- }
246-
247- groupValue [ queryParamName ] = newValue ;
248- } ) ;
249-
250- this . getQueryParamGroup ( ) . setValue ( groupValue , {
251- emitEvent : ! synthetic ,
252- emitModelToViewChange : false ,
253- } ) ;
254- } ) ;
269+ } ) ,
270+ takeUntil ( this . destroy$ ) ,
271+ ) . subscribe ( ) ;
255272 }
256273
257274 /** Listens for newly added parameters and starts synchronization for them. */
@@ -350,7 +367,7 @@ export class QueryParamGroupService implements OnDestroy {
350367
351368 return {
352369 ...( this . globalRouterOptions || { } ) ,
353- ...groupOptions ,
370+ ...( groupOptions || { } ) ,
354371 } ;
355372 }
356373
0 commit comments