floppy-disk 2.5.0-beta.1 → 2.5.0-beta.3
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 +4 -4
- package/esm/preact/create-mutation.d.ts +4 -4
- package/esm/preact/create-mutation.js +6 -6
- package/esm/preact/create-query.d.ts +50 -38
- package/esm/preact/create-query.js +21 -23
- package/esm/preact/create-stores.d.ts +1 -2
- package/esm/react/create-mutation.d.ts +4 -4
- package/esm/react/create-mutation.js +6 -6
- package/esm/react/create-query.d.ts +50 -38
- package/esm/react/create-query.js +21 -23
- package/esm/react/create-stores.d.ts +1 -2
- package/esm/utils/index.d.ts +1 -0
- package/lib/preact/create-mutation.d.ts +4 -4
- package/lib/preact/create-mutation.js +6 -6
- package/lib/preact/create-query.d.ts +50 -38
- package/lib/preact/create-query.js +21 -23
- package/lib/preact/create-stores.d.ts +1 -2
- package/lib/react/create-mutation.d.ts +4 -4
- package/lib/react/create-mutation.js +6 -6
- package/lib/react/create-query.d.ts +50 -38
- package/lib/react/create-query.js +21 -23
- package/lib/react/create-stores.d.ts +1 -2
- package/lib/utils/index.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
useInfiniteQuery,
|
|
19
19
|
useMutation,
|
|
20
20
|
} from '@tanstack/react-query'; // 41 kB (gzipped: 11 kB)
|
|
21
|
-
import { createQuery, createMutation } from 'floppy-disk'; //
|
|
21
|
+
import { createQuery, createMutation } from 'floppy-disk'; // 9.2 kB (gzipped: 3.1 kB) 🎉
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
- Using Zustand & React-Query: https://demo-zustand-react-query.vercel.app/
|
|
@@ -641,7 +641,7 @@ const usePokemonsInfQuery = createQuery(
|
|
|
641
641
|
throw res;
|
|
642
642
|
},
|
|
643
643
|
{
|
|
644
|
-
select: (response, { data }) => [...
|
|
644
|
+
select: (response, { data = [] }) => [...data, ...response.results],
|
|
645
645
|
getNextPageParam: (lastPageResponse, i) => {
|
|
646
646
|
if (i > 5) return undefined; // Return undefined means you have reached the end of the pages
|
|
647
647
|
return i * 10;
|
|
@@ -650,11 +650,11 @@ const usePokemonsInfQuery = createQuery(
|
|
|
650
650
|
);
|
|
651
651
|
|
|
652
652
|
function PokemonListPage() {
|
|
653
|
-
const { data, fetchNextPage, hasNextPage, isWaitingNextPage } = usePokemonsInfQuery();
|
|
653
|
+
const { data = [], fetchNextPage, hasNextPage, isWaitingNextPage } = usePokemonsInfQuery();
|
|
654
654
|
|
|
655
655
|
return (
|
|
656
656
|
<div>
|
|
657
|
-
{data
|
|
657
|
+
{data.map((pokemon) => (
|
|
658
658
|
<div key={pokemon.name}>{pokemon.name}</div>
|
|
659
659
|
))}
|
|
660
660
|
{isWaitingNextPage ? (
|
|
@@ -7,10 +7,10 @@ export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
|
7
7
|
isWaiting: boolean;
|
|
8
8
|
isSuccess: boolean;
|
|
9
9
|
isError: boolean;
|
|
10
|
-
response: TResponse |
|
|
11
|
-
responseUpdatedAt: number |
|
|
12
|
-
error: TError |
|
|
13
|
-
errorUpdatedAt: number |
|
|
10
|
+
response: TResponse | undefined;
|
|
11
|
+
responseUpdatedAt: number | undefined;
|
|
12
|
+
error: TError | undefined;
|
|
13
|
+
errorUpdatedAt: number | undefined;
|
|
14
14
|
/**
|
|
15
15
|
* Mutate function.
|
|
16
16
|
*
|
|
@@ -9,10 +9,10 @@ export const createMutation = (mutationFn, options = {}) => {
|
|
|
9
9
|
isWaiting: false,
|
|
10
10
|
isSuccess: false,
|
|
11
11
|
isError: false,
|
|
12
|
-
response:
|
|
13
|
-
responseUpdatedAt:
|
|
14
|
-
error:
|
|
15
|
-
errorUpdatedAt:
|
|
12
|
+
response: undefined,
|
|
13
|
+
responseUpdatedAt: undefined,
|
|
14
|
+
error: undefined,
|
|
15
|
+
errorUpdatedAt: undefined,
|
|
16
16
|
mutate: ((variables) => {
|
|
17
17
|
set({ isWaiting: true });
|
|
18
18
|
const stateBeforeMutate = get();
|
|
@@ -26,8 +26,8 @@ export const createMutation = (mutationFn, options = {}) => {
|
|
|
26
26
|
isError: false,
|
|
27
27
|
response,
|
|
28
28
|
responseUpdatedAt: Date.now(),
|
|
29
|
-
error:
|
|
30
|
-
errorUpdatedAt:
|
|
29
|
+
error: undefined,
|
|
30
|
+
errorUpdatedAt: undefined,
|
|
31
31
|
});
|
|
32
32
|
onSuccess(response, variables, stateBeforeMutate);
|
|
33
33
|
resolve({ response, variables });
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { FunctionComponent } from 'preact';
|
|
3
|
+
import { Maybe } from '../utils';
|
|
3
4
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
4
|
-
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
5
|
+
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = {
|
|
5
6
|
/**
|
|
6
7
|
* Query store key, an object that will be hashed into a string as a query store identifier.
|
|
7
8
|
*/
|
|
@@ -19,17 +20,17 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
19
20
|
*
|
|
20
21
|
* @returns Promise that will always get resolved.
|
|
21
22
|
*/
|
|
22
|
-
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
|
|
23
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
23
24
|
/**
|
|
24
25
|
* Fetch next page if has next page.
|
|
25
26
|
*
|
|
26
27
|
* If the data is empty, it will just fetch the first page.
|
|
27
28
|
*
|
|
28
|
-
* You can ignore this if your query is not
|
|
29
|
+
* You can ignore this if your query is not an infinite query.
|
|
29
30
|
*
|
|
30
31
|
* @returns Promise that will always get resolved.
|
|
31
32
|
*/
|
|
32
|
-
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
|
|
33
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
33
34
|
/**
|
|
34
35
|
* Set query state (data, error, etc) to initial state.
|
|
35
36
|
*/
|
|
@@ -41,7 +42,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
41
42
|
*
|
|
42
43
|
* IMPORTANT NOTE: This won't work well on infinite query.
|
|
43
44
|
*/
|
|
44
|
-
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse)) => {
|
|
45
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
45
46
|
revert: () => void;
|
|
46
47
|
invalidate: () => void;
|
|
47
48
|
};
|
|
@@ -55,14 +56,19 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
55
56
|
isWaitingNextPage: boolean;
|
|
56
57
|
isRefetching: boolean;
|
|
57
58
|
isRefetchError: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Will be `true` if the response/data comes from the previous query key.
|
|
61
|
+
*
|
|
62
|
+
* @see `keepPreviousData` option
|
|
63
|
+
*/
|
|
58
64
|
isPreviousData: boolean;
|
|
59
65
|
isOptimisticData: boolean;
|
|
60
|
-
error: TError |
|
|
61
|
-
errorUpdatedAt: number |
|
|
66
|
+
error: TError | undefined;
|
|
67
|
+
errorUpdatedAt: number | undefined;
|
|
62
68
|
retryCount: number;
|
|
63
69
|
isGoingToRetry: boolean;
|
|
64
|
-
pageParam:
|
|
65
|
-
pageParams:
|
|
70
|
+
pageParam: Maybe<TPageParam>;
|
|
71
|
+
pageParams: Maybe<TPageParam>[];
|
|
66
72
|
hasNextPage: boolean;
|
|
67
73
|
retryNextPageCount: number;
|
|
68
74
|
isGoingToRetryNextPage: boolean;
|
|
@@ -78,6 +84,8 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
78
84
|
*
|
|
79
85
|
* It has no relation with network fetching state.
|
|
80
86
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
87
|
+
*
|
|
88
|
+
* @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
|
|
81
89
|
*/
|
|
82
90
|
status: 'loading';
|
|
83
91
|
/**
|
|
@@ -85,6 +93,8 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
85
93
|
*
|
|
86
94
|
* It has no relation with network fetching state.
|
|
87
95
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
96
|
+
*
|
|
97
|
+
* @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
|
|
88
98
|
*/
|
|
89
99
|
isLoading: true;
|
|
90
100
|
/**
|
|
@@ -99,9 +109,9 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
99
109
|
* If data fetched successfully but then an error occured, `isError` will be `false` but `isRefetchError` will be `true`.
|
|
100
110
|
*/
|
|
101
111
|
isError: false;
|
|
102
|
-
data:
|
|
103
|
-
response:
|
|
104
|
-
responseUpdatedAt:
|
|
112
|
+
data: undefined;
|
|
113
|
+
response: undefined;
|
|
114
|
+
responseUpdatedAt: undefined;
|
|
105
115
|
} | {
|
|
106
116
|
status: 'success';
|
|
107
117
|
isLoading: false;
|
|
@@ -109,18 +119,18 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
109
119
|
isError: false;
|
|
110
120
|
data: TData;
|
|
111
121
|
response: TResponse;
|
|
112
|
-
responseUpdatedAt: number |
|
|
122
|
+
responseUpdatedAt: number | undefined;
|
|
113
123
|
} | {
|
|
114
124
|
status: 'error';
|
|
115
125
|
isLoading: false;
|
|
116
126
|
isSuccess: false;
|
|
117
127
|
isError: true;
|
|
118
|
-
data:
|
|
119
|
-
response:
|
|
120
|
-
responseUpdatedAt:
|
|
128
|
+
data: undefined;
|
|
129
|
+
response: undefined;
|
|
130
|
+
responseUpdatedAt: undefined;
|
|
121
131
|
});
|
|
122
|
-
export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError>> & {
|
|
123
|
-
select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError>, 'data' | 'key'>) => TData;
|
|
132
|
+
export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
|
|
133
|
+
select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, 'data' | 'key'>) => TData;
|
|
124
134
|
/**
|
|
125
135
|
* Stale time in miliseconds.
|
|
126
136
|
*
|
|
@@ -161,29 +171,31 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
|
|
|
161
171
|
*
|
|
162
172
|
* Defaults to `1`.
|
|
163
173
|
*/
|
|
164
|
-
retry?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError>) => number);
|
|
174
|
+
retry?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
|
|
165
175
|
/**
|
|
166
176
|
* Error retry delay in miliseconds.
|
|
167
177
|
*
|
|
168
178
|
* Defaults to `2000` (2 seconds).
|
|
169
179
|
*/
|
|
170
|
-
retryDelay?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError>) => number);
|
|
180
|
+
retryDelay?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
|
|
171
181
|
/**
|
|
172
|
-
*
|
|
182
|
+
* Used for lagged query.
|
|
173
183
|
*
|
|
174
|
-
*
|
|
184
|
+
* If set to `true`, then:
|
|
185
|
+
* when the query key changed and there is no `data` in the next query key cache,
|
|
186
|
+
* the previous query key cache `data` will be used while fetching new data.
|
|
175
187
|
*/
|
|
176
188
|
keepPreviousData?: boolean;
|
|
177
189
|
/**
|
|
178
190
|
* Only set this if you have an infinite query.
|
|
179
191
|
*
|
|
180
|
-
* This function should return a variable that will be used when fetching next page
|
|
192
|
+
* This function should return a variable that will be stored as `pageParam` that can be used when fetching next page.
|
|
181
193
|
*/
|
|
182
|
-
getNextPageParam?: (lastPage: TResponse, index: number) =>
|
|
183
|
-
onBeforeFetch?: (cancel: () => void, state: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
184
|
-
onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
185
|
-
onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
186
|
-
onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
194
|
+
getNextPageParam?: (lastPage: TResponse, index: number) => Maybe<TPageParam>;
|
|
195
|
+
onBeforeFetch?: (cancel: () => void, state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
196
|
+
onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
197
|
+
onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
198
|
+
onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
187
199
|
/**
|
|
188
200
|
* Cache time in miliseconds.
|
|
189
201
|
*
|
|
@@ -204,9 +216,9 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
|
|
|
204
216
|
*
|
|
205
217
|
* @see https://floppy-disk.vercel.app/docs/query/polling
|
|
206
218
|
*/
|
|
207
|
-
refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError>) => number | false);
|
|
219
|
+
refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number | false);
|
|
208
220
|
};
|
|
209
|
-
export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError>> & {
|
|
221
|
+
export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
|
|
210
222
|
/**
|
|
211
223
|
* Set query's initial response.
|
|
212
224
|
*
|
|
@@ -215,7 +227,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
215
227
|
* IMPORTANT NOTE: Put this on the root component or parent component, before any component subscribed!
|
|
216
228
|
*/
|
|
217
229
|
setInitialResponse: (options: {
|
|
218
|
-
key?: TKey
|
|
230
|
+
key?: Maybe<TKey>;
|
|
219
231
|
response: TResponse;
|
|
220
232
|
skipRevalidation?: boolean;
|
|
221
233
|
}) => void;
|
|
@@ -226,7 +238,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
226
238
|
/**
|
|
227
239
|
* Set query state (data, error, etc) to initial state.
|
|
228
240
|
*/
|
|
229
|
-
resetSpecificKey: (key?: TKey
|
|
241
|
+
resetSpecificKey: (key?: Maybe<TKey>) => void;
|
|
230
242
|
/**
|
|
231
243
|
* Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
|
|
232
244
|
*/
|
|
@@ -234,7 +246,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
234
246
|
/**
|
|
235
247
|
* Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
|
|
236
248
|
*/
|
|
237
|
-
invalidateSpecificKey: (key?: TKey
|
|
249
|
+
invalidateSpecificKey: (key?: Maybe<TKey>) => void;
|
|
238
250
|
/**
|
|
239
251
|
* Optimistic update.
|
|
240
252
|
*
|
|
@@ -243,8 +255,8 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
243
255
|
* IMPORTANT NOTE: This won't work well on infinite query.
|
|
244
256
|
*/
|
|
245
257
|
optimisticUpdate: (options: {
|
|
246
|
-
key?: TKey
|
|
247
|
-
response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse);
|
|
258
|
+
key?: Maybe<TKey>;
|
|
259
|
+
response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse);
|
|
248
260
|
}) => {
|
|
249
261
|
revert: () => void;
|
|
250
262
|
invalidate: () => void;
|
|
@@ -252,11 +264,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
252
264
|
/**
|
|
253
265
|
* Use query with suspense mode.
|
|
254
266
|
*/
|
|
255
|
-
suspend: (key?: TKey
|
|
267
|
+
suspend: (key?: Maybe<TKey>) => Extract<QueryState<TKey, TResponse, TData, TError, TPageParam>, {
|
|
256
268
|
status: 'success';
|
|
257
269
|
}>;
|
|
258
270
|
Render: (props: {
|
|
259
|
-
queryKey?: TKey
|
|
271
|
+
queryKey?: Maybe<TKey>;
|
|
260
272
|
loading?: FunctionComponent<TKey>;
|
|
261
273
|
success?: FunctionComponent<TKey>;
|
|
262
274
|
error?: FunctionComponent<TKey>;
|
|
@@ -265,4 +277,4 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
265
277
|
/**
|
|
266
278
|
* @see https://floppy-disk.vercel.app/docs/api#createquery
|
|
267
279
|
*/
|
|
268
|
-
export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
|
|
280
|
+
export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError, TPageParam>) => UseQuery<TKey, TResponse, TData, TError, TPageParam>;
|
|
@@ -13,11 +13,11 @@ const INITIAL_QUERY_STATE = {
|
|
|
13
13
|
isRefetchError: false,
|
|
14
14
|
isPreviousData: false,
|
|
15
15
|
isOptimisticData: false,
|
|
16
|
-
data:
|
|
17
|
-
response:
|
|
18
|
-
responseUpdatedAt:
|
|
19
|
-
error:
|
|
20
|
-
errorUpdatedAt:
|
|
16
|
+
data: undefined,
|
|
17
|
+
response: undefined,
|
|
18
|
+
responseUpdatedAt: undefined,
|
|
19
|
+
error: undefined,
|
|
20
|
+
errorUpdatedAt: undefined,
|
|
21
21
|
retryCount: 0,
|
|
22
22
|
isGoingToRetry: false,
|
|
23
23
|
pageParam: undefined,
|
|
@@ -54,11 +54,11 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
54
54
|
return { shouldRetry: retryCount < maxRetryCount, delay };
|
|
55
55
|
};
|
|
56
56
|
const forceFetch = () => new Promise((resolve) => {
|
|
57
|
+
const state = get();
|
|
57
58
|
const responseAllPages = [];
|
|
58
|
-
const newPageParams = [
|
|
59
|
-
let pageParam =
|
|
59
|
+
const newPageParams = [state.pageParams[0]];
|
|
60
|
+
let pageParam = state.pageParams[0];
|
|
60
61
|
clearTimeout(refetchIntervalTimeoutId.get(keyHash));
|
|
61
|
-
const state = get();
|
|
62
62
|
const { isWaiting, isLoading, pageParams } = state;
|
|
63
63
|
if (isWaiting || !getValueOrComputedValue(enabled, key))
|
|
64
64
|
return resolve(state);
|
|
@@ -109,11 +109,11 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
109
109
|
isOptimisticData: false,
|
|
110
110
|
data: responseAllPages.reduce((prev, responseCurrentPage) => {
|
|
111
111
|
return select(responseCurrentPage, { key, data: prev });
|
|
112
|
-
},
|
|
112
|
+
}, undefined),
|
|
113
113
|
response,
|
|
114
114
|
responseUpdatedAt: Date.now(),
|
|
115
|
-
error:
|
|
116
|
-
errorUpdatedAt:
|
|
115
|
+
error: undefined,
|
|
116
|
+
errorUpdatedAt: undefined,
|
|
117
117
|
retryCount: 0,
|
|
118
118
|
pageParam: newPageParam,
|
|
119
119
|
pageParams: newPageParams,
|
|
@@ -128,7 +128,6 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
128
128
|
}
|
|
129
129
|
set(nextState);
|
|
130
130
|
onSuccess(response, stateBeforeCallQuery);
|
|
131
|
-
resolve(get());
|
|
132
131
|
})
|
|
133
132
|
.catch((error) => {
|
|
134
133
|
const prevState = get();
|
|
@@ -142,7 +141,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
142
141
|
data: responseAllPages.length
|
|
143
142
|
? responseAllPages.reduce((prev, response) => {
|
|
144
143
|
return select(response, { key, data: prev });
|
|
145
|
-
},
|
|
144
|
+
}, undefined)
|
|
146
145
|
: prevState.data,
|
|
147
146
|
error,
|
|
148
147
|
errorUpdatedAt,
|
|
@@ -155,7 +154,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
155
154
|
status: 'error',
|
|
156
155
|
isLoading: false,
|
|
157
156
|
isError: true,
|
|
158
|
-
data:
|
|
157
|
+
data: undefined,
|
|
159
158
|
error,
|
|
160
159
|
errorUpdatedAt,
|
|
161
160
|
isGoingToRetry: shouldRetry,
|
|
@@ -169,17 +168,17 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
169
168
|
}, delay));
|
|
170
169
|
}
|
|
171
170
|
onError(error, stateBeforeCallQuery);
|
|
172
|
-
resolve(get());
|
|
173
171
|
})
|
|
174
172
|
.finally(() => {
|
|
175
173
|
onSettled(stateBeforeCallQuery);
|
|
174
|
+
resolve(get());
|
|
176
175
|
});
|
|
177
176
|
};
|
|
178
177
|
callQuery();
|
|
179
178
|
});
|
|
180
179
|
const fetch = () => {
|
|
181
180
|
const { responseUpdatedAt } = get();
|
|
182
|
-
const isStale = Date.now() >
|
|
181
|
+
const isStale = Date.now() > (responseUpdatedAt || 0) + staleTime;
|
|
183
182
|
if (!isStale)
|
|
184
183
|
return;
|
|
185
184
|
forceFetch();
|
|
@@ -218,7 +217,6 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
218
217
|
hasNextPage: hasValue(newPageParam),
|
|
219
218
|
});
|
|
220
219
|
onSuccess(response, stateBeforeCallQuery);
|
|
221
|
-
resolve(get());
|
|
222
220
|
})
|
|
223
221
|
.catch((error) => {
|
|
224
222
|
const prevState = get();
|
|
@@ -237,10 +235,10 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
237
235
|
}, delay));
|
|
238
236
|
}
|
|
239
237
|
onError(error, stateBeforeCallQuery);
|
|
240
|
-
resolve(get());
|
|
241
238
|
})
|
|
242
239
|
.finally(() => {
|
|
243
240
|
onSettled(stateBeforeCallQuery);
|
|
241
|
+
resolve(get());
|
|
244
242
|
});
|
|
245
243
|
});
|
|
246
244
|
return {
|
|
@@ -338,8 +336,8 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
338
336
|
isSuccess: true,
|
|
339
337
|
isError: false,
|
|
340
338
|
response,
|
|
341
|
-
responseUpdatedAt: skipRevalidation ? Date.now() :
|
|
342
|
-
data: select(response, { key: key, data:
|
|
339
|
+
responseUpdatedAt: skipRevalidation ? Date.now() : undefined,
|
|
340
|
+
data: select(response, { key: key, data: undefined }),
|
|
343
341
|
pageParam: newPageParam,
|
|
344
342
|
pageParams: [undefined, newPageParam],
|
|
345
343
|
hasNextPage: hasValue(newPageParam),
|
|
@@ -358,14 +356,14 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
358
356
|
useQuery.invalidate = () => {
|
|
359
357
|
useQuery.getStores().forEach((store) => {
|
|
360
358
|
const { get, set, getSubscribers } = store;
|
|
361
|
-
set({ responseUpdatedAt:
|
|
359
|
+
set({ responseUpdatedAt: undefined });
|
|
362
360
|
if (getSubscribers().size > 0)
|
|
363
361
|
get().forceFetch();
|
|
364
362
|
});
|
|
365
363
|
};
|
|
366
364
|
useQuery.invalidateSpecificKey = (key) => {
|
|
367
365
|
const { get, set, getSubscribers } = useQuery.getStore(key);
|
|
368
|
-
set({ responseUpdatedAt:
|
|
366
|
+
set({ responseUpdatedAt: undefined });
|
|
369
367
|
if (getSubscribers().size > 0)
|
|
370
368
|
get().forceFetch();
|
|
371
369
|
};
|
|
@@ -377,7 +375,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
377
375
|
useQuery.set(key, {
|
|
378
376
|
isOptimisticData: true,
|
|
379
377
|
response: optimisticResponse,
|
|
380
|
-
data: select(optimisticResponse, { key: key, data:
|
|
378
|
+
data: select(optimisticResponse, { key: key, data: undefined }),
|
|
381
379
|
});
|
|
382
380
|
preventReplaceResponse.set(prevState.keyHash, true);
|
|
383
381
|
const revert = () => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { Maybe } from '../utils';
|
|
1
2
|
import { InitStoreOptions, InitStoreReturn, SelectDeps, SetStoreData, StoreData, Subscribers } from '../vanilla';
|
|
2
3
|
import { WatchProps } from './create-store';
|
|
3
|
-
type Maybe<T> = T | null | undefined;
|
|
4
4
|
export type StoreKey = Record<string, any> | undefined;
|
|
5
5
|
export type StoresInitializer<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = (api: {
|
|
6
6
|
get: () => T;
|
|
@@ -50,4 +50,3 @@ export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends Stor
|
|
|
50
50
|
* @see https://floppy-disk.vercel.app/docs/api#createstores
|
|
51
51
|
*/
|
|
52
52
|
export declare const createStores: <TKey extends StoreKey = StoreKey, T extends StoreData = StoreData>(initializer: StoresInitializer<TKey, T>, options?: CreateStoresOptions<TKey, T>) => UseStores<TKey, T>;
|
|
53
|
-
export {};
|
|
@@ -7,10 +7,10 @@ export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
|
7
7
|
isWaiting: boolean;
|
|
8
8
|
isSuccess: boolean;
|
|
9
9
|
isError: boolean;
|
|
10
|
-
response: TResponse |
|
|
11
|
-
responseUpdatedAt: number |
|
|
12
|
-
error: TError |
|
|
13
|
-
errorUpdatedAt: number |
|
|
10
|
+
response: TResponse | undefined;
|
|
11
|
+
responseUpdatedAt: number | undefined;
|
|
12
|
+
error: TError | undefined;
|
|
13
|
+
errorUpdatedAt: number | undefined;
|
|
14
14
|
/**
|
|
15
15
|
* Mutate function.
|
|
16
16
|
*
|
|
@@ -9,10 +9,10 @@ export const createMutation = (mutationFn, options = {}) => {
|
|
|
9
9
|
isWaiting: false,
|
|
10
10
|
isSuccess: false,
|
|
11
11
|
isError: false,
|
|
12
|
-
response:
|
|
13
|
-
responseUpdatedAt:
|
|
14
|
-
error:
|
|
15
|
-
errorUpdatedAt:
|
|
12
|
+
response: undefined,
|
|
13
|
+
responseUpdatedAt: undefined,
|
|
14
|
+
error: undefined,
|
|
15
|
+
errorUpdatedAt: undefined,
|
|
16
16
|
mutate: ((variables) => {
|
|
17
17
|
set({ isWaiting: true });
|
|
18
18
|
const stateBeforeMutate = get();
|
|
@@ -26,8 +26,8 @@ export const createMutation = (mutationFn, options = {}) => {
|
|
|
26
26
|
isError: false,
|
|
27
27
|
response,
|
|
28
28
|
responseUpdatedAt: Date.now(),
|
|
29
|
-
error:
|
|
30
|
-
errorUpdatedAt:
|
|
29
|
+
error: undefined,
|
|
30
|
+
errorUpdatedAt: undefined,
|
|
31
31
|
});
|
|
32
32
|
onSuccess(response, variables, stateBeforeMutate);
|
|
33
33
|
resolve({ response, variables });
|