mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
56 lines
2.0 KiB
TypeScript
56 lines
2.0 KiB
TypeScript
import { DevToolsContext } from "@refinedev/devtools-shared";
|
|
import { QueryClient } from "@tanstack/react-query";
|
|
import React, { useContext } from "react";
|
|
import { createQueryListener, createMutationListener } from "./listeners";
|
|
|
|
export const useQuerySubscription =
|
|
__DEV_CONDITION__ !== "development"
|
|
? () => ({})
|
|
: (queryClient: QueryClient) => {
|
|
const { ws } = useContext(DevToolsContext);
|
|
const queryCacheSubscription = React.useRef<() => void>();
|
|
const mutationCacheSubscription = React.useRef<() => void>();
|
|
|
|
React.useEffect(() => {
|
|
if (!ws) return () => 0;
|
|
|
|
const queryCache = queryClient.getQueryCache();
|
|
|
|
const queryListener = createQueryListener(ws);
|
|
|
|
queryCache.getAll().forEach(queryListener);
|
|
|
|
queryCacheSubscription.current = queryCache.subscribe(
|
|
({ query, type }) =>
|
|
(type === "added" || type === "updated") &&
|
|
queryListener(query),
|
|
);
|
|
|
|
return () => {
|
|
queryCacheSubscription.current?.();
|
|
};
|
|
}, [ws, queryClient]);
|
|
|
|
React.useEffect(() => {
|
|
if (!ws) return () => 0;
|
|
|
|
const mutationCache = queryClient.getMutationCache();
|
|
|
|
const mutationListener = createMutationListener(ws);
|
|
|
|
mutationCache.getAll().forEach(mutationListener);
|
|
|
|
mutationCacheSubscription.current = mutationCache.subscribe(
|
|
({ mutation, type }) =>
|
|
(type === "added" || type === "updated") &&
|
|
mutationListener(mutation),
|
|
);
|
|
|
|
return () => {
|
|
mutationCacheSubscription.current?.();
|
|
};
|
|
}, [ws, queryClient]);
|
|
|
|
return {};
|
|
};
|