Skip to content

Commit 1add8a8

Browse files
committed
fix(core): Do not synchronize detached parameters
While a parameter is detached from its group, do not synchronize it with the URL; however, *do* synchronize it once it is added back again. relates #44 Signed-off-by: Ingo Bürk <ingo.buerk@tngtech.com>
1 parent 985a96b commit 1add8a8

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

projects/ngqp-demo/src/app/docs-items/examples/add-remove-parameter/add-remove-parameter-example.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</button>
1515
</div>
1616

17-
<input type="text" autocomplete="off" class="form-control" [queryParamName]="hasParam ? 'param' : null" />
17+
<input type="text" autocomplete="off" class="form-control" queryParamName="param" />
1818
</div>
1919
</ng-container>
2020
</demo-browser>

projects/ngqp/core/src/lib/directives/query-param-group.service.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Inject, Injectable, isDevMode, OnDestroy, Optional } from '@angular/core';
22
import { Params } from '@angular/router';
33
import { 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';
55
import { isMissing, isPresent, NOP } from '../util';
66
import { Unpack } from '../types';
77
import { 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

Comments
 (0)