Skip to content

Commit

Permalink
fix: remove snapshot as route.params emits synchronously (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
eneajaho authored Feb 25, 2024
1 parent 9f90045 commit 1ec0098
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
6 changes: 6 additions & 0 deletions libs/ngxtension/inject-params/src/inject-params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ describe(injectParams.name, () => {
expect(instance.params()).toEqual({ id: 'angular' });
expect(instance.userId()).toEqual('angular');
expect(instance.paramKeysList()).toEqual(['id']);

await harness.navigateByUrl('/user/test', UserProfileComponent);

expect(instance.params()).toEqual({ id: 'test' });
expect(instance.userId()).toEqual('test');
expect(instance.paramKeysList()).toEqual(['id']);
});
});

Expand Down
7 changes: 2 additions & 5 deletions libs/ngxtension/inject-params/src/inject-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,15 @@ export function injectParams<T>(
): Signal<T | Params | string | null> {
assertInInjectionContext(injectParams);
const route = inject(ActivatedRoute);
const params = route.snapshot.params || {};

if (typeof keyOrTransform === 'function') {
return toSignal(route.params.pipe(map(keyOrTransform)), {
initialValue: keyOrTransform(params),
requireSync: true,
});
}

const getParam = (params: Params) =>
keyOrTransform ? params?.[keyOrTransform] ?? null : params;

return toSignal(route.params.pipe(map(getParam)), {
initialValue: getParam(params),
});
return toSignal(route.params.pipe(map(getParam)), { requireSync: true });
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ export function injectQueryParams<T>(
): Signal<T | Params | string | null> {
assertInInjectionContext(injectQueryParams);
const route = inject(ActivatedRoute);
const queryParams = route.snapshot.queryParams || {};

if (typeof keyOrTransform === 'function') {
return toSignal(route.queryParams.pipe(map(keyOrTransform)), {
initialValue: keyOrTransform(queryParams),
requireSync: true,
});
}

const getParam = (params: Params) =>
keyOrTransform ? params?.[keyOrTransform] ?? null : params;

return toSignal(route.queryParams.pipe(map(getParam)), {
initialValue: getParam(queryParams),
requireSync: true,
});
}

0 comments on commit 1ec0098

Please sign in to comment.