UNPKG

react-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in React

3 lines (2 loc) 2.5 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).ReactQueryPersistLocalStorageExperimental={})}(this,(function(t){"use strict";function e(){return(e=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var a=arguments[e];for(var r in a)Object.prototype.hasOwnProperty.call(a,r)&&(t[r]=a[r])}return t}).apply(this,arguments)}function a(t){return t.state.isPaused}function r(t){return"success"===t.state.status}function n(t,e){var n,o,u=[],i=[];if(!1!==(null==(n=e=e||{})?void 0:n.dehydrateMutations)){var s=e.shouldDehydrateMutation||a;t.getMutationCache().getAll().forEach((function(t){s(t)&&u.push(function(t){return{mutationKey:t.options.mutationKey,state:t.state}}(t))}))}if(!1!==(null==(o=e)?void 0:o.dehydrateQueries)){var l=e.shouldDehydrateQuery||r;t.getQueryCache().getAll().forEach((function(t){l(t)&&i.push(function(t){return{state:t.state,queryKey:t.queryKey,queryHash:t.queryHash}}(t))}))}return{mutations:u,queries:i}}t.persistWithLocalStorage=function(t,a){var r=void 0===a?{}:a,o=r.localStorageKey,u=void 0===o?"REACT_QUERY_OFFLINE_CACHE":o,i=r.throttleTime,s=void 0===i?1e3:i,l=r.maxAge,c=void 0===l?864e5:l,f=r.buster,d=void 0===f?"":f;if("undefined"!=typeof window){var y=function(t,e){void 0===e&&(e=100);var a=null;return function(){for(var r=arguments.length,n=new Array(r),o=0;o<r;o++)n[o]=arguments[o];null===a&&(a=setTimeout((function(){t.apply(void 0,n),a=null}),e))}}((function(){var e={buster:d,timestamp:Date.now(),cacheState:n(t)};localStorage.setItem(u,JSON.stringify(e))}),s);t.getQueryCache().subscribe(y);var h=localStorage.getItem(u);if(!h)return;var v=JSON.parse(h);if(v.timestamp){var p=Date.now()-v.timestamp>c,m=v.buster!==d;p||m?localStorage.removeItem(u):function(t,a,r){if("object"==typeof a&&null!==a){var n=t.getMutationCache(),o=t.getQueryCache(),u=a.mutations||[],i=a.queries||[];u.forEach((function(a){var o;n.build(t,e({},null==r||null==(o=r.defaultOptions)?void 0:o.mutations,{mutationKey:a.mutationKey}),a.state)})),i.forEach((function(a){var n,u=o.get(a.queryHash);u?u.state.dataUpdatedAt<a.state.dataUpdatedAt&&u.setState(a.state):o.build(t,e({},null==r||null==(n=r.defaultOptions)?void 0:n.queries,{queryKey:a.queryKey,queryHash:a.queryHash}),a.state)}))}}(t,v.cacheState)}else localStorage.removeItem(u)}},Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=persist-localstorage-experimental.production.min.js.map