precallback

This commit is contained in:
Nate Kelley 2025-02-13 12:29:33 -07:00
parent 7d46a35304
commit 8ec0137fbc
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
1 changed files with 10 additions and 2 deletions

View File

@ -36,6 +36,7 @@ export function useSocketMutation<
socketRequest: BusterSocketRequestConfig<TRequestRoute>,
socketResponse: BusterSocketResponseConfig<TRoute>,
options?: UseQueryOptions<TQueryData, any, TQueryData, any>,
preCallback?: (currentData: TQueryData | null, variables: TPayload) => TQueryData,
callback?: (
currentData: TQueryData | null,
newData: InferBusterSocketResponseData<TRoute>
@ -45,6 +46,14 @@ export function useSocketMutation<
const queryClient = useQueryClient();
const mutationFn = useMemoizedFn(async (variables: TPayload): Promise<TData> => {
const queryKey = options?.queryKey;
if (queryKey && preCallback) {
const currentData = queryClient.getQueryData<TQueryData>(queryKey) ?? null;
const transformedData = preCallback(currentData, variables);
queryClient.setQueryData(queryKey, transformedData);
}
try {
const result = await busterSocket.emitAndOnce({
emitEvent: {
@ -58,8 +67,7 @@ export function useSocketMutation<
} as BusterSocketResponse
});
if (options?.queryKey && callback) {
const queryKey = options.queryKey;
if (queryKey && callback) {
const socketData = result as InferBusterSocketResponseData<TRoute>;
const currentData = queryClient.getQueryData<TQueryData>(queryKey) ?? null;
const transformedData = callback(currentData, socketData);