package.cjs.react-utils.usePersistedQuery.usePersistedQuery.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _reactQuery = require("@tanstack/react-query");
var _ramda = require("ramda");
var _constants = require("../constants");
var _general = require("../../utils/general");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var localStorageQueryCache = {
set: function set(key, data) {
var cache = localStorageQueryCache.getAll();
var newCache = _objectSpread(_objectSpread({}, cache), {}, (0, _defineProperty2["default"])({}, key, {
data: data,
modifiedAt: Date.now()
}));
(0, _general.setToLocalStorage)(_constants.QUERY_CACHE_NAME_SPACE, newCache);
},
getAll: function getAll() {
var cache = (0, _general.getFromLocalStorage)(_constants.QUERY_CACHE_NAME_SPACE);
if (!cache) return {};
return cache;
},
get: function get(key) {
return localStorageQueryCache.getAll()[key];
}
};
var isOutdated = function isOutdated(localCache, staleTime) {
if ((0, _ramda.isNil)(localCache)) return true;
if ((0, _ramda.isNil)(staleTime)) return false;
return localCache.modifiedAt <= Date.now() - staleTime;
};
var usePersistedQuery = function usePersistedQuery(queryKey, fetch, options) {
var queryResult = (0, _reactQuery.useQuery)(_objectSpread({
queryKey: queryKey,
queryFn: fetch
}, options));
var localCache = localStorageQueryCache.get(queryKey);
(0, _react.useEffect)(function () {
if (!queryResult.isSuccess) return;
localStorageQueryCache.set(queryKey, queryResult.data);
}, [queryKey, queryResult.data, queryResult.isSuccess]);
if (isOutdated(localCache, options === null || options === void 0 ? void 0 : options.staleTime)) {
return _objectSpread(_objectSpread({}, queryResult), {}, {
isFreshLoading: queryResult.isLoading
});
}
return _objectSpread(_objectSpread({}, queryResult), {}, {
data: queryResult.data || localCache.data,
isFreshLoading: false
});
};
usePersistedQuery.getCache = localStorageQueryCache.get;
var _default = usePersistedQuery;
exports["default"] = _default;
//# sourceMappingURL=usePersistedQuery.js.map