...
const createProductFetcher = () => {
const cache = new Map<
string,
{ data: Resource<FullProduct>; refetch: () => void }
>();
const getFullProduct = (category: string, productSlug: string) => {
const key = category + "_" + productSlug;
if (!cache.has(key)) {
const [data, { refetch }] = createResource(
() => ({ category, productSlug }),
fetchProduct //fetches the json file
);
if (data) {
cache.set(key, { data, refetch });
}
}
return cache.get(key) as {
data: Resource<FullProduct>;
refetch: () => void;
};
};
return { getFullProduct };
};
export const { getFullProduct } = createProductFetcher();
...
const createProductFetcher = () => {
const cache = new Map<
string,
{ data: Resource<FullProduct>; refetch: () => void }
>();
const getFullProduct = (category: string, productSlug: string) => {
const key = category + "_" + productSlug;
if (!cache.has(key)) {
const [data, { refetch }] = createResource(
() => ({ category, productSlug }),
fetchProduct //fetches the json file
);
if (data) {
cache.set(key, { data, refetch });
}
}
return cache.get(key) as {
data: Resource<FullProduct>;
refetch: () => void;
};
};
return { getFullProduct };
};
export const { getFullProduct } = createProductFetcher();