From 1ec0098f541cdae72b01589f772dcffd5d5bc771 Mon Sep 17 00:00:00 2001 From: Enea Jahollari Date: Sun, 25 Feb 2024 12:10:44 +0100 Subject: [PATCH] fix: remove snapshot as route.params emits synchronously (#272) --- libs/ngxtension/inject-params/src/inject-params.spec.ts | 6 ++++++ libs/ngxtension/inject-params/src/inject-params.ts | 7 ++----- .../inject-query-params/src/inject-query-params.ts | 5 ++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libs/ngxtension/inject-params/src/inject-params.spec.ts b/libs/ngxtension/inject-params/src/inject-params.spec.ts index bac4cd14..2f1d8cbb 100644 --- a/libs/ngxtension/inject-params/src/inject-params.spec.ts +++ b/libs/ngxtension/inject-params/src/inject-params.spec.ts @@ -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']); }); }); diff --git a/libs/ngxtension/inject-params/src/inject-params.ts b/libs/ngxtension/inject-params/src/inject-params.ts index 8339186f..41baf96d 100644 --- a/libs/ngxtension/inject-params/src/inject-params.ts +++ b/libs/ngxtension/inject-params/src/inject-params.ts @@ -38,18 +38,15 @@ export function injectParams( ): Signal { 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 }); } diff --git a/libs/ngxtension/inject-query-params/src/inject-query-params.ts b/libs/ngxtension/inject-query-params/src/inject-query-params.ts index b6055166..c061dab7 100644 --- a/libs/ngxtension/inject-query-params/src/inject-query-params.ts +++ b/libs/ngxtension/inject-query-params/src/inject-query-params.ts @@ -40,11 +40,10 @@ export function injectQueryParams( ): Signal { 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, }); } @@ -52,6 +51,6 @@ export function injectQueryParams( keyOrTransform ? params?.[keyOrTransform] ?? null : params; return toSignal(route.queryParams.pipe(map(getParam)), { - initialValue: getParam(queryParams), + requireSync: true, }); }