From 87b4b45e64275d74d71aa672e75a25eaddc8a792 Mon Sep 17 00:00:00 2001 From: Saul Date: Thu, 29 Feb 2024 10:04:23 +0100 Subject: [PATCH] fix: new query deep signals --- .../ngrx-rtk-query/src/lib/build-hooks.ts | 42 ++++++++++++++----- .../src/lib/types/hooks-types.ts | 19 +++++---- .../src/lib/utils/deep-signal.ts | 10 ++--- .../counter-manager.component.ts | 2 +- .../counter/counter/counter.component.ts | 2 +- .../lazy/lazy-counter/lazy.component.ts | 2 +- .../character-card.component.ts | 2 +- .../characters-list.component.ts | 16 +++---- .../post-detail/post-detail.component.ts | 14 +++---- .../posts/posts-list/posts-list.component.ts | 2 +- 10 files changed, 68 insertions(+), 43 deletions(-) diff --git a/projects/ngrx-rtk-query/src/lib/build-hooks.ts b/projects/ngrx-rtk-query/src/lib/build-hooks.ts index b0cd4ee..7e01424 100644 --- a/projects/ngrx-rtk-query/src/lib/build-hooks.ts +++ b/projects/ngrx-rtk-query/src/lib/build-hooks.ts @@ -128,6 +128,13 @@ export function buildHooks({ isFetching, isLoading, isSuccess, + // Deep signals required init in undefined atleast + endpointName: currentState.endpointName, + error: currentState.error, + fulfilledTimeStamp: currentState.fulfilledTimeStamp, + originalArgs: currentState.originalArgs, + requestId: currentState.requestId, + startedTimeStamp: currentState.startedTimeStamp, } as UseQueryStateDefaultResult; } @@ -370,8 +377,9 @@ export function buildHooks({ lastValue = selectDefaultResult(getState()); return currentState(); }); + const deepSignal = toDeepSignal(currentState); - return currentState; + return deepSignal as any; }; return { @@ -385,11 +393,10 @@ export function buildHooks({ skip: arg() === UNINITIALIZED_VALUE, })); const queryStateResults = useQueryState(arg, subscriptionOptions); - const queryState = computed(() => ({ ...queryStateResults(), lastArg: arg() })); - const deepSignal = toDeepSignal(queryState); - Object.assign(deepSignal, { fetch: trigger }); + Object.assign(queryStateResults, { fetch: trigger }); + Object.assign(queryStateResults, { lastArg: arg }); - return deepSignal as any; + return queryStateResults as any; }, useQuery(arg, options) { const querySubscriptionResults = useQuerySubscription(arg, options); @@ -400,10 +407,9 @@ export function buildHooks({ return { selectFromResult, ...options }; }); const queryStateResults = useQueryState(arg, subscriptionOptions); - const queryState = computed(() => ({ ...queryStateResults(), ...querySubscriptionResults })); - const deepSignal = toDeepSignal(queryState); + Object.assign(queryStateResults, querySubscriptionResults); - return deepSignal as any; + return queryStateResults as any; }, selector: select as QuerySelector, }; @@ -433,8 +439,22 @@ export function buildHooks({ return promise; }; + const fixedSelect: typeof select = (args) => (state) => { + const currentState = select(args)(state); + return { + ...currentState, + // Deep signals required init in undefined atleast + data: currentState.data, + endpointName: currentState.endpointName, + error: currentState.error, + fulfilledTimeStamp: currentState.fulfilledTimeStamp, + requestId: currentState.requestId, + startedTimeStamp: currentState.startedTimeStamp, + } as any; + }; + const requestId = computed(() => promiseRef()?.requestId); - const selectDefaultResult = (requestId?: string) => select({ fixedCacheKey, requestId }); + const selectDefaultResult = (requestId?: string) => fixedSelect({ fixedCacheKey, requestId }); const mutationSelector = ( requestId?: string, ): MemoizedSelector, any, DefaultProjectorFn> => @@ -462,9 +482,11 @@ export function buildHooks({ } }; - const finalState = computed(() => ({ ...currentState()(), originalArgs: originalArgs(), reset })); + const finalState = computed(() => currentState()()); const deepSignal = toDeepSignal(finalState); Object.assign(deepSignal, { dispatch: triggerMutation }); + Object.assign(deepSignal, { originalArgs }); + Object.assign(deepSignal, { reset }); return deepSignal as any; }; diff --git a/projects/ngrx-rtk-query/src/lib/types/hooks-types.ts b/projects/ngrx-rtk-query/src/lib/types/hooks-types.ts index 55fd96d..8781bf8 100644 --- a/projects/ngrx-rtk-query/src/lib/types/hooks-types.ts +++ b/projects/ngrx-rtk-query/src/lib/types/hooks-types.ts @@ -70,7 +70,7 @@ export type UseQuery> = < >( arg: Signal | SkipToken> | QueryArgFrom | SkipToken, options?: UseQueryOptions | Signal>, -) => DeepSignal>; +) => UseQueryHookResult; export type TypedUseQuery = UseQuery< QueryDefinition @@ -238,8 +238,9 @@ export type UseLazyQuery> = < R extends Record = UseQueryStateDefaultResult, >( options?: UseLazyQueryOptions | Signal>, -) => DeepSignal & { lastArg: QueryArgFrom }> & { +) => UseQueryStateResult & { fetch: LazyQueryTrigger; + lastArg: Signal>; }; export type TypedUseLazyQuery = UseLazyQuery< @@ -304,7 +305,7 @@ export type UseQueryState> = < >( arg: Signal | SkipToken> | QueryArgFrom | SkipToken, options?: UseQueryStateOptions | Signal>, -) => Signal>; +) => UseQueryStateResult; export type UseQueryStateOptions, R extends Record> = { /** @@ -349,7 +350,7 @@ export type UseQueryStateOptions, selectFromResult?: QueryStateSelector; }; -export type UseQueryStateResult<_ extends QueryDefinition, R> = TSHelpersNoInfer; +export type UseQueryStateResult<_ extends QueryDefinition, R> = DeepSignal>; /** * Helper type to manually type the result @@ -360,7 +361,7 @@ export type TypedUseQueryStateResult< QueryArg, BaseQuery extends BaseQueryFn, R = UseQueryStateDefaultResult>, -> = TSHelpersNoInfer; +> = DeepSignal>; type UseQueryStateBaseResult> = QuerySubState & { /** @@ -429,8 +430,10 @@ export type UseMutationStateOptions, R> = TSHelpersNoInfer & { - originalArgs?: QueryArgFrom; +export type UseMutationStateResult, R> = DeepSignal< + TSHelpersNoInfer +> & { + originalArgs: Signal | undefined>; /** * Resets the hook state to it's initial `uninitialized` state. * This will also remove the last result from the cache. @@ -464,7 +467,7 @@ export type UseMutation> = < R extends Record = MutationResultSelectorResult, >( options?: UseMutationStateOptions, -) => DeepSignal> & { dispatch: MutationTrigger }; +) => UseMutationStateResult & { dispatch: MutationTrigger }; export type TypedUseMutation = UseMutation< MutationDefinition diff --git a/projects/ngrx-rtk-query/src/lib/utils/deep-signal.ts b/projects/ngrx-rtk-query/src/lib/utils/deep-signal.ts index 45a4278..ffb16bc 100644 --- a/projects/ngrx-rtk-query/src/lib/utils/deep-signal.ts +++ b/projects/ngrx-rtk-query/src/lib/utils/deep-signal.ts @@ -17,11 +17,11 @@ export interface Signal extends NgSignal { } export type DeepSignal = Signal & - (IsKnownRecord extends true - ? Readonly<{ - [K in keyof T]: IsKnownRecord extends true ? DeepSignal : Signal; - }> - : unknown); + Readonly< + Required<{ + [K in keyof T]: IsKnownRecord extends true ? DeepSignal : Signal; + }> + >; export function toDeepSignal(signal: Signal): DeepSignal { const value = untracked(() => signal()); diff --git a/src/app/features/counter/counter-manager/counter-manager.component.ts b/src/app/features/counter/counter-manager/counter-manager.component.ts index 0e024e6..007dcd3 100644 --- a/src/app/features/counter/counter-manager/counter-manager.component.ts +++ b/src/app/features/counter/counter-manager/counter-manager.component.ts @@ -13,7 +13,7 @@ import { nanoid } from '@reduxjs/toolkit'; - {{ countQuery().data?.count || 0 }} + {{ countQuery.data()?.count || 0 }} Decrease is a optimistic update! diff --git a/src/app/features/counter/counter/counter.component.ts b/src/app/features/counter/counter/counter.component.ts index b456814..7f6ccc8 100644 --- a/src/app/features/counter/counter/counter.component.ts +++ b/src/app/features/counter/counter/counter.component.ts @@ -18,7 +18,7 @@ import { pollingOptions } from '../utils/polling-options'; + {{ - countQuery().data?.count || 0 + countQuery.data()?.count || 0 }}
-
{{ charactersQuery().error | json }}
+
{{ charactersQuery.error() | json }}
-
+
-

{{ postQuery().data?.name }}

- Loading... +

{{ postQuery.data()?.name }}

+ Loading...
-
@@ -46,7 +46,7 @@ import { useDeletePostMutation, useGetPostQuery, useUpdatePostMutation } from '.
-
{{ postQuery().data | json }}
+
{{ postQuery.data() | json }}
`, styles: [], diff --git a/src/app/features/posts/posts-list/posts-list.component.ts b/src/app/features/posts/posts-list/posts-list.component.ts index 26cb158..5708097 100644 --- a/src/app/features/posts/posts-list/posts-list.component.ts +++ b/src/app/features/posts/posts-list/posts-list.component.ts @@ -6,7 +6,7 @@ import { useGetPostsQuery } from '../services'; selector: 'app-posts-list', template: `
- +