@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
package/src/persist.ts ADDED
@@ -0,0 +1,165 @@
1
+ import {
2
+ QueryClient,
3
+ dehydrate,
4
+ DehydratedState,
5
+ DehydrateOptions,
6
+ HydrateOptions,
7
+ hydrate,
8
+ } from '@tanstack/query-core'
9
+
10
+ export type Promisable<T> = T | PromiseLike<T>
11
+
12
+ export interface Persister {
13
+ persistClient(persistClient: PersistedClient): Promisable<void>
14
+ restoreClient(): Promisable<PersistedClient | undefined>
15
+ removeClient(): Promisable<void>
16
+ }
17
+
18
+ export interface PersistedClient {
19
+ timestamp: number
20
+ buster: string
21
+ clientState: DehydratedState
22
+ }
23
+
24
+ export interface PersistQueryClienRootOptions {
25
+ /** The QueryClient to persist */
26
+ queryClient: QueryClient
27
+ /** The Persister interface for storing and restoring the cache
28
+ * to/from a persisted location */
29
+ persister: Persister
30
+ /** A unique string that can be used to forcefully
31
+ * invalidate existing caches if they do not share the same buster string */
32
+ buster?: string
33
+ }
34
+
35
+ export interface PersistedQueryClientRestoreOptions
36
+ extends PersistQueryClienRootOptions {
37
+ /** The max-allowed age of the cache in milliseconds.
38
+ * If a persisted cache is found that is older than this
39
+ * time, it will be discarded */
40
+ maxAge?: number
41
+ /** The options passed to the hydrate function */
42
+ hydrateOptions?: HydrateOptions
43
+ }
44
+
45
+ export interface PersistedQueryClientSaveOptions
46
+ extends PersistQueryClienRootOptions {
47
+ /** The options passed to the dehydrate function */
48
+ dehydrateOptions?: DehydrateOptions
49
+ }
50
+
51
+ export interface PersistQueryClientOptions
52
+ extends PersistedQueryClientRestoreOptions,
53
+ PersistedQueryClientSaveOptions,
54
+ PersistQueryClienRootOptions {}
55
+
56
+ /**
57
+ * Restores persisted data to the QueryCache
58
+ * - data obtained from persister.restoreClient
59
+ * - data is hydrated using hydrateOptions
60
+ * If data is expired, busted, empty, or throws, it runs persister.removeClient
61
+ */
62
+ export async function persistQueryClientRestore({
63
+ queryClient,
64
+ persister,
65
+ maxAge = 1000 * 60 * 60 * 24,
66
+ buster = '',
67
+ hydrateOptions,
68
+ }: PersistedQueryClientRestoreOptions) {
69
+ try {
70
+ const persistedClient = await persister.restoreClient()
71
+
72
+ if (persistedClient) {
73
+ if (persistedClient.timestamp) {
74
+ const expired = Date.now() - persistedClient.timestamp > maxAge
75
+ const busted = persistedClient.buster !== buster
76
+ if (expired || busted) {
77
+ persister.removeClient()
78
+ } else {
79
+ hydrate(queryClient, persistedClient.clientState, hydrateOptions)
80
+ }
81
+ } else {
82
+ persister.removeClient()
83
+ }
84
+ }
85
+ } catch (err) {
86
+ if (process.env.NODE_ENV !== 'production') {
87
+ queryClient.getLogger().error(err)
88
+ queryClient
89
+ .getLogger()
90
+ .warn(
91
+ 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',
92
+ )
93
+ }
94
+ persister.removeClient()
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Persists data from the QueryCache
100
+ * - data dehydrated using dehydrateOptions
101
+ * - data is persisted using persister.persistClient
102
+ */
103
+ export async function persistQueryClientSave({
104
+ queryClient,
105
+ persister,
106
+ buster = '',
107
+ dehydrateOptions,
108
+ }: PersistedQueryClientSaveOptions) {
109
+ const persistClient: PersistedClient = {
110
+ buster,
111
+ timestamp: Date.now(),
112
+ clientState: dehydrate(queryClient, dehydrateOptions),
113
+ }
114
+
115
+ await persister.persistClient(persistClient)
116
+ }
117
+
118
+ /**
119
+ * Subscribe to QueryCache and MutationCache updates (for persisting)
120
+ * @returns an unsubscribe function (to discontinue monitoring)
121
+ */
122
+ export function persistQueryClientSubscribe(
123
+ props: PersistedQueryClientSaveOptions,
124
+ ) {
125
+ const unsubscribeQueryCache = props.queryClient
126
+ .getQueryCache()
127
+ .subscribe(() => {
128
+ persistQueryClientSave(props)
129
+ })
130
+
131
+ const unusbscribeMutationCache = props.queryClient
132
+ .getMutationCache()
133
+ .subscribe(() => {
134
+ persistQueryClientSave(props)
135
+ })
136
+
137
+ return () => {
138
+ unsubscribeQueryCache()
139
+ unusbscribeMutationCache()
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Restores persisted data to QueryCache and persists further changes.
145
+ */
146
+ export function persistQueryClient(
147
+ props: PersistQueryClientOptions,
148
+ ): [() => void, Promise<void>] {
149
+ let hasUnsubscribed = false
150
+ let persistQueryClientUnsubscribe: (() => void) | undefined
151
+ const unsubscribe = () => {
152
+ hasUnsubscribed = true
153
+ persistQueryClientUnsubscribe?.()
154
+ }
155
+
156
+ // Attempt restore
157
+ const restorePromise = persistQueryClientRestore(props).then(() => {
158
+ if (!hasUnsubscribed) {
159
+ // Subscribe to changes in the query cache to trigger the save
160
+ persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)
161
+ }
162
+ })
163
+
164
+ return [unsubscribe, restorePromise]
165
+ }
@@ -0,0 +1,30 @@
1
+ import { PersistedClient } from './persist'
2
+
3
+ export type PersistRetryer = (props: {
4
+ persistedClient: PersistedClient
5
+ error: Error
6
+ errorCount: number
7
+ }) => PersistedClient | undefined
8
+
9
+ export const removeOldestQuery: PersistRetryer = ({ persistedClient }) => {
10
+ const mutations = [...persistedClient.clientState.mutations]
11
+ const queries = [...persistedClient.clientState.queries]
12
+ const client: PersistedClient = {
13
+ ...persistedClient,
14
+ clientState: { mutations, queries },
15
+ }
16
+
17
+ // sort queries by dataUpdatedAt (oldest first)
18
+ const sortedQueries = [...queries].sort(
19
+ (a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt,
20
+ )
21
+
22
+ // clean oldest query
23
+ if (sortedQueries.length > 0) {
24
+ const oldestData = sortedQueries.shift()
25
+ client.clientState.queries = queries.filter((q) => q !== oldestData)
26
+ return client
27
+ }
28
+
29
+ return undefined
30
+ }