Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add queries synchronization feature #4

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions packages/normy-react-query/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { QueryClient, QueryKey } from '@tanstack/react-query';
import {
QueryClient,
QueryKey,
SetDataOptions,
Updater,
} from '@tanstack/react-query';
import { createNormalizer, NormalizerConfig, Data } from '@normy/core';

const shouldBeNormalized = (
Expand All @@ -12,6 +17,23 @@ const shouldBeNormalized = (
return localNormalize;
};

const updateQueriesFromQueryData = (
originalQueryKey: string,
queryData: Data,
normalizer: ReturnType<typeof createNormalizer>,
queryClient: QueryClient,
) => {
const queriesToUpdate = normalizer
.getQueriesToUpdate(queryData)
.filter(query => query.queryKey !== originalQueryKey);
queriesToUpdate.forEach(query => {
queryClient.setQueryData(
JSON.parse(query.queryKey) as QueryKey,
() => query.data,
);
});
};

const updateQueriesFromMutationData = (
mutationData: Data,
normalizer: ReturnType<typeof createNormalizer>,
Expand All @@ -29,7 +51,9 @@ const updateQueriesFromMutationData = (

export const createQueryNormalizer = (
queryClient: QueryClient,
normalizerConfig: NormalizerConfig & { normalize?: boolean } = {},
normalizerConfig: NormalizerConfig & {
normalize?: boolean;
} = {},
) => {
const normalize = normalizerConfig.normalize ?? true;
const normalizer = createNormalizer(normalizerConfig);
Expand All @@ -46,10 +70,14 @@ export const createQueryNormalizer = (
event.query.meta?.normalize as boolean | undefined,
)
) {
normalizer.setQuery(
JSON.stringify(event.query.queryKey),
event.action.data as Data,
);
const queryKey = JSON.stringify(event.query.queryKey);
const data = event.action.data as Data;

normalizer.setQuery(queryKey, data);

if (!event.action.manual) {
updateQueriesFromQueryData(queryKey, data, normalizer, queryClient);
}
}
});

Expand Down Expand Up @@ -95,12 +123,31 @@ export const createQueryNormalizer = (
}
});

const setQueryData = <TQueryFnData>(
queryKey: QueryKey,
updater: Updater<TQueryFnData | undefined, TQueryFnData | undefined>,
options?: SetDataOptions,
): TQueryFnData | undefined => {
const data = queryClient.setQueryData(queryKey, updater, options);
const queryKeyString = JSON.stringify(queryKey);

updateQueriesFromQueryData(
queryKeyString,
data as Data,
normalizer,
queryClient,
);

return data;
};

return {
getNormalizedData: normalizer.getNormalizedData,
clear: () => {
unsubscribeQueryCache();
unsubscribeMutationCache();
normalizer.clearNormalizedData();
},
setQueryData,
};
};