Skip to content

Commit ce945be

Browse files
committed
feat: 🎸 syncdefs
support updating deps in different times
1 parent 577ca0a commit ce945be

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

‎projects/ngneat/bind-query-params/src/lib/BindQueryParamsManager.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class BindQueryParamsManager<T = any> {
1111
private defs: QueryParamDef<T>[];
1212
private group: FormGroup;
1313
private $destroy = new Subject();
14-
private defsSynced = false;
14+
private defsSynced: Record<keyof T, boolean> = {} as Record<keyof T, boolean>;
1515

1616
connect(group: FormGroup) {
1717
this.group = group;
@@ -28,7 +28,7 @@ export class BindQueryParamsManager<T = any> {
2828
}
2929

3030
onInit() {
31-
this.updateControl(this.defs, (def) => def.strategy === 'twoWay');
31+
this.updateControl(this.defs, { emitEvent: true }, (def) => def.strategy === 'twoWay');
3232

3333
const controls = this.defs.map((def) => {
3434
return this.group.get(def.path).valueChanges.pipe(
@@ -79,11 +79,21 @@ export class BindQueryParamsManager<T = any> {
7979
return result;
8080
}
8181

82-
syncDefs(queryKeys: (keyof T & string) | (keyof T & string)[]) {
83-
if (!this.defsSynced) {
84-
const defs = coerceArray(queryKeys).map((key) => this.getDef(key as keyof T));
85-
this.updateControl(defs);
86-
this.defsSynced = true;
82+
syncDefs(
83+
queryKeys: (keyof T & string) | (keyof T & string)[],
84+
options: { emitEvent: boolean } = { emitEvent: true }
85+
) {
86+
const defs = [];
87+
88+
coerceArray(queryKeys).forEach((key) => {
89+
if (!this.defsSynced[key]) {
90+
this.defsSynced[key] = true;
91+
defs.push(this.getDef(key as keyof T));
92+
}
93+
});
94+
95+
if (defs.length) {
96+
this.updateControl(defs, options);
8797
}
8898
}
8999

@@ -95,7 +105,11 @@ export class BindQueryParamsManager<T = any> {
95105
});
96106
}
97107

98-
private updateControl(defs: QueryParamDef[], updatePredicate = (def: QueryParamDef) => true) {
108+
private updateControl(
109+
defs: QueryParamDef[],
110+
options: { emitEvent: boolean },
111+
updatePredicate = (def: QueryParamDef) => true
112+
) {
99113
const queryParams = new URLSearchParams(this.options.windowRef.location.search);
100114
let value = {};
101115

@@ -110,7 +124,7 @@ export class BindQueryParamsManager<T = any> {
110124
}
111125

112126
if (Object.keys(value).length) {
113-
this.group.patchValue(value);
127+
this.group.patchValue(value, options);
114128
}
115129
}
116130
}

‎projects/ngneat/bind-query-params/src/lib/lib.spec.ts

+29
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,35 @@ describe('BindQueryParams', () => {
312312
})
313313
);
314314
});
315+
316+
it('should allow sync different controls in different times', () => {
317+
spectator = createComponent({
318+
providers: [stubQueryParams('modelToUrl=1,2,3&modelToUrl2=1,2')],
319+
});
320+
321+
expect(spectator.component.group.value).toEqual(
322+
jasmine.objectContaining({
323+
modelToUrl: [],
324+
modelToUrl2: [],
325+
})
326+
);
327+
328+
spectator.component.bindQueryParams.syncDefs('modelToUrl');
329+
330+
expect(spectator.component.group.value).toEqual(
331+
jasmine.objectContaining({
332+
modelToUrl: ['1', '2', '3'],
333+
})
334+
);
335+
336+
spectator.component.bindQueryParams.syncDefs('modelToUrl2');
337+
338+
expect(spectator.component.group.value).toEqual(
339+
jasmine.objectContaining({
340+
modelToUrl2: ['1', '2'],
341+
})
342+
);
343+
});
315344
});
316345
});
317346
});

0 commit comments

Comments
 (0)