@tanstack/react-query-persist-client 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/build/cjs/PersistQueryClientProvider.js +83 -0
  2. package/build/cjs/PersistQueryClientProvider.js.map +1 -0
  3. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +33 -0
  4. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
  5. package/build/cjs/index.js +27 -0
  6. package/build/cjs/index.js.map +1 -0
  7. package/build/cjs/persist.js +119 -0
  8. package/build/cjs/persist.js.map +1 -0
  9. package/build/cjs/query-core/build/esm/index.js +314 -0
  10. package/build/cjs/query-core/build/esm/index.js.map +1 -0
  11. package/build/cjs/react-query-persist-client/src/PersistQueryClientProvider.js +83 -0
  12. package/build/cjs/react-query-persist-client/src/PersistQueryClientProvider.js.map +1 -0
  13. package/build/cjs/react-query-persist-client/src/index.js +27 -0
  14. package/build/cjs/react-query-persist-client/src/index.js.map +1 -0
  15. package/build/cjs/react-query-persist-client/src/persist.js +119 -0
  16. package/build/cjs/react-query-persist-client/src/persist.js.map +1 -0
  17. package/build/cjs/react-query-persist-client/src/retryStrategies.js +39 -0
  18. package/build/cjs/react-query-persist-client/src/retryStrategies.js.map +1 -0
  19. package/build/cjs/retryStrategies.js +39 -0
  20. package/build/cjs/retryStrategies.js.map +1 -0
  21. package/build/esm/index.js +492 -0
  22. package/build/esm/index.js.map +1 -0
  23. package/build/stats-html.html +2689 -0
  24. package/build/umd/index.development.js +524 -0
  25. package/build/umd/index.development.js.map +1 -0
  26. package/build/umd/index.production.js +22 -0
  27. package/build/umd/index.production.js.map +1 -0
  28. package/package.json +25 -0
  29. package/src/PersistQueryClientProvider.tsx +55 -0
  30. package/src/__tests__/PersistQueryClientProvider.test.tsx +538 -0
  31. package/src/__tests__/persist.test.tsx +48 -0
  32. package/src/index.ts +3 -0
  33. package/src/persist.ts +165 -0
  34. package/src/retryStrategies.ts +30 -0
@@ -0,0 +1,83 @@
1
+ /**
2
+ * react-query-persist-client
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ 'use strict';
12
+
13
+ Object.defineProperty(exports, '__esModule', { value: true });
14
+
15
+ var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
16
+ var React = require('react');
17
+ var persist = require('./persist.js');
18
+ var reactQuery = require('@tanstack/react-query');
19
+
20
+ function _interopNamespace(e) {
21
+ if (e && e.__esModule) return e;
22
+ var n = Object.create(null);
23
+ if (e) {
24
+ Object.keys(e).forEach(function (k) {
25
+ if (k !== 'default') {
26
+ var d = Object.getOwnPropertyDescriptor(e, k);
27
+ Object.defineProperty(n, k, d.get ? d : {
28
+ enumerable: true,
29
+ get: function () { return e[k]; }
30
+ });
31
+ }
32
+ });
33
+ }
34
+ n["default"] = e;
35
+ return Object.freeze(n);
36
+ }
37
+
38
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
39
+
40
+ const PersistQueryClientProvider = ({
41
+ client,
42
+ children,
43
+ persistOptions,
44
+ onSuccess,
45
+ ...props
46
+ }) => {
47
+ const [isRestoring, setIsRestoring] = React__namespace.useState(true);
48
+ const refs = React__namespace.useRef({
49
+ persistOptions,
50
+ onSuccess
51
+ });
52
+ React__namespace.useEffect(() => {
53
+ refs.current = {
54
+ persistOptions,
55
+ onSuccess
56
+ };
57
+ });
58
+ React__namespace.useEffect(() => {
59
+ let isStale = false;
60
+ setIsRestoring(true);
61
+ const [unsubscribe, promise] = persist.persistQueryClient({ ...refs.current.persistOptions,
62
+ queryClient: client
63
+ });
64
+ promise.then(() => {
65
+ if (!isStale) {
66
+ refs.current.onSuccess == null ? void 0 : refs.current.onSuccess();
67
+ setIsRestoring(false);
68
+ }
69
+ });
70
+ return () => {
71
+ isStale = true;
72
+ unsubscribe();
73
+ };
74
+ }, [client]);
75
+ return /*#__PURE__*/React__namespace.createElement(reactQuery.QueryClientProvider, _rollupPluginBabelHelpers["extends"]({
76
+ client: client
77
+ }, props), /*#__PURE__*/React__namespace.createElement(reactQuery.IsRestoringProvider, {
78
+ value: isRestoring
79
+ }, children));
80
+ };
81
+
82
+ exports.PersistQueryClientProvider = PersistQueryClientProvider;
83
+ //# sourceMappingURL=PersistQueryClientProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PersistQueryClientProvider.js","sources":["../../src/PersistQueryClientProvider.tsx"],"sourcesContent":["import * as React from 'react'\n\nimport { persistQueryClient, PersistQueryClientOptions } from './persist'\nimport { QueryClientProvider, QueryClientProviderProps, IsRestoringProvider } from '@tanstack/react-query'\n\nexport type PersistQueryClientProviderProps = QueryClientProviderProps & {\n persistOptions: Omit<PersistQueryClientOptions, 'queryClient'>\n onSuccess?: () => void\n}\n\nexport const PersistQueryClientProvider = ({\n client,\n children,\n persistOptions,\n onSuccess,\n ...props\n}: PersistQueryClientProviderProps): JSX.Element => {\n const [isRestoring, setIsRestoring] = React.useState(true)\n const refs = React.useRef({ persistOptions, onSuccess })\n\n React.useEffect(() => {\n refs.current = { persistOptions, onSuccess }\n })\n\n React.useEffect(() => {\n let isStale = false\n setIsRestoring(true)\n const [unsubscribe, promise] = persistQueryClient({\n ...refs.current.persistOptions,\n queryClient: client,\n })\n\n promise.then(() => {\n if (!isStale) {\n refs.current.onSuccess?.()\n setIsRestoring(false)\n }\n })\n\n return () => {\n isStale = true\n unsubscribe()\n }\n }, [client])\n\n return (\n <QueryClientProvider client={client} {...props}>\n <IsRestoringProvider value={isRestoring}>{children}</IsRestoringProvider>\n </QueryClientProvider>\n )\n}\n"],"names":["PersistQueryClientProvider","client","children","persistOptions","onSuccess","props","isRestoring","setIsRestoring","React","useState","refs","useRef","useEffect","current","isStale","unsubscribe","promise","persistQueryClient","queryClient","then","QueryClientProvider","_extends","IsRestoringProvider"],"mappings":";;;;;;;;;;;;;;;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,MAAAA,WAAW,EAAA,CAAA;AACZ,KAHD,CAAA;AAID,GAnBD,EAmBG,CAACd,MAAD,CAnBH,CAAA,CAAA;AAqBA,EAAA,oBACEO,+BAACY,8BAAD,EAAAC,oCAAA,CAAA;AAAqB,IAAA,MAAM,EAAEpB,MAAAA;AAA7B,GAAyCI,EAAAA,KAAzC,CACE,eAAAG,gBAAA,CAAA,aAAA,CAACc,8BAAD,EAAA;AAAqB,IAAA,KAAK,EAAEhB,WAAAA;AAA5B,GAA0CJ,EAAAA,QAA1C,CADF,CADF,CAAA;AAKD;;;;"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * react-query-persist-client
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ 'use strict';
12
+
13
+ Object.defineProperty(exports, '__esModule', { value: true });
14
+
15
+ function _extends() {
16
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
17
+ for (var i = 1; i < arguments.length; i++) {
18
+ var source = arguments[i];
19
+
20
+ for (var key in source) {
21
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
22
+ target[key] = source[key];
23
+ }
24
+ }
25
+ }
26
+
27
+ return target;
28
+ };
29
+ return _extends.apply(this, arguments);
30
+ }
31
+
32
+ exports["extends"] = _extends;
33
+ //# sourceMappingURL=_rollupPluginBabelHelpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_rollupPluginBabelHelpers.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * react-query-persist-client
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ 'use strict';
12
+
13
+ Object.defineProperty(exports, '__esModule', { value: true });
14
+
15
+ var persist = require('./persist.js');
16
+ var PersistQueryClientProvider = require('./PersistQueryClientProvider.js');
17
+ var retryStrategies = require('./retryStrategies.js');
18
+
19
+
20
+
21
+ exports.persistQueryClient = persist.persistQueryClient;
22
+ exports.persistQueryClientRestore = persist.persistQueryClientRestore;
23
+ exports.persistQueryClientSave = persist.persistQueryClientSave;
24
+ exports.persistQueryClientSubscribe = persist.persistQueryClientSubscribe;
25
+ exports.PersistQueryClientProvider = PersistQueryClientProvider.PersistQueryClientProvider;
26
+ exports.removeOldestQuery = retryStrategies.removeOldestQuery;
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,119 @@
1
+ /**
2
+ * react-query-persist-client
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ 'use strict';
12
+
13
+ Object.defineProperty(exports, '__esModule', { value: true });
14
+
15
+ var queryCore = require('@tanstack/query-core');
16
+
17
+ /**
18
+ * Restores persisted data to the QueryCache
19
+ * - data obtained from persister.restoreClient
20
+ * - data is hydrated using hydrateOptions
21
+ * If data is expired, busted, empty, or throws, it runs persister.removeClient
22
+ */
23
+ async function persistQueryClientRestore({
24
+ queryClient,
25
+ persister,
26
+ maxAge = 1000 * 60 * 60 * 24,
27
+ buster = '',
28
+ hydrateOptions
29
+ }) {
30
+ try {
31
+ const persistedClient = await persister.restoreClient();
32
+
33
+ if (persistedClient) {
34
+ if (persistedClient.timestamp) {
35
+ const expired = Date.now() - persistedClient.timestamp > maxAge;
36
+ const busted = persistedClient.buster !== buster;
37
+
38
+ if (expired || busted) {
39
+ persister.removeClient();
40
+ } else {
41
+ queryCore.hydrate(queryClient, persistedClient.clientState, hydrateOptions);
42
+ }
43
+ } else {
44
+ persister.removeClient();
45
+ }
46
+ }
47
+ } catch (err) {
48
+ if (process.env.NODE_ENV !== 'production') {
49
+ queryClient.getLogger().error(err);
50
+ queryClient.getLogger().warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
51
+ }
52
+
53
+ persister.removeClient();
54
+ }
55
+ }
56
+ /**
57
+ * Persists data from the QueryCache
58
+ * - data dehydrated using dehydrateOptions
59
+ * - data is persisted using persister.persistClient
60
+ */
61
+
62
+ async function persistQueryClientSave({
63
+ queryClient,
64
+ persister,
65
+ buster = '',
66
+ dehydrateOptions
67
+ }) {
68
+ const persistClient = {
69
+ buster,
70
+ timestamp: Date.now(),
71
+ clientState: queryCore.dehydrate(queryClient, dehydrateOptions)
72
+ };
73
+ await persister.persistClient(persistClient);
74
+ }
75
+ /**
76
+ * Subscribe to QueryCache and MutationCache updates (for persisting)
77
+ * @returns an unsubscribe function (to discontinue monitoring)
78
+ */
79
+
80
+ function persistQueryClientSubscribe(props) {
81
+ const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(() => {
82
+ persistQueryClientSave(props);
83
+ });
84
+ const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(() => {
85
+ persistQueryClientSave(props);
86
+ });
87
+ return () => {
88
+ unsubscribeQueryCache();
89
+ unusbscribeMutationCache();
90
+ };
91
+ }
92
+ /**
93
+ * Restores persisted data to QueryCache and persists further changes.
94
+ */
95
+
96
+ function persistQueryClient(props) {
97
+ let hasUnsubscribed = false;
98
+ let persistQueryClientUnsubscribe;
99
+
100
+ const unsubscribe = () => {
101
+ hasUnsubscribed = true;
102
+ persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
103
+ }; // Attempt restore
104
+
105
+
106
+ const restorePromise = persistQueryClientRestore(props).then(() => {
107
+ if (!hasUnsubscribed) {
108
+ // Subscribe to changes in the query cache to trigger the save
109
+ persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
110
+ }
111
+ });
112
+ return [unsubscribe, restorePromise];
113
+ }
114
+
115
+ exports.persistQueryClient = persistQueryClient;
116
+ exports.persistQueryClientRestore = persistQueryClientRestore;
117
+ exports.persistQueryClientSave = persistQueryClientSave;
118
+ exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
119
+ //# sourceMappingURL=persist.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persist.js","sources":["../../src/persist.ts"],"sourcesContent":["import {\n QueryClient,\n dehydrate,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n hydrate,\n} from '@tanstack/query-core'\n\nexport type Promisable<T> = T | PromiseLike<T>\n\nexport interface Persister {\n persistClient(persistClient: PersistedClient): Promisable<void>\n restoreClient(): Promisable<PersistedClient | undefined>\n removeClient(): Promisable<void>\n}\n\nexport interface PersistedClient {\n timestamp: number\n buster: string\n clientState: DehydratedState\n}\n\nexport interface PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\n /** The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded */\n maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\n\n/**\n * Restores persisted data to the QueryCache\n * - data obtained from persister.restoreClient\n * - data is hydrated using hydrateOptions\n * If data is expired, busted, empty, or throws, it runs persister.removeClient\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n queryClient.getLogger().error(err)\n queryClient\n .getLogger()\n .warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.'\n )\n }\n persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\n */\nexport async function persistQueryClientSave({\n queryClient,\n persister,\n buster = '',\n dehydrateOptions,\n}: PersistedQueryClientSaveOptions) {\n const persistClient: PersistedClient = {\n buster,\n timestamp: Date.now(),\n clientState: dehydrate(queryClient, dehydrateOptions),\n }\n\n await persister.persistClient(persistClient)\n}\n\n/**\n * Subscribe to QueryCache and MutationCache updates (for persisting)\n * @returns an unsubscribe function (to discontinue monitoring)\n */\nexport function persistQueryClientSubscribe(\n props: PersistedQueryClientSaveOptions\n) {\n const unsubscribeQueryCache = props.queryClient\n .getQueryCache()\n .subscribe(() => {\n persistQueryClientSave(props)\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe(() => {\n persistQueryClientSave(props)\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n"],"names":["persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","getLogger","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":";;;;;;;;;;;;;;;;AAuDA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeA,yBAAf,CAAyC;AAC9CC,EAAAA,WAD8C;AAE9CC,EAAAA,SAF8C;AAG9CC,EAAAA,MAAM,GAAG,IAAA,GAAO,EAAP,GAAY,EAAZ,GAAiB,EAHoB;AAI9CC,EAAAA,MAAM,GAAG,EAJqC;AAK9CC,EAAAA,cAAAA;AAL8C,CAAzC,EAMgC;AACrC,EAAI,IAAA;AACF,IAAA,MAAMC,eAAe,GAAG,MAAMJ,SAAS,CAACK,aAAV,EAA9B,CAAA;;AAEA,IAAA,IAAID,eAAJ,EAAqB;AACnB,MAAIA,IAAAA,eAAe,CAACE,SAApB,EAA+B;AAC7B,QAAMC,MAAAA,OAAO,GAAGC,IAAI,CAACC,GAAL,KAAaL,eAAe,CAACE,SAA7B,GAAyCL,MAAzD,CAAA;AACA,QAAA,MAAMS,MAAM,GAAGN,eAAe,CAACF,MAAhB,KAA2BA,MAA1C,CAAA;;AACA,QAAIK,IAAAA,OAAO,IAAIG,MAAf,EAAuB;AACrBV,UAAAA,SAAS,CAACW,YAAV,EAAA,CAAA;AACD,SAFD,MAEO;AACLC,UAAAA,iBAAO,CAACb,WAAD,EAAcK,eAAe,CAACS,WAA9B,EAA2CV,cAA3C,CAAP,CAAA;AACD,SAA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pB,MAAM;AACfR,IAAAA,sBAAsB,CAACK,KAAD,CAAtB,CAAA;AACD,GAJ8B,CAAjC,CAAA;AAMA,EAAA,OAAO,MAAM;AACXC,IAAAA,qBAAqB,EAAA,CAAA;AACrBG,IAAAA,wBAAwB,EAAA,CAAA;AACzB,GAHD,CAAA;AAID,CAAA;AAED;AACA;AACA;;AACO,SAASE,kBAAT,CACLN,KADK,EAEwB;AAC7B,EAAIO,IAAAA,eAAe,GAAG,KAAtB,CAAA;AACA,EAAA,IAAIC,6BAAJ,CAAA;;AACA,EAAMC,MAAAA,WAAW,GAAG,MAAM;AACxBF,IAAAA,eAAe,GAAG,IAAlB,CAAA;AACAC,IAAAA,6BAA6B,IAA7B,IAAA,GAAA,KAAA,CAAA,GAAAA,6BAA6B,EAAA,CAAA;AAC9B,GAHD,CAH6B;;;AAS7B,EAAME,MAAAA,cAAc,GAAGtC,yBAAyB,CAAC4B,KAAD,CAAzB,CAAiCW,IAAjC,CAAsC,MAAM;AACjE,IAAI,IAAA,CAACJ,eAAL,EAAsB;AACpB;AACAC,MAAAA,6BAA6B,GAAGT,2BAA2B,CAACC,KAAD,CAA3D,CAAA;AACD,KAAA;AACF,GALsB,CAAvB,CAAA;AAOA,EAAA,OAAO,CAACS,WAAD,EAAcC,cAAd,CAAP,CAAA;AACD;;;;;;;"}
@@ -0,0 +1,314 @@
1
+ /**
2
+ * react-query-persist-client
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ 'use strict';
12
+
13
+ Object.defineProperty(exports, '__esModule', { value: true });
14
+
15
+ /**
16
+ * query-core
17
+ *
18
+ * Copyright (c) TanStack
19
+ *
20
+ * This source code is licensed under the MIT license found in the
21
+ * LICENSE.md file in the root directory of this source tree.
22
+ *
23
+ * @license MIT
24
+ */
25
+ class Subscribable {
26
+ constructor() {
27
+ this.listeners = [];
28
+ this.subscribe = this.subscribe.bind(this);
29
+ }
30
+
31
+ subscribe(listener) {
32
+ this.listeners.push(listener);
33
+ this.onSubscribe();
34
+ return () => {
35
+ this.listeners = this.listeners.filter(x => x !== listener);
36
+ this.onUnsubscribe();
37
+ };
38
+ }
39
+
40
+ hasListeners() {
41
+ return this.listeners.length > 0;
42
+ }
43
+
44
+ onSubscribe() {// Do nothing
45
+ }
46
+
47
+ onUnsubscribe() {// Do nothing
48
+ }
49
+
50
+ }
51
+
52
+ // TYPES
53
+ // UTILS
54
+ const isServer = typeof window === 'undefined';
55
+
56
+ class FocusManager extends Subscribable {
57
+ constructor() {
58
+ super();
59
+
60
+ this.setup = onFocus => {
61
+ // addEventListener does not exist in React Native, but window does
62
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
63
+ if (!isServer && window.addEventListener) {
64
+ const listener = () => onFocus(); // Listen to visibillitychange and focus
65
+
66
+
67
+ window.addEventListener('visibilitychange', listener, false);
68
+ window.addEventListener('focus', listener, false);
69
+ return () => {
70
+ // Be sure to unsubscribe if a new handler is set
71
+ window.removeEventListener('visibilitychange', listener);
72
+ window.removeEventListener('focus', listener);
73
+ };
74
+ }
75
+ };
76
+ }
77
+
78
+ onSubscribe() {
79
+ if (!this.cleanup) {
80
+ this.setEventListener(this.setup);
81
+ }
82
+ }
83
+
84
+ onUnsubscribe() {
85
+ if (!this.hasListeners()) {
86
+ var _this$cleanup;
87
+
88
+ (_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
89
+ this.cleanup = undefined;
90
+ }
91
+ }
92
+
93
+ setEventListener(setup) {
94
+ var _this$cleanup2;
95
+
96
+ this.setup = setup;
97
+ (_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
98
+ this.cleanup = setup(focused => {
99
+ if (typeof focused === 'boolean') {
100
+ this.setFocused(focused);
101
+ } else {
102
+ this.onFocus();
103
+ }
104
+ });
105
+ }
106
+
107
+ setFocused(focused) {
108
+ this.focused = focused;
109
+
110
+ if (focused) {
111
+ this.onFocus();
112
+ }
113
+ }
114
+
115
+ onFocus() {
116
+ this.listeners.forEach(listener => {
117
+ listener();
118
+ });
119
+ }
120
+
121
+ isFocused() {
122
+ if (typeof this.focused === 'boolean') {
123
+ return this.focused;
124
+ } // document global can be unavailable in react native
125
+
126
+
127
+ if (typeof document === 'undefined') {
128
+ return true;
129
+ }
130
+
131
+ return [undefined, 'visible', 'prerender'].includes(document.visibilityState);
132
+ }
133
+
134
+ }
135
+ new FocusManager();
136
+
137
+ class OnlineManager extends Subscribable {
138
+ constructor() {
139
+ super();
140
+
141
+ this.setup = onOnline => {
142
+ // addEventListener does not exist in React Native, but window does
143
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
144
+ if (!isServer && window.addEventListener) {
145
+ const listener = () => onOnline(); // Listen to online
146
+
147
+
148
+ window.addEventListener('online', listener, false);
149
+ window.addEventListener('offline', listener, false);
150
+ return () => {
151
+ // Be sure to unsubscribe if a new handler is set
152
+ window.removeEventListener('online', listener);
153
+ window.removeEventListener('offline', listener);
154
+ };
155
+ }
156
+ };
157
+ }
158
+
159
+ onSubscribe() {
160
+ if (!this.cleanup) {
161
+ this.setEventListener(this.setup);
162
+ }
163
+ }
164
+
165
+ onUnsubscribe() {
166
+ if (!this.hasListeners()) {
167
+ var _this$cleanup;
168
+
169
+ (_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
170
+ this.cleanup = undefined;
171
+ }
172
+ }
173
+
174
+ setEventListener(setup) {
175
+ var _this$cleanup2;
176
+
177
+ this.setup = setup;
178
+ (_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
179
+ this.cleanup = setup(online => {
180
+ if (typeof online === 'boolean') {
181
+ this.setOnline(online);
182
+ } else {
183
+ this.onOnline();
184
+ }
185
+ });
186
+ }
187
+
188
+ setOnline(online) {
189
+ this.online = online;
190
+
191
+ if (online) {
192
+ this.onOnline();
193
+ }
194
+ }
195
+
196
+ onOnline() {
197
+ this.listeners.forEach(listener => {
198
+ listener();
199
+ });
200
+ }
201
+
202
+ isOnline() {
203
+ if (typeof this.online === 'boolean') {
204
+ return this.online;
205
+ }
206
+
207
+ if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') {
208
+ return true;
209
+ }
210
+
211
+ return navigator.onLine;
212
+ }
213
+
214
+ }
215
+ new OnlineManager();
216
+
217
+ // TYPES
218
+ // FUNCTIONS
219
+ function dehydrateMutation(mutation) {
220
+ return {
221
+ mutationKey: mutation.options.mutationKey,
222
+ state: mutation.state
223
+ };
224
+ } // Most config is not dehydrated but instead meant to configure again when
225
+ // consuming the de/rehydrated data, typically with useQuery on the client.
226
+ // Sometimes it might make sense to prefetch data on the server and include
227
+ // in the html-payload, but not consume it on the initial render.
228
+
229
+
230
+ function dehydrateQuery(query) {
231
+ return {
232
+ state: query.state,
233
+ queryKey: query.queryKey,
234
+ queryHash: query.queryHash
235
+ };
236
+ }
237
+
238
+ function defaultShouldDehydrateMutation(mutation) {
239
+ return mutation.state.isPaused;
240
+ }
241
+
242
+ function defaultShouldDehydrateQuery(query) {
243
+ return query.state.status === 'success';
244
+ }
245
+
246
+ function dehydrate(client, options = {}) {
247
+ const mutations = [];
248
+ const queries = [];
249
+
250
+ if (options.dehydrateMutations !== false) {
251
+ const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation;
252
+ client.getMutationCache().getAll().forEach(mutation => {
253
+ if (shouldDehydrateMutation(mutation)) {
254
+ mutations.push(dehydrateMutation(mutation));
255
+ }
256
+ });
257
+ }
258
+
259
+ if (options.dehydrateQueries !== false) {
260
+ const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
261
+ client.getQueryCache().getAll().forEach(query => {
262
+ if (shouldDehydrateQuery(query)) {
263
+ queries.push(dehydrateQuery(query));
264
+ }
265
+ });
266
+ }
267
+
268
+ return {
269
+ mutations,
270
+ queries
271
+ };
272
+ }
273
+ function hydrate(client, dehydratedState, options) {
274
+ if (typeof dehydratedState !== 'object' || dehydratedState === null) {
275
+ return;
276
+ }
277
+
278
+ const mutationCache = client.getMutationCache();
279
+ const queryCache = client.getQueryCache(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
280
+
281
+ const mutations = dehydratedState.mutations || []; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
282
+
283
+ const queries = dehydratedState.queries || [];
284
+ mutations.forEach(dehydratedMutation => {
285
+ var _options$defaultOptio;
286
+
287
+ mutationCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio = options.defaultOptions) == null ? void 0 : _options$defaultOptio.mutations),
288
+ mutationKey: dehydratedMutation.mutationKey
289
+ }, dehydratedMutation.state);
290
+ });
291
+ queries.forEach(dehydratedQuery => {
292
+ var _options$defaultOptio2;
293
+
294
+ const query = queryCache.get(dehydratedQuery.queryHash); // Do not hydrate if an existing query exists with newer data
295
+
296
+ if (query) {
297
+ if (query.state.dataUpdatedAt < dehydratedQuery.state.dataUpdatedAt) {
298
+ query.setState(dehydratedQuery.state);
299
+ }
300
+
301
+ return;
302
+ } // Restore query
303
+
304
+
305
+ queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
306
+ queryKey: dehydratedQuery.queryKey,
307
+ queryHash: dehydratedQuery.queryHash
308
+ }, dehydratedQuery.state);
309
+ });
310
+ }
311
+
312
+ exports.dehydrate = dehydrate;
313
+ exports.hydrate = hydrate;
314
+ //# sourceMappingURL=index.js.map