@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,524 @@
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
+ (function (global, factory) {
12
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('@tanstack/react-query')) :
13
+ typeof define === 'function' && define.amd ? define(['exports', 'react', '@tanstack/react-query'], factory) :
14
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactQueryPersistClient = {}, global.React, global.reactQuery));
15
+ })(this, (function (exports, React, reactQuery) { 'use strict';
16
+
17
+ function _interopNamespace(e) {
18
+ if (e && e.__esModule) return e;
19
+ var n = Object.create(null);
20
+ if (e) {
21
+ Object.keys(e).forEach(function (k) {
22
+ if (k !== 'default') {
23
+ var d = Object.getOwnPropertyDescriptor(e, k);
24
+ Object.defineProperty(n, k, d.get ? d : {
25
+ enumerable: true,
26
+ get: function () { return e[k]; }
27
+ });
28
+ }
29
+ });
30
+ }
31
+ n["default"] = e;
32
+ return Object.freeze(n);
33
+ }
34
+
35
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
36
+
37
+ /**
38
+ * query-core
39
+ *
40
+ * Copyright (c) TanStack
41
+ *
42
+ * This source code is licensed under the MIT license found in the
43
+ * LICENSE.md file in the root directory of this source tree.
44
+ *
45
+ * @license MIT
46
+ */
47
+ class Subscribable {
48
+ constructor() {
49
+ this.listeners = [];
50
+ this.subscribe = this.subscribe.bind(this);
51
+ }
52
+
53
+ subscribe(listener) {
54
+ this.listeners.push(listener);
55
+ this.onSubscribe();
56
+ return () => {
57
+ this.listeners = this.listeners.filter(x => x !== listener);
58
+ this.onUnsubscribe();
59
+ };
60
+ }
61
+
62
+ hasListeners() {
63
+ return this.listeners.length > 0;
64
+ }
65
+
66
+ onSubscribe() {// Do nothing
67
+ }
68
+
69
+ onUnsubscribe() {// Do nothing
70
+ }
71
+
72
+ }
73
+
74
+ // TYPES
75
+ // UTILS
76
+ const isServer = typeof window === 'undefined';
77
+
78
+ class FocusManager extends Subscribable {
79
+ constructor() {
80
+ super();
81
+
82
+ this.setup = onFocus => {
83
+ // addEventListener does not exist in React Native, but window does
84
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
85
+ if (!isServer && window.addEventListener) {
86
+ const listener = () => onFocus(); // Listen to visibillitychange and focus
87
+
88
+
89
+ window.addEventListener('visibilitychange', listener, false);
90
+ window.addEventListener('focus', listener, false);
91
+ return () => {
92
+ // Be sure to unsubscribe if a new handler is set
93
+ window.removeEventListener('visibilitychange', listener);
94
+ window.removeEventListener('focus', listener);
95
+ };
96
+ }
97
+ };
98
+ }
99
+
100
+ onSubscribe() {
101
+ if (!this.cleanup) {
102
+ this.setEventListener(this.setup);
103
+ }
104
+ }
105
+
106
+ onUnsubscribe() {
107
+ if (!this.hasListeners()) {
108
+ var _this$cleanup;
109
+
110
+ (_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
111
+ this.cleanup = undefined;
112
+ }
113
+ }
114
+
115
+ setEventListener(setup) {
116
+ var _this$cleanup2;
117
+
118
+ this.setup = setup;
119
+ (_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
120
+ this.cleanup = setup(focused => {
121
+ if (typeof focused === 'boolean') {
122
+ this.setFocused(focused);
123
+ } else {
124
+ this.onFocus();
125
+ }
126
+ });
127
+ }
128
+
129
+ setFocused(focused) {
130
+ this.focused = focused;
131
+
132
+ if (focused) {
133
+ this.onFocus();
134
+ }
135
+ }
136
+
137
+ onFocus() {
138
+ this.listeners.forEach(listener => {
139
+ listener();
140
+ });
141
+ }
142
+
143
+ isFocused() {
144
+ if (typeof this.focused === 'boolean') {
145
+ return this.focused;
146
+ } // document global can be unavailable in react native
147
+
148
+
149
+ if (typeof document === 'undefined') {
150
+ return true;
151
+ }
152
+
153
+ return [undefined, 'visible', 'prerender'].includes(document.visibilityState);
154
+ }
155
+
156
+ }
157
+ new FocusManager();
158
+
159
+ class OnlineManager extends Subscribable {
160
+ constructor() {
161
+ super();
162
+
163
+ this.setup = onOnline => {
164
+ // addEventListener does not exist in React Native, but window does
165
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
166
+ if (!isServer && window.addEventListener) {
167
+ const listener = () => onOnline(); // Listen to online
168
+
169
+
170
+ window.addEventListener('online', listener, false);
171
+ window.addEventListener('offline', listener, false);
172
+ return () => {
173
+ // Be sure to unsubscribe if a new handler is set
174
+ window.removeEventListener('online', listener);
175
+ window.removeEventListener('offline', listener);
176
+ };
177
+ }
178
+ };
179
+ }
180
+
181
+ onSubscribe() {
182
+ if (!this.cleanup) {
183
+ this.setEventListener(this.setup);
184
+ }
185
+ }
186
+
187
+ onUnsubscribe() {
188
+ if (!this.hasListeners()) {
189
+ var _this$cleanup;
190
+
191
+ (_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
192
+ this.cleanup = undefined;
193
+ }
194
+ }
195
+
196
+ setEventListener(setup) {
197
+ var _this$cleanup2;
198
+
199
+ this.setup = setup;
200
+ (_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
201
+ this.cleanup = setup(online => {
202
+ if (typeof online === 'boolean') {
203
+ this.setOnline(online);
204
+ } else {
205
+ this.onOnline();
206
+ }
207
+ });
208
+ }
209
+
210
+ setOnline(online) {
211
+ this.online = online;
212
+
213
+ if (online) {
214
+ this.onOnline();
215
+ }
216
+ }
217
+
218
+ onOnline() {
219
+ this.listeners.forEach(listener => {
220
+ listener();
221
+ });
222
+ }
223
+
224
+ isOnline() {
225
+ if (typeof this.online === 'boolean') {
226
+ return this.online;
227
+ }
228
+
229
+ if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') {
230
+ return true;
231
+ }
232
+
233
+ return navigator.onLine;
234
+ }
235
+
236
+ }
237
+ new OnlineManager();
238
+
239
+ // TYPES
240
+ // FUNCTIONS
241
+ function dehydrateMutation(mutation) {
242
+ return {
243
+ mutationKey: mutation.options.mutationKey,
244
+ state: mutation.state
245
+ };
246
+ } // Most config is not dehydrated but instead meant to configure again when
247
+ // consuming the de/rehydrated data, typically with useQuery on the client.
248
+ // Sometimes it might make sense to prefetch data on the server and include
249
+ // in the html-payload, but not consume it on the initial render.
250
+
251
+
252
+ function dehydrateQuery(query) {
253
+ return {
254
+ state: query.state,
255
+ queryKey: query.queryKey,
256
+ queryHash: query.queryHash
257
+ };
258
+ }
259
+
260
+ function defaultShouldDehydrateMutation(mutation) {
261
+ return mutation.state.isPaused;
262
+ }
263
+
264
+ function defaultShouldDehydrateQuery(query) {
265
+ return query.state.status === 'success';
266
+ }
267
+
268
+ function dehydrate(client, options = {}) {
269
+ const mutations = [];
270
+ const queries = [];
271
+
272
+ if (options.dehydrateMutations !== false) {
273
+ const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation;
274
+ client.getMutationCache().getAll().forEach(mutation => {
275
+ if (shouldDehydrateMutation(mutation)) {
276
+ mutations.push(dehydrateMutation(mutation));
277
+ }
278
+ });
279
+ }
280
+
281
+ if (options.dehydrateQueries !== false) {
282
+ const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
283
+ client.getQueryCache().getAll().forEach(query => {
284
+ if (shouldDehydrateQuery(query)) {
285
+ queries.push(dehydrateQuery(query));
286
+ }
287
+ });
288
+ }
289
+
290
+ return {
291
+ mutations,
292
+ queries
293
+ };
294
+ }
295
+ function hydrate(client, dehydratedState, options) {
296
+ if (typeof dehydratedState !== 'object' || dehydratedState === null) {
297
+ return;
298
+ }
299
+
300
+ const mutationCache = client.getMutationCache();
301
+ const queryCache = client.getQueryCache(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
302
+
303
+ const mutations = dehydratedState.mutations || []; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
304
+
305
+ const queries = dehydratedState.queries || [];
306
+ mutations.forEach(dehydratedMutation => {
307
+ var _options$defaultOptio;
308
+
309
+ mutationCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio = options.defaultOptions) == null ? void 0 : _options$defaultOptio.mutations),
310
+ mutationKey: dehydratedMutation.mutationKey
311
+ }, dehydratedMutation.state);
312
+ });
313
+ queries.forEach(dehydratedQuery => {
314
+ var _options$defaultOptio2;
315
+
316
+ const query = queryCache.get(dehydratedQuery.queryHash); // Do not hydrate if an existing query exists with newer data
317
+
318
+ if (query) {
319
+ if (query.state.dataUpdatedAt < dehydratedQuery.state.dataUpdatedAt) {
320
+ query.setState(dehydratedQuery.state);
321
+ }
322
+
323
+ return;
324
+ } // Restore query
325
+
326
+
327
+ queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
328
+ queryKey: dehydratedQuery.queryKey,
329
+ queryHash: dehydratedQuery.queryHash
330
+ }, dehydratedQuery.state);
331
+ });
332
+ }
333
+
334
+ /**
335
+ * Restores persisted data to the QueryCache
336
+ * - data obtained from persister.restoreClient
337
+ * - data is hydrated using hydrateOptions
338
+ * If data is expired, busted, empty, or throws, it runs persister.removeClient
339
+ */
340
+ async function persistQueryClientRestore({
341
+ queryClient,
342
+ persister,
343
+ maxAge = 1000 * 60 * 60 * 24,
344
+ buster = '',
345
+ hydrateOptions
346
+ }) {
347
+ try {
348
+ const persistedClient = await persister.restoreClient();
349
+
350
+ if (persistedClient) {
351
+ if (persistedClient.timestamp) {
352
+ const expired = Date.now() - persistedClient.timestamp > maxAge;
353
+ const busted = persistedClient.buster !== buster;
354
+
355
+ if (expired || busted) {
356
+ persister.removeClient();
357
+ } else {
358
+ hydrate(queryClient, persistedClient.clientState, hydrateOptions);
359
+ }
360
+ } else {
361
+ persister.removeClient();
362
+ }
363
+ }
364
+ } catch (err) {
365
+ {
366
+ queryClient.getLogger().error(err);
367
+ queryClient.getLogger().warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
368
+ }
369
+
370
+ persister.removeClient();
371
+ }
372
+ }
373
+ /**
374
+ * Persists data from the QueryCache
375
+ * - data dehydrated using dehydrateOptions
376
+ * - data is persisted using persister.persistClient
377
+ */
378
+
379
+ async function persistQueryClientSave({
380
+ queryClient,
381
+ persister,
382
+ buster = '',
383
+ dehydrateOptions
384
+ }) {
385
+ const persistClient = {
386
+ buster,
387
+ timestamp: Date.now(),
388
+ clientState: dehydrate(queryClient, dehydrateOptions)
389
+ };
390
+ await persister.persistClient(persistClient);
391
+ }
392
+ /**
393
+ * Subscribe to QueryCache and MutationCache updates (for persisting)
394
+ * @returns an unsubscribe function (to discontinue monitoring)
395
+ */
396
+
397
+ function persistQueryClientSubscribe(props) {
398
+ const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(() => {
399
+ persistQueryClientSave(props);
400
+ });
401
+ const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(() => {
402
+ persistQueryClientSave(props);
403
+ });
404
+ return () => {
405
+ unsubscribeQueryCache();
406
+ unusbscribeMutationCache();
407
+ };
408
+ }
409
+ /**
410
+ * Restores persisted data to QueryCache and persists further changes.
411
+ */
412
+
413
+ function persistQueryClient(props) {
414
+ let hasUnsubscribed = false;
415
+ let persistQueryClientUnsubscribe;
416
+
417
+ const unsubscribe = () => {
418
+ hasUnsubscribed = true;
419
+ persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
420
+ }; // Attempt restore
421
+
422
+
423
+ const restorePromise = persistQueryClientRestore(props).then(() => {
424
+ if (!hasUnsubscribed) {
425
+ // Subscribe to changes in the query cache to trigger the save
426
+ persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
427
+ }
428
+ });
429
+ return [unsubscribe, restorePromise];
430
+ }
431
+
432
+ function _extends() {
433
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
434
+ for (var i = 1; i < arguments.length; i++) {
435
+ var source = arguments[i];
436
+
437
+ for (var key in source) {
438
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
439
+ target[key] = source[key];
440
+ }
441
+ }
442
+ }
443
+
444
+ return target;
445
+ };
446
+ return _extends.apply(this, arguments);
447
+ }
448
+
449
+ const PersistQueryClientProvider = ({
450
+ client,
451
+ children,
452
+ persistOptions,
453
+ onSuccess,
454
+ ...props
455
+ }) => {
456
+ const [isRestoring, setIsRestoring] = React__namespace.useState(true);
457
+ const refs = React__namespace.useRef({
458
+ persistOptions,
459
+ onSuccess
460
+ });
461
+ React__namespace.useEffect(() => {
462
+ refs.current = {
463
+ persistOptions,
464
+ onSuccess
465
+ };
466
+ });
467
+ React__namespace.useEffect(() => {
468
+ let isStale = false;
469
+ setIsRestoring(true);
470
+ const [unsubscribe, promise] = persistQueryClient({ ...refs.current.persistOptions,
471
+ queryClient: client
472
+ });
473
+ promise.then(() => {
474
+ if (!isStale) {
475
+ refs.current.onSuccess == null ? void 0 : refs.current.onSuccess();
476
+ setIsRestoring(false);
477
+ }
478
+ });
479
+ return () => {
480
+ isStale = true;
481
+ unsubscribe();
482
+ };
483
+ }, [client]);
484
+ return /*#__PURE__*/React__namespace.createElement(reactQuery.QueryClientProvider, _extends({
485
+ client: client
486
+ }, props), /*#__PURE__*/React__namespace.createElement(reactQuery.IsRestoringProvider, {
487
+ value: isRestoring
488
+ }, children));
489
+ };
490
+
491
+ const removeOldestQuery = ({
492
+ persistedClient
493
+ }) => {
494
+ const mutations = [...persistedClient.clientState.mutations];
495
+ const queries = [...persistedClient.clientState.queries];
496
+ const client = { ...persistedClient,
497
+ clientState: {
498
+ mutations,
499
+ queries
500
+ }
501
+ }; // sort queries by dataUpdatedAt (oldest first)
502
+
503
+ const sortedQueries = [...queries].sort((a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt); // clean oldest query
504
+
505
+ if (sortedQueries.length > 0) {
506
+ const oldestData = sortedQueries.shift();
507
+ client.clientState.queries = queries.filter(q => q !== oldestData);
508
+ return client;
509
+ }
510
+
511
+ return undefined;
512
+ };
513
+
514
+ exports.PersistQueryClientProvider = PersistQueryClientProvider;
515
+ exports.persistQueryClient = persistQueryClient;
516
+ exports.persistQueryClientRestore = persistQueryClientRestore;
517
+ exports.persistQueryClientSave = persistQueryClientSave;
518
+ exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
519
+ exports.removeOldestQuery = removeOldestQuery;
520
+
521
+ Object.defineProperty(exports, '__esModule', { value: true });
522
+
523
+ }));
524
+ //# sourceMappingURL=index.development.js.map