@qiaopeng/tanstack-query-plus 0.2.1 → 0.2.2
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.
package/README.md
CHANGED
|
@@ -126,6 +126,7 @@ function App() {
|
|
|
126
126
|
enableOfflineSupport={true} // 启用离线状态监听(默认 true)
|
|
127
127
|
cacheKey="my-app-cache" // 自定义缓存 key(默认 'tanstack-query-cache')
|
|
128
128
|
onPersistRestore={() => console.log('缓存已恢复')} // 缓存恢复回调
|
|
129
|
+
onPersistError={(err) => console.error('持久化错误', err)}
|
|
129
130
|
>
|
|
130
131
|
<YourApp />
|
|
131
132
|
</PersistQueryClientProvider>
|
|
@@ -150,15 +151,15 @@ function App() {
|
|
|
150
151
|
```typescript
|
|
151
152
|
{
|
|
152
153
|
queries: {
|
|
153
|
-
staleTime: 30000,
|
|
154
|
-
gcTime: 600000,
|
|
155
|
-
retry:
|
|
156
|
-
retryDelay:
|
|
157
|
-
refetchOnWindowFocus: true,
|
|
158
|
-
refetchOnReconnect: true,
|
|
154
|
+
staleTime: 30000,
|
|
155
|
+
gcTime: 600000,
|
|
156
|
+
retry: defaultQueryRetryStrategy,
|
|
157
|
+
retryDelay: exponentialBackoff,
|
|
158
|
+
refetchOnWindowFocus: true,
|
|
159
|
+
refetchOnReconnect: true,
|
|
159
160
|
},
|
|
160
161
|
mutations: {
|
|
161
|
-
retry: 0,
|
|
162
|
+
retry: 0,
|
|
162
163
|
gcTime: 600000,
|
|
163
164
|
}
|
|
164
165
|
}
|
|
@@ -2479,8 +2480,9 @@ const queueManager = createOfflineQueueManager({
|
|
|
2479
2480
|
})
|
|
2480
2481
|
|
|
2481
2482
|
// 注册 mutation 函数(用于恢复队列时执行)
|
|
2482
|
-
|
|
2483
|
-
mutationRegistry.register('
|
|
2483
|
+
// 注册函数签名为 () => Promise<unknown>,如需变量请使用闭包或在入队项的 mutationFn 捕获
|
|
2484
|
+
mutationRegistry.register('updateUser', () => updateUserAPI(savedUserData))
|
|
2485
|
+
mutationRegistry.register('createPost', () => createPostAPI(savedPostData))
|
|
2484
2486
|
|
|
2485
2487
|
// 添加操作到队列
|
|
2486
2488
|
async function handleUpdateUser(userData) {
|
|
@@ -2895,7 +2897,7 @@ const { data: adminEmails } = useQuery({
|
|
|
2895
2897
|
queryFn: fetchUsers,
|
|
2896
2898
|
select: selectors.compose(
|
|
2897
2899
|
selectors.where(u => u.role === 'admin'),
|
|
2898
|
-
selectors.
|
|
2900
|
+
selectors.map(u => u.email)
|
|
2899
2901
|
),
|
|
2900
2902
|
})
|
|
2901
2903
|
```
|
|
@@ -3260,12 +3262,14 @@ src/
|
|
|
3260
3262
|
```tsx
|
|
3261
3263
|
// queries/users.ts
|
|
3262
3264
|
import { useEnhancedQuery } from '@qiaopeng/tanstack-query-plus/hooks'
|
|
3263
|
-
import {
|
|
3265
|
+
import { createDomainKeyFactory } from '@qiaopeng/tanstack-query-plus/core'
|
|
3264
3266
|
import { fetchUser, fetchUsers } from '@/api/users'
|
|
3265
3267
|
|
|
3268
|
+
const userKeys = createDomainKeyFactory('users')
|
|
3269
|
+
|
|
3266
3270
|
export function useUser(userId: string) {
|
|
3267
3271
|
return useEnhancedQuery({
|
|
3268
|
-
queryKey:
|
|
3272
|
+
queryKey: userKeys.detail(userId),
|
|
3269
3273
|
queryFn: () => fetchUser(userId),
|
|
3270
3274
|
enabled: !!userId,
|
|
3271
3275
|
trackPerformance: true,
|
|
@@ -3274,7 +3278,7 @@ export function useUser(userId: string) {
|
|
|
3274
3278
|
|
|
3275
3279
|
export function useUsers(filters?: UserFilters) {
|
|
3276
3280
|
return useEnhancedQuery({
|
|
3277
|
-
queryKey:
|
|
3281
|
+
queryKey: userKeys.list(filters),
|
|
3278
3282
|
queryFn: () => fetchUsers(filters),
|
|
3279
3283
|
})
|
|
3280
3284
|
}
|
|
@@ -11,7 +11,7 @@ export function PersistQueryClientProvider({ children, client, cacheKey = "tanst
|
|
|
11
11
|
}
|
|
12
12
|
}, [enableOfflineSupport]);
|
|
13
13
|
if (enablePersistence) {
|
|
14
|
-
const persister = createPersister(cacheKey);
|
|
14
|
+
const persister = createPersister(cacheKey, undefined, _onPersistError);
|
|
15
15
|
if (!persister) {
|
|
16
16
|
return _jsx(QueryClientProvider, { client: client, children: children });
|
|
17
17
|
}
|
|
@@ -13,7 +13,7 @@ export declare function createPersistOptions(config?: Partial<PersistOptions>):
|
|
|
13
13
|
shouldDehydrateQuery?: (query: Query) => boolean;
|
|
14
14
|
};
|
|
15
15
|
};
|
|
16
|
-
export declare function createPersister(storageKey?: string, storage?: Storage): Persister | undefined;
|
|
16
|
+
export declare function createPersister(storageKey?: string, storage?: Storage, onError?: (error: Error) => void): Persister | undefined;
|
|
17
17
|
export declare function clearCache(key?: string): void;
|
|
18
18
|
export declare function clearExpiredCache(key?: string, maxAge?: number): void;
|
|
19
19
|
export declare function migrateToIndexedDB(localStorageKey: string | undefined, indexedDBKey: string | undefined, indexedDBStorage: Storage | {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persistence.d.ts","sourceRoot":"","sources":["../../src/features/persistence.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAsBvF,MAAM,WAAW,cAAc;IAAG,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAAC,gBAAgB,CAAC,EAAE;QAAE,oBAAoB,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAA;KAAE,CAAA;CAAE;AAC1J,wBAAgB,oBAAoB,CAAC,MAAM,GAAE,OAAO,CAAC,cAAc,CAAM,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,gBAAgB,CAAC,EAAE;QAAE,oBAAoB,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,K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E,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAWlO;AACD,wBAAgB,gBAAgB,CAAC,GAAG,SAAyB,GAAG;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CASjJ;AACD,wBAAgB,eAAe,CAAC,GAAG,SAAyB,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAA;CAAE,CAS9L;AACD,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -72,7 +72,7 @@ export function createPersistOptions(config = {}) {
|
|
|
72
72
|
}
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
|
-
export function createPersister(storageKey = "tanstack-query-cache", storage) {
|
|
75
|
+
export function createPersister(storageKey = "tanstack-query-cache", storage, onError) {
|
|
76
76
|
if (typeof window === "undefined") {
|
|
77
77
|
return undefined;
|
|
78
78
|
}
|
|
@@ -86,18 +86,29 @@ export function createPersister(storageKey = "tanstack-query-cache", storage) {
|
|
|
86
86
|
persistClient: async (client) => { try {
|
|
87
87
|
safeStorage.setItem(storageKey, JSON.stringify(client));
|
|
88
88
|
}
|
|
89
|
-
catch {
|
|
89
|
+
catch (e) {
|
|
90
|
+
if (onError && e instanceof Error) {
|
|
91
|
+
onError(e);
|
|
92
|
+
}
|
|
93
|
+
} },
|
|
90
94
|
restoreClient: async () => { try {
|
|
91
95
|
const raw = safeStorage.getItem(storageKey);
|
|
92
96
|
return raw ? JSON.parse(raw) : undefined;
|
|
93
97
|
}
|
|
94
|
-
catch {
|
|
98
|
+
catch (e) {
|
|
99
|
+
if (onError && e instanceof Error) {
|
|
100
|
+
onError(e);
|
|
101
|
+
}
|
|
95
102
|
return undefined;
|
|
96
103
|
} },
|
|
97
104
|
removeClient: async () => { try {
|
|
98
105
|
safeStorage.removeItem(storageKey);
|
|
99
106
|
}
|
|
100
|
-
catch {
|
|
107
|
+
catch (e) {
|
|
108
|
+
if (onError && e instanceof Error) {
|
|
109
|
+
onError(e);
|
|
110
|
+
}
|
|
111
|
+
} }
|
|
101
112
|
};
|
|
102
113
|
return persister;
|
|
103
114
|
}
|