All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.react-utils.usePersistedQuery.usePersistedQuery.js.map Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
{"version":3,"file":"usePersistedQuery.js","names":["useEffect","useQuery","isNil","QUERY_CACHE_NAME_SPACE","getFromLocalStorage","setToLocalStorage","localStorageQueryCache","set","key","data","cache","getAll","newCache","_objectSpread","_defineProperty","modifiedAt","Date","now","get","isOutdated","localCache","staleTime","usePersistedQuery","queryKey","fetch","options","queryResult","queryFn","isSuccess","isFreshLoading","isLoading","getCache"],"sources":["../../../src/react-utils/usePersistedQuery/usePersistedQuery.js"],"sourcesContent":["import { useEffect } from \"react\";\n\nimport { useQuery } from \"@tanstack/react-query\";\nimport { isNil } from \"ramda\";\nimport { QUERY_CACHE_NAME_SPACE } from \"react-utils/constants\";\nimport { getFromLocalStorage, setToLocalStorage } from \"utils/general\";\n\nconst localStorageQueryCache = {\n  set: (key, data) => {\n    const cache = localStorageQueryCache.getAll();\n    const newCache = { ...cache, [key]: { data, modifiedAt: Date.now() } };\n    setToLocalStorage(QUERY_CACHE_NAME_SPACE, newCache);\n  },\n  getAll: () => {\n    const cache = getFromLocalStorage(QUERY_CACHE_NAME_SPACE);\n    if (!cache) return {};\n\n    return cache;\n  },\n  get: key => localStorageQueryCache.getAll()[key],\n};\n\nconst isOutdated = (localCache, staleTime) => {\n  if (isNil(localCache)) return true;\n\n  if (isNil(staleTime)) return false;\n\n  return localCache.modifiedAt <= Date.now() - staleTime;\n};\n\nconst usePersistedQuery = (queryKey, fetch, options) => {\n  const queryResult = useQuery({\n    queryKey,\n    queryFn: fetch,\n    ...options,\n  });\n  const localCache = localStorageQueryCache.get(queryKey);\n\n  useEffect(() => {\n    if (!queryResult.isSuccess) return;\n    localStorageQueryCache.set(queryKey, queryResult.data);\n  }, [queryKey, queryResult.data, queryResult.isSuccess]);\n\n  if (isOutdated(localCache, options?.staleTime)) {\n    return { ...queryResult, isFreshLoading: queryResult.isLoading };\n  }\n\n  return {\n    ...queryResult,\n    data: queryResult.data || localCache.data,\n    isFreshLoading: false,\n  };\n};\n\nusePersistedQuery.getCache = localStorageQueryCache.get;\n\nexport default usePersistedQuery;\n"],"mappings":";;;AAAA,SAASA,SAAS,QAAQ,OAAO;AAEjC,SAASC,QAAQ,QAAQ,uBAAuB;AAChD,SAASC,KAAK,QAAQ,OAAO;AAC7B,SAASC,sBAAsB;AAC/B,SAASC,mBAAmB,EAAEC,iBAAiB;AAE/C,IAAMC,sBAAsB,GAAG;EAC7BC,GAAG,EAAE,SAALA,GAAGA,CAAGC,GAAG,EAAEC,IAAI,EAAK;IAClB,IAAMC,KAAK,GAAGJ,sBAAsB,CAACK,MAAM,CAAC,CAAC;IAC7C,IAAMC,QAAQ,GAAAC,aAAA,CAAAA,aAAA,KAAQH,KAAK,OAAAI,eAAA,KAAGN,GAAG,EAAG;MAAEC,IAAI,EAAJA,IAAI;MAAEM,UAAU,EAAEC,IAAI,CAACC,GAAG,CAAC;IAAE,CAAC,EAAE;IACtEZ,iBAAiB,CAACF,sBAAsB,EAAES,QAAQ,CAAC;EACrD,CAAC;EACDD,MAAM,EAAE,SAARA,MAAMA,CAAA,EAAQ;IACZ,IAAMD,KAAK,GAAGN,mBAAmB,CAACD,sBAAsB,CAAC;IACzD,IAAI,CAACO,KAAK,EAAE,OAAO,CAAC,CAAC;IAErB,OAAOA,KAAK;EACd,CAAC;EACDQ,GAAG,EAAE,SAALA,GAAGA,CAAEV,GAAG;IAAA,OAAIF,sBAAsB,CAACK,MAAM,CAAC,CAAC,CAACH,GAAG,CAAC;EAAA;AAClD,CAAC;AAED,IAAMW,UAAU,GAAG,SAAbA,UAAUA,CAAIC,UAAU,EAAEC,SAAS,EAAK;EAC5C,IAAInB,KAAK,CAACkB,UAAU,CAAC,EAAE,OAAO,IAAI;EAElC,IAAIlB,KAAK,CAACmB,SAAS,CAAC,EAAE,OAAO,KA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