mirror of https://github.com/buster-so/buster.git
precallback
This commit is contained in:
parent
7d46a35304
commit
8ec0137fbc
|
@ -36,6 +36,7 @@ export function useSocketMutation<
|
||||||
socketRequest: BusterSocketRequestConfig<TRequestRoute>,
|
socketRequest: BusterSocketRequestConfig<TRequestRoute>,
|
||||||
socketResponse: BusterSocketResponseConfig<TRoute>,
|
socketResponse: BusterSocketResponseConfig<TRoute>,
|
||||||
options?: UseQueryOptions<TQueryData, any, TQueryData, any>,
|
options?: UseQueryOptions<TQueryData, any, TQueryData, any>,
|
||||||
|
preCallback?: (currentData: TQueryData | null, variables: TPayload) => TQueryData,
|
||||||
callback?: (
|
callback?: (
|
||||||
currentData: TQueryData | null,
|
currentData: TQueryData | null,
|
||||||
newData: InferBusterSocketResponseData<TRoute>
|
newData: InferBusterSocketResponseData<TRoute>
|
||||||
|
@ -45,6 +46,14 @@ export function useSocketMutation<
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const mutationFn = useMemoizedFn(async (variables: TPayload): Promise<TData> => {
|
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 {
|
try {
|
||||||
const result = await busterSocket.emitAndOnce({
|
const result = await busterSocket.emitAndOnce({
|
||||||
emitEvent: {
|
emitEvent: {
|
||||||
|
@ -58,8 +67,7 @@ export function useSocketMutation<
|
||||||
} as BusterSocketResponse
|
} as BusterSocketResponse
|
||||||
});
|
});
|
||||||
|
|
||||||
if (options?.queryKey && callback) {
|
if (queryKey && callback) {
|
||||||
const queryKey = options.queryKey;
|
|
||||||
const socketData = result as InferBusterSocketResponseData<TRoute>;
|
const socketData = result as InferBusterSocketResponseData<TRoute>;
|
||||||
const currentData = queryClient.getQueryData<TQueryData>(queryKey) ?? null;
|
const currentData = queryClient.getQueryData<TQueryData>(queryKey) ?? null;
|
||||||
const transformedData = callback(currentData, socketData);
|
const transformedData = callback(currentData, socketData);
|
||||||
|
|
Loading…
Reference in New Issue