5
5
*/
6
6
7
7
8
- import { getRpcClient } from './extern'
8
+ import { getRpcClient } from './extern'
9
9
import {
10
10
isRpc ,
11
11
Rpc ,
@@ -99,6 +99,14 @@ export interface UseQueryBuilderOptions<TReq, TRes> {
99
99
queryKeyPrefix : string ,
100
100
}
101
101
102
+ const getRpcClientFromCache = (
103
+ queryClient : ReturnType < typeof useQueryClient > ,
104
+ key : string ,
105
+ rpcEndpoint ?: string | HttpEndpoint
106
+ ) : Rpc | undefined => {
107
+ const queryKey = rpcEndpoint ? [ key , rpcEndpoint ] : [ key ] ;
108
+ return queryClient . getQueryData < Rpc > ( queryKey ) ;
109
+ } ;
102
110
103
111
export function buildUseQuery < TReq , TRes > ( opts : UseQueryBuilderOptions < TReq , TRes > ) {
104
112
return < TData = TRes > ( {
@@ -115,16 +123,17 @@ export function buildUseQuery<TReq, TRes>(opts: UseQueryBuilderOptions<TReq, TRe
115
123
116
124
if ( isRpc ( clientResolver ) ) {
117
125
rpcResolver = clientResolver ;
118
- } else if ( isCacheResolver ( clientResolver ) ) {
119
- const key = clientResolver . clientQueryKey || DEFAULT_RPC_CLIENT_QUERY_KEY ;
120
- const queryKey = clientResolver . rpcEndpoint ? [ key , clientResolver . rpcEndpoint ] : [ key ] ;
121
- rpcResolver = queryClient . getQueryData < Rpc > ( queryKey ) ;
122
-
123
- if ( ! rpcResolver && clientResolver . rpcEndpoint ) {
124
- rpcResolver = clientResolver . rpcEndpoint ;
125
- }
126
126
} else {
127
- rpcResolver = clientResolver ;
127
+ const key = isCacheResolver ( clientResolver )
128
+ ? clientResolver . clientQueryKey || DEFAULT_RPC_CLIENT_QUERY_KEY
129
+ : DEFAULT_RPC_CLIENT_QUERY_KEY ;
130
+ const endpoint = isCacheResolver ( clientResolver ) ? clientResolver . rpcEndpoint : undefined ;
131
+
132
+ const cachedClient = getRpcClientFromCache ( queryClient , key , endpoint ) ;
133
+ // For CacheResolver with endpoint, use endpoint as fallback if no cached client
134
+ rpcResolver = cachedClient ||
135
+ ( isCacheResolver ( clientResolver ) && clientResolver . rpcEndpoint ? clientResolver . rpcEndpoint :
136
+ ( ! isCacheResolver ( clientResolver ) ? clientResolver : undefined ) ) ;
128
137
}
129
138
130
139
const queryFn = opts . builderQueryFn ( rpcResolver ) ;
@@ -153,6 +162,14 @@ export interface UseMutationBuilderOptions<TMsg> {
153
162
) => Promise < DeliverTxResponse > ,
154
163
}
155
164
165
+ const getSigningClientFromCache = (
166
+ queryClient : ReturnType < typeof useQueryClient > ,
167
+ key : string ,
168
+ rpcEndpoint ?: string | HttpEndpoint
169
+ ) : ISigningClient | undefined => {
170
+ const queryKey = rpcEndpoint ? [ key , rpcEndpoint ] : [ key ] ;
171
+ return queryClient . getQueryData < ISigningClient > ( queryKey ) ;
172
+ } ;
156
173
157
174
export function buildUseMutation < TMsg , TError > ( opts : UseMutationBuilderOptions < TMsg > ) {
158
175
return ( {
@@ -167,12 +184,15 @@ export function buildUseMutation<TMsg, TError>(opts: UseMutationBuilderOptions<T
167
184
168
185
if ( isISigningClient ( clientResolver ) ) {
169
186
signingClientResolver = clientResolver ;
170
- } else if ( isCacheResolver ( clientResolver ) ) {
171
- const key = clientResolver . clientQueryKey || DEFAULT_SIGNING_CLIENT_QUERY_KEY ;
172
- const queryKey = clientResolver . rpcEndpoint ? [ key , clientResolver . rpcEndpoint ] : [ key ] ;
173
- signingClientResolver = queryClient . getQueryData < ISigningClient > ( queryKey ) ;
174
187
} else {
175
- clientResolver = clientResolver ;
188
+ // For both CacheResolver and other cases, try to get from cache first
189
+ const key = isCacheResolver ( clientResolver )
190
+ ? clientResolver . clientQueryKey || DEFAULT_SIGNING_CLIENT_QUERY_KEY
191
+ : DEFAULT_SIGNING_CLIENT_QUERY_KEY ;
192
+ const endpoint = isCacheResolver ( clientResolver ) ? clientResolver . rpcEndpoint : undefined ;
193
+
194
+ const cachedClient = getSigningClientFromCache ( queryClient , key , endpoint ) ;
195
+ signingClientResolver = cachedClient || ( ! isCacheResolver ( clientResolver ) ? clientResolver : undefined ) ;
176
196
}
177
197
178
198
const mutationFn = opts . builderMutationFn ( signingClientResolver ) ;
0 commit comments