Skip to content

Commit 6ade19d

Browse files
committed
fix(core): Added internal documentation
1 parent 2c695a3 commit 6ade19d

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,32 @@ function hasArraySerialization(queryParam: QueryParam<any>, values: string | str
2424
return isMultiQueryParam(queryParam);
2525
}
2626

27-
/** @internal */
27+
/**
28+
* Service implementing the synchronization logic
29+
*
30+
* This service is the key to the synchronization process by binding a {@link QueryParamGroup}
31+
* to the router.
32+
*
33+
* @internal
34+
*/
2835
@Injectable()
2936
export class QueryParamGroupService implements OnDestroy {
3037

38+
/** The {@link QueryParamGroup} to bind. */
3139
private queryParamGroup: QueryParamGroup;
40+
41+
/** List of {@link QueryParamNameDirective} directives registered to this service. */
3242
private directives: QueryParamNameDirective[] = [];
3343

44+
/**
45+
* Queue of navigation parameters
46+
*
47+
* A queue is used for navigations as we need to make sure all parameter changes
48+
* are executed in sequence as otherwise navigations might overwrite each other.
49+
*/
3450
private queue$ = new Subject<Params>();
51+
52+
/** @ignore */
3553
private destroy$ = new Subject<void>();
3654

3755
constructor(
@@ -47,11 +65,23 @@ export class QueryParamGroupService implements OnDestroy {
4765
this.destroy$.complete();
4866
}
4967

68+
/**
69+
* Uses the given {@link QueryParamGroup} for synchronization.
70+
*/
5071
public setQueryParamGroup(queryParamGroup: QueryParamGroup): void {
72+
// FIXME: If this is called when we already have a group, we probably need to do
73+
// some cleanup first.
74+
if (this.queryParamGroup) {
75+
throw new Error(`A QueryParamGroup has already been setup. Changing the group is currently not supported.`);
76+
}
77+
5178
this.queryParamGroup = queryParamGroup;
5279
this.startSynchronization();
5380
}
5481

82+
/**
83+
* Registers a {@link QueryParamNameDirective} directive.
84+
*/
5585
public addQueryParam(directive: QueryParamNameDirective): void {
5686
const queryParam: QueryParam<any> = this.queryParamGroup.get(directive.name);
5787
if (!queryParam) {
@@ -84,6 +114,7 @@ export class QueryParamGroupService implements OnDestroy {
84114
this.setupRouterListener();
85115
}
86116

117+
/** Listens for programmatic changes on group level and synchronizes to the router. */
87118
private setupGroupChangeListener() {
88119
this.queryParamGroup._registerOnChange((value: Record<string, any>) => {
89120
let params: Params = {};
@@ -100,6 +131,7 @@ export class QueryParamGroupService implements OnDestroy {
100131
});
101132
}
102133

134+
/** Listens for programmatic changes on parameter level and synchronizes to the router. */
103135
private setupParamChangeListeners() {
104136
Object.keys(this.queryParamGroup.queryParams).forEach(queryParamName => {
105137
const queryParam: QueryParam<any> = this.queryParamGroup.get(queryParamName);
@@ -109,6 +141,7 @@ export class QueryParamGroupService implements OnDestroy {
109141
});
110142
}
111143

144+
/** Listens for changes in the router and synchronizes to the model. */
112145
private setupRouterListener() {
113146
this.routerAdapter.queryParamMap.subscribe(queryParamMap => {
114147
Object.keys(this.queryParamGroup.queryParams).forEach(queryParamName => {
@@ -132,17 +165,26 @@ export class QueryParamGroupService implements OnDestroy {
132165
});
133166
}
134167

168+
/** Subscribes to the parameter queue and executes navigations in sequence. */
135169
private setupNavigationQueue() {
170+
// FIXME: We should implement some error handling here.
136171
this.queue$.pipe(
137172
takeUntil(this.destroy$),
138173
concatMap(params => this.routerAdapter.navigate(params, this.routerOptions)),
139174
).subscribe();
140175
}
141176

177+
/** Sends a change of parameters to the queue. */
142178
private enqueueNavigation(params: Params): void {
143179
this.queue$.next(params);
144180
}
145181

182+
/**
183+
* Returns the full set of parameters given a value for a parameter model.
184+
*
185+
* This consists mainly of properly serializing the model value and ensuring to take
186+
* side effect changes into account that may have been configured.
187+
*/
146188
private getParamsForValue<T>(queryParam: QueryParam<any>, value: T | undefined | null): Params {
147189
const newValue = this.serialize(queryParam, value);
148190

@@ -171,6 +213,11 @@ export class QueryParamGroupService implements OnDestroy {
171213
}
172214
}
173215

216+
/**
217+
* Returns the current set of options to pass to the router.
218+
*
219+
* This merges the global configuration with the group specific configuration.
220+
*/
174221
private get routerOptions(): RouterOptions {
175222
const groupOptions = this.queryParamGroup ? this.queryParamGroup.routerOptions : {};
176223

0 commit comments

Comments
 (0)