@tanstack/react-query-persist-client
Advanced tools
Comparing version
@@ -12,6 +12,6 @@ /** | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tanstack/query-core'), require('react'), require('@tanstack/react-query')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@tanstack/query-core', 'react', '@tanstack/react-query'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactQueryPersistClient = {}, global.QueryCore, global.React, global.ReactQuery)); | ||
})(this, (function (exports, queryCore, React, reactQuery) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('@tanstack/react-query')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'react', '@tanstack/react-query'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactQueryPersistClient = {}, global.React, global.ReactQuery)); | ||
})(this, (function (exports, React, reactQuery) { 'use strict'; | ||
@@ -39,2 +39,299 @@ function _interopNamespace(e) { | ||
/** | ||
* query-core | ||
* | ||
* Copyright (c) TanStack | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.md file in the root directory of this source tree. | ||
* | ||
* @license MIT | ||
*/ | ||
class Subscribable { | ||
constructor() { | ||
this.listeners = []; | ||
this.subscribe = this.subscribe.bind(this); | ||
} | ||
subscribe(listener) { | ||
this.listeners.push(listener); | ||
this.onSubscribe(); | ||
return () => { | ||
this.listeners = this.listeners.filter(x => x !== listener); | ||
this.onUnsubscribe(); | ||
}; | ||
} | ||
hasListeners() { | ||
return this.listeners.length > 0; | ||
} | ||
onSubscribe() {// Do nothing | ||
} | ||
onUnsubscribe() {// Do nothing | ||
} | ||
} | ||
// TYPES | ||
// UTILS | ||
const isServer = typeof window === 'undefined'; | ||
class FocusManager extends Subscribable { | ||
constructor() { | ||
super(); | ||
this.setup = onFocus => { | ||
// addEventListener does not exist in React Native, but window does | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (!isServer && window.addEventListener) { | ||
const listener = () => onFocus(); // Listen to visibillitychange and focus | ||
window.addEventListener('visibilitychange', listener, false); | ||
window.addEventListener('focus', listener, false); | ||
return () => { | ||
// Be sure to unsubscribe if a new handler is set | ||
window.removeEventListener('visibilitychange', listener); | ||
window.removeEventListener('focus', listener); | ||
}; | ||
} | ||
}; | ||
} | ||
onSubscribe() { | ||
if (!this.cleanup) { | ||
this.setEventListener(this.setup); | ||
} | ||
} | ||
onUnsubscribe() { | ||
if (!this.hasListeners()) { | ||
var _this$cleanup; | ||
(_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this); | ||
this.cleanup = undefined; | ||
} | ||
} | ||
setEventListener(setup) { | ||
var _this$cleanup2; | ||
this.setup = setup; | ||
(_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this); | ||
this.cleanup = setup(focused => { | ||
if (typeof focused === 'boolean') { | ||
this.setFocused(focused); | ||
} else { | ||
this.onFocus(); | ||
} | ||
}); | ||
} | ||
setFocused(focused) { | ||
this.focused = focused; | ||
if (focused) { | ||
this.onFocus(); | ||
} | ||
} | ||
onFocus() { | ||
this.listeners.forEach(listener => { | ||
listener(); | ||
}); | ||
} | ||
isFocused() { | ||
if (typeof this.focused === 'boolean') { | ||
return this.focused; | ||
} // document global can be unavailable in react native | ||
if (typeof document === 'undefined') { | ||
return true; | ||
} | ||
return [undefined, 'visible', 'prerender'].includes(document.visibilityState); | ||
} | ||
} | ||
new FocusManager(); | ||
class OnlineManager extends Subscribable { | ||
constructor() { | ||
super(); | ||
this.setup = onOnline => { | ||
// addEventListener does not exist in React Native, but window does | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (!isServer && window.addEventListener) { | ||
const listener = () => onOnline(); // Listen to online | ||
window.addEventListener('online', listener, false); | ||
window.addEventListener('offline', listener, false); | ||
return () => { | ||
// Be sure to unsubscribe if a new handler is set | ||
window.removeEventListener('online', listener); | ||
window.removeEventListener('offline', listener); | ||
}; | ||
} | ||
}; | ||
} | ||
onSubscribe() { | ||
if (!this.cleanup) { | ||
this.setEventListener(this.setup); | ||
} | ||
} | ||
onUnsubscribe() { | ||
if (!this.hasListeners()) { | ||
var _this$cleanup; | ||
(_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this); | ||
this.cleanup = undefined; | ||
} | ||
} | ||
setEventListener(setup) { | ||
var _this$cleanup2; | ||
this.setup = setup; | ||
(_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this); | ||
this.cleanup = setup(online => { | ||
if (typeof online === 'boolean') { | ||
this.setOnline(online); | ||
} else { | ||
this.onOnline(); | ||
} | ||
}); | ||
} | ||
setOnline(online) { | ||
this.online = online; | ||
if (online) { | ||
this.onOnline(); | ||
} | ||
} | ||
onOnline() { | ||
this.listeners.forEach(listener => { | ||
listener(); | ||
}); | ||
} | ||
isOnline() { | ||
if (typeof this.online === 'boolean') { | ||
return this.online; | ||
} | ||
if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') { | ||
return true; | ||
} | ||
return navigator.onLine; | ||
} | ||
} | ||
new OnlineManager(); | ||
// TYPES | ||
// FUNCTIONS | ||
function dehydrateMutation(mutation) { | ||
return { | ||
mutationKey: mutation.options.mutationKey, | ||
state: mutation.state | ||
}; | ||
} // Most config is not dehydrated but instead meant to configure again when | ||
// consuming the de/rehydrated data, typically with useQuery on the client. | ||
// Sometimes it might make sense to prefetch data on the server and include | ||
// in the html-payload, but not consume it on the initial render. | ||
function dehydrateQuery(query) { | ||
return { | ||
state: query.state, | ||
queryKey: query.queryKey, | ||
queryHash: query.queryHash | ||
}; | ||
} | ||
function defaultShouldDehydrateMutation(mutation) { | ||
return mutation.state.isPaused; | ||
} | ||
function defaultShouldDehydrateQuery(query) { | ||
return query.state.status === 'success'; | ||
} | ||
function dehydrate(client, options = {}) { | ||
const mutations = []; | ||
const queries = []; | ||
if (options.dehydrateMutations !== false) { | ||
const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation; | ||
client.getMutationCache().getAll().forEach(mutation => { | ||
if (shouldDehydrateMutation(mutation)) { | ||
mutations.push(dehydrateMutation(mutation)); | ||
} | ||
}); | ||
} | ||
if (options.dehydrateQueries !== false) { | ||
const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery; | ||
client.getQueryCache().getAll().forEach(query => { | ||
if (shouldDehydrateQuery(query)) { | ||
queries.push(dehydrateQuery(query)); | ||
} | ||
}); | ||
} | ||
return { | ||
mutations, | ||
queries | ||
}; | ||
} | ||
function hydrate(client, dehydratedState, options) { | ||
if (typeof dehydratedState !== 'object' || dehydratedState === null) { | ||
return; | ||
} | ||
const mutationCache = client.getMutationCache(); | ||
const queryCache = client.getQueryCache(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
const mutations = dehydratedState.mutations || []; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
const queries = dehydratedState.queries || []; | ||
mutations.forEach(dehydratedMutation => { | ||
var _options$defaultOptio; | ||
mutationCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio = options.defaultOptions) == null ? void 0 : _options$defaultOptio.mutations), | ||
mutationKey: dehydratedMutation.mutationKey | ||
}, dehydratedMutation.state); | ||
}); | ||
queries.forEach(dehydratedQuery => { | ||
var _options$defaultOptio2; | ||
const query = queryCache.get(dehydratedQuery.queryHash); // Do not hydrate if an existing query exists with newer data | ||
if (query) { | ||
if (query.state.dataUpdatedAt < dehydratedQuery.state.dataUpdatedAt) { | ||
query.setState(dehydratedQuery.state); | ||
} | ||
return; | ||
} // Restore query | ||
queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries), | ||
queryKey: dehydratedQuery.queryKey, | ||
queryHash: dehydratedQuery.queryHash | ||
}, dehydratedQuery.state); | ||
}); | ||
} | ||
/** | ||
* Restores persisted data to the QueryCache | ||
@@ -63,3 +360,3 @@ * - data obtained from persister.restoreClient | ||
} else { | ||
queryCore.hydrate(queryClient, persistedClient.clientState, hydrateOptions); | ||
hydrate(queryClient, persistedClient.clientState, hydrateOptions); | ||
} | ||
@@ -94,3 +391,3 @@ } else { | ||
timestamp: Date.now(), | ||
clientState: queryCore.dehydrate(queryClient, dehydrateOptions) | ||
clientState: dehydrate(queryClient, dehydrateOptions) | ||
}; | ||
@@ -97,0 +394,0 @@ await persister.persistClient(persistClient); |
@@ -11,3 +11,13 @@ /** | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@tanstack/query-core"),require("react"),require("@tanstack/react-query")):"function"==typeof define&&define.amd?define(["exports","@tanstack/query-core","react","@tanstack/react-query"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactQueryPersistClient={},e.QueryCore,e.React,e.ReactQuery)}(this,(function(e,t,r,n){"use strict";function s(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var i=s(r);async function u({queryClient:e,persister:r,maxAge:n=864e5,buster:s="",hydrateOptions:i}){try{const u=await r.restoreClient();if(u)if(u.timestamp){const c=Date.now()-u.timestamp>n,a=u.buster!==s;c||a?r.removeClient():t.hydrate(e,u.clientState,i)}else r.removeClient()}catch(e){r.removeClient()}}async function c({queryClient:e,persister:r,buster:n="",dehydrateOptions:s}){const i={buster:n,timestamp:Date.now(),clientState:t.dehydrate(e,s)};await r.persistClient(i)}function a(e){const t=e.queryClient.getQueryCache().subscribe((()=>{c(e)})),r=e.queryClient.getMutationCache().subscribe((()=>{c(e)}));return()=>{t(),r()}}function o(e){let t,r=!1;return[()=>{r=!0,null==t||t()},u(e).then((()=>{r||(t=a(e))}))]}function l(){return l=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},l.apply(this,arguments)}e.PersistQueryClientProvider=({client:e,children:t,persistOptions:r,onSuccess:s,...u})=>{const[c,a]=i.useState(!0),f=i.useRef({persistOptions:r,onSuccess:s});return i.useEffect((()=>{f.current={persistOptions:r,onSuccess:s}})),i.useEffect((()=>{let t=!1;a(!0);const[r,n]=o({...f.current.persistOptions,queryClient:e});return n.then((()=>{t||(null==f.current.onSuccess||f.current.onSuccess(),a(!1))})),()=>{t=!0,r()}}),[e]),i.createElement(n.QueryClientProvider,l({client:e},u),i.createElement(n.IsRestoringProvider,{value:c},t))},e.persistQueryClient=o,e.persistQueryClientRestore=u,e.persistQueryClientSave=c,e.persistQueryClientSubscribe=a,e.removeOldestQuery=({persistedClient:e})=>{const t=[...e.clientState.mutations],r=[...e.clientState.queries],n={...e,clientState:{mutations:t,queries:r}},s=[...r].sort(((e,t)=>e.state.dataUpdatedAt-t.state.dataUpdatedAt));if(s.length>0){const e=s.shift();return n.clientState.queries=r.filter((t=>t!==e)),n}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("@tanstack/react-query")):"function"==typeof define&&define.amd?define(["exports","react","@tanstack/react-query"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactQueryPersistClient={},e.React,e.ReactQuery)}(this,(function(e,t,n){"use strict";function s(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var s=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,s.get?s:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var i=s(t); | ||
/** | ||
* query-core | ||
* | ||
* Copyright (c) TanStack | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.md file in the root directory of this source tree. | ||
* | ||
* @license MIT | ||
*/class r{constructor(){this.listeners=[],this.subscribe=this.subscribe.bind(this)}subscribe(e){return this.listeners.push(e),this.onSubscribe(),()=>{this.listeners=this.listeners.filter((t=>t!==e)),this.onUnsubscribe()}}hasListeners(){return this.listeners.length>0}onSubscribe(){}onUnsubscribe(){}}const u="undefined"==typeof window;new class extends r{constructor(){super(),this.setup=e=>{if(!u&&window.addEventListener){const t=()=>e();return window.addEventListener("visibilitychange",t,!1),window.addEventListener("focus",t,!1),()=>{window.removeEventListener("visibilitychange",t),window.removeEventListener("focus",t)}}}}onSubscribe(){this.cleanup||this.setEventListener(this.setup)}onUnsubscribe(){var e;this.hasListeners()||(null==(e=this.cleanup)||e.call(this),this.cleanup=void 0)}setEventListener(e){var t;this.setup=e,null==(t=this.cleanup)||t.call(this),this.cleanup=e((e=>{"boolean"==typeof e?this.setFocused(e):this.onFocus()}))}setFocused(e){this.focused=e,e&&this.onFocus()}onFocus(){this.listeners.forEach((e=>{e()}))}isFocused(){return"boolean"==typeof this.focused?this.focused:"undefined"==typeof document||[void 0,"visible","prerender"].includes(document.visibilityState)}};function o(e){return e.state.isPaused}function a(e){return"success"===e.state.status}function c(e,t={}){const n=[],s=[];if(!1!==t.dehydrateMutations){const s=t.shouldDehydrateMutation||o;e.getMutationCache().getAll().forEach((e=>{s(e)&&n.push(function(e){return{mutationKey:e.options.mutationKey,state:e.state}}(e))}))}if(!1!==t.dehydrateQueries){const n=t.shouldDehydrateQuery||a;e.getQueryCache().getAll().forEach((e=>{n(e)&&s.push(function(e){return{state:e.state,queryKey:e.queryKey,queryHash:e.queryHash}}(e))}))}return{mutations:n,queries:s}}async function l({queryClient:e,persister:t,maxAge:n=864e5,buster:s="",hydrateOptions:i}){try{const r=await t.restoreClient();if(r)if(r.timestamp){const u=Date.now()-r.timestamp>n,o=r.buster!==s;u||o?t.removeClient():function(e,t,n){if("object"!=typeof t||null===t)return;const s=e.getMutationCache(),i=e.getQueryCache(),r=t.mutations||[],u=t.queries||[];r.forEach((t=>{var i;s.build(e,{...null==n||null==(i=n.defaultOptions)?void 0:i.mutations,mutationKey:t.mutationKey},t.state)})),u.forEach((t=>{var s;const r=i.get(t.queryHash);r?r.state.dataUpdatedAt<t.state.dataUpdatedAt&&r.setState(t.state):i.build(e,{...null==n||null==(s=n.defaultOptions)?void 0:s.queries,queryKey:t.queryKey,queryHash:t.queryHash},t.state)}))}(e,r.clientState,i)}else t.removeClient()}catch(e){t.removeClient()}}async function d({queryClient:e,persister:t,buster:n="",dehydrateOptions:s}){const i={buster:n,timestamp:Date.now(),clientState:c(e,s)};await t.persistClient(i)}function h(e){const t=e.queryClient.getQueryCache().subscribe((()=>{d(e)})),n=e.queryClient.getMutationCache().subscribe((()=>{d(e)}));return()=>{t(),n()}}function f(e){let t,n=!1;return[()=>{n=!0,null==t||t()},l(e).then((()=>{n||(t=h(e))}))]}function p(){return p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var s in n)Object.prototype.hasOwnProperty.call(n,s)&&(e[s]=n[s])}return e},p.apply(this,arguments)}new class extends r{constructor(){super(),this.setup=e=>{if(!u&&window.addEventListener){const t=()=>e();return window.addEventListener("online",t,!1),window.addEventListener("offline",t,!1),()=>{window.removeEventListener("online",t),window.removeEventListener("offline",t)}}}}onSubscribe(){this.cleanup||this.setEventListener(this.setup)}onUnsubscribe(){var e;this.hasListeners()||(null==(e=this.cleanup)||e.call(this),this.cleanup=void 0)}setEventListener(e){var t;this.setup=e,null==(t=this.cleanup)||t.call(this),this.cleanup=e((e=>{"boolean"==typeof e?this.setOnline(e):this.onOnline()}))}setOnline(e){this.online=e,e&&this.onOnline()}onOnline(){this.listeners.forEach((e=>{e()}))}isOnline(){return"boolean"==typeof this.online?this.online:"undefined"==typeof navigator||void 0===navigator.onLine||navigator.onLine}};e.PersistQueryClientProvider=({client:e,children:t,persistOptions:s,onSuccess:r,...u})=>{const[o,a]=i.useState(!0),c=i.useRef({persistOptions:s,onSuccess:r});return i.useEffect((()=>{c.current={persistOptions:s,onSuccess:r}})),i.useEffect((()=>{let t=!1;a(!0);const[n,s]=f({...c.current.persistOptions,queryClient:e});return s.then((()=>{t||(null==c.current.onSuccess||c.current.onSuccess(),a(!1))})),()=>{t=!0,n()}}),[e]),i.createElement(n.QueryClientProvider,p({client:e},u),i.createElement(n.IsRestoringProvider,{value:o},t))},e.persistQueryClient=f,e.persistQueryClientRestore=l,e.persistQueryClientSave=d,e.persistQueryClientSubscribe=h,e.removeOldestQuery=({persistedClient:e})=>{const t=[...e.clientState.mutations],n=[...e.clientState.queries],s={...e,clientState:{mutations:t,queries:n}},i=[...n].sort(((e,t)=>e.state.dataUpdatedAt-t.state.dataUpdatedAt));if(i.length>0){const e=i.shift();return s.clientState.queries=n.filter((t=>t!==e)),s}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.production.js.map |
{ | ||
"name": "@tanstack/react-query-persist-client", | ||
"version": "4.0.11-beta.0", | ||
"version": "4.2.1", | ||
"description": "TODO", | ||
@@ -13,27 +13,14 @@ "author": "tannerlinsley", | ||
}, | ||
"module": "build/lib/index.mjs", | ||
"main": "build/lib/index.js", | ||
"module": "build/esm/index.js", | ||
"main": "build/cjs/react-query-persist-client/src/index.js", | ||
"browser": "build/umd/index.production.js", | ||
"types": "build/lib/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./build/lib/index.d.ts", | ||
"import": "./build/lib/index.mjs", | ||
"default": "./build/lib/index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"types": "build/types/packages/react-query-persist-client/src/index.d.ts", | ||
"files": [ | ||
"build/lib/*", | ||
"build/umd/*", | ||
"build/*", | ||
"src" | ||
], | ||
"scripts": { | ||
"clean": "rm -rf ./build", | ||
"test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src" | ||
}, | ||
"dependencies": { | ||
"@tanstack/query-core": "^4.0.10", | ||
"@tanstack/react-query": "^4.0.10" | ||
"test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src", | ||
"compile": "../../node_modules/.bin/tsc -p tsconfig.json --noEmit --emitDeclarationOnly false" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
697858
639.52%0
-100%71
273.68%3860
179.71%1
-50%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed