floppy-disk 2.5.0-beta.2 → 2.5.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.
- package/esm/preact/create-mutation.d.ts +2 -2
- package/esm/preact/create-mutation.js +3 -3
- package/esm/preact/create-query.d.ts +45 -33
- package/esm/preact/create-query.js +10 -10
- package/esm/preact/create-stores.d.ts +1 -2
- package/esm/react/create-mutation.d.ts +2 -2
- package/esm/react/create-mutation.js +3 -3
- package/esm/react/create-query.d.ts +45 -33
- package/esm/react/create-query.js +10 -10
- package/esm/react/create-stores.d.ts +1 -2
- package/esm/utils/index.d.ts +1 -0
- package/lib/preact/create-mutation.d.ts +2 -2
- package/lib/preact/create-mutation.js +3 -3
- package/lib/preact/create-query.d.ts +45 -33
- package/lib/preact/create-query.js +10 -10
- package/lib/preact/create-stores.d.ts +1 -2
- package/lib/react/create-mutation.d.ts +2 -2
- package/lib/react/create-mutation.js +3 -3
- package/lib/react/create-query.d.ts +45 -33
- package/lib/react/create-query.js +10 -10
- package/lib/react/create-stores.d.ts +1 -2
- package/lib/utils/index.d.ts +1 -0
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { FunctionComponent } from 'react';
|
|
2
|
+
import { Maybe } from '../utils';
|
|
2
3
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
3
|
-
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
4
|
+
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = {
|
|
4
5
|
/**
|
|
5
6
|
* Query store key, an object that will be hashed into a string as a query store identifier.
|
|
6
7
|
*/
|
|
@@ -18,17 +19,17 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
18
19
|
*
|
|
19
20
|
* @returns Promise that will always get resolved.
|
|
20
21
|
*/
|
|
21
|
-
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
|
|
22
|
+
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
22
23
|
/**
|
|
23
24
|
* Fetch next page if has next page.
|
|
24
25
|
*
|
|
25
26
|
* If the data is empty, it will just fetch the first page.
|
|
26
27
|
*
|
|
27
|
-
* You can ignore this if your query is not
|
|
28
|
+
* You can ignore this if your query is not an infinite query.
|
|
28
29
|
*
|
|
29
30
|
* @returns Promise that will always get resolved.
|
|
30
31
|
*/
|
|
31
|
-
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
|
|
32
|
+
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
32
33
|
/**
|
|
33
34
|
* Set query state (data, error, etc) to initial state.
|
|
34
35
|
*/
|
|
@@ -40,7 +41,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
40
41
|
*
|
|
41
42
|
* IMPORTANT NOTE: This won't work well on infinite query.
|
|
42
43
|
*/
|
|
43
|
-
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse)) => {
|
|
44
|
+
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
44
45
|
revert: () => void;
|
|
45
46
|
invalidate: () => void;
|
|
46
47
|
};
|
|
@@ -54,14 +55,19 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
54
55
|
isWaitingNextPage: boolean;
|
|
55
56
|
isRefetching: boolean;
|
|
56
57
|
isRefetchError: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Will be `true` if the response/data comes from the previous query key.
|
|
60
|
+
*
|
|
61
|
+
* @see `keepPreviousData` option
|
|
62
|
+
*/
|
|
57
63
|
isPreviousData: boolean;
|
|
58
64
|
isOptimisticData: boolean;
|
|
59
65
|
error: TError | undefined;
|
|
60
|
-
errorUpdatedAt: number |
|
|
66
|
+
errorUpdatedAt: number | undefined;
|
|
61
67
|
retryCount: number;
|
|
62
68
|
isGoingToRetry: boolean;
|
|
63
|
-
pageParam:
|
|
64
|
-
pageParams:
|
|
69
|
+
pageParam: Maybe<TPageParam>;
|
|
70
|
+
pageParams: Maybe<TPageParam>[];
|
|
65
71
|
hasNextPage: boolean;
|
|
66
72
|
retryNextPageCount: number;
|
|
67
73
|
isGoingToRetryNextPage: boolean;
|
|
@@ -77,6 +83,8 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
77
83
|
*
|
|
78
84
|
* It has no relation with network fetching state.
|
|
79
85
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
86
|
+
*
|
|
87
|
+
* @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
|
|
80
88
|
*/
|
|
81
89
|
status: 'loading';
|
|
82
90
|
/**
|
|
@@ -84,6 +92,8 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
84
92
|
*
|
|
85
93
|
* It has no relation with network fetching state.
|
|
86
94
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
95
|
+
*
|
|
96
|
+
* @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
|
|
87
97
|
*/
|
|
88
98
|
isLoading: true;
|
|
89
99
|
/**
|
|
@@ -100,7 +110,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
100
110
|
isError: false;
|
|
101
111
|
data: undefined;
|
|
102
112
|
response: undefined;
|
|
103
|
-
responseUpdatedAt:
|
|
113
|
+
responseUpdatedAt: undefined;
|
|
104
114
|
} | {
|
|
105
115
|
status: 'success';
|
|
106
116
|
isLoading: false;
|
|
@@ -108,7 +118,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
108
118
|
isError: false;
|
|
109
119
|
data: TData;
|
|
110
120
|
response: TResponse;
|
|
111
|
-
responseUpdatedAt: number |
|
|
121
|
+
responseUpdatedAt: number | undefined;
|
|
112
122
|
} | {
|
|
113
123
|
status: 'error';
|
|
114
124
|
isLoading: false;
|
|
@@ -116,10 +126,10 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
116
126
|
isError: true;
|
|
117
127
|
data: undefined;
|
|
118
128
|
response: undefined;
|
|
119
|
-
responseUpdatedAt:
|
|
129
|
+
responseUpdatedAt: undefined;
|
|
120
130
|
});
|
|
121
|
-
export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError>> & {
|
|
122
|
-
select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError>, 'data' | 'key'>) => TData;
|
|
131
|
+
export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
|
|
132
|
+
select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, 'data' | 'key'>) => TData;
|
|
123
133
|
/**
|
|
124
134
|
* Stale time in miliseconds.
|
|
125
135
|
*
|
|
@@ -160,29 +170,31 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
|
|
|
160
170
|
*
|
|
161
171
|
* Defaults to `1`.
|
|
162
172
|
*/
|
|
163
|
-
retry?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError>) => number);
|
|
173
|
+
retry?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
|
|
164
174
|
/**
|
|
165
175
|
* Error retry delay in miliseconds.
|
|
166
176
|
*
|
|
167
177
|
* Defaults to `2000` (2 seconds).
|
|
168
178
|
*/
|
|
169
|
-
retryDelay?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError>) => number);
|
|
179
|
+
retryDelay?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
|
|
170
180
|
/**
|
|
171
|
-
*
|
|
181
|
+
* Used for lagged query.
|
|
172
182
|
*
|
|
173
|
-
*
|
|
183
|
+
* If set to `true`, then:
|
|
184
|
+
* when the query key changed and there is no `data` in the next query key cache,
|
|
185
|
+
* the previous query key cache `data` will be used while fetching new data.
|
|
174
186
|
*/
|
|
175
187
|
keepPreviousData?: boolean;
|
|
176
188
|
/**
|
|
177
189
|
* Only set this if you have an infinite query.
|
|
178
190
|
*
|
|
179
|
-
* This function should return a variable that will be used when fetching next page
|
|
191
|
+
* This function should return a variable that will be stored as `pageParam` that can be used when fetching next page.
|
|
180
192
|
*/
|
|
181
|
-
getNextPageParam?: (lastPage: TResponse, index: number) =>
|
|
182
|
-
onBeforeFetch?: (cancel: () => void, state: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
183
|
-
onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
184
|
-
onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
185
|
-
onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
193
|
+
getNextPageParam?: (lastPage: TResponse, index: number) => Maybe<TPageParam>;
|
|
194
|
+
onBeforeFetch?: (cancel: () => void, state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
195
|
+
onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
196
|
+
onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
197
|
+
onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
186
198
|
/**
|
|
187
199
|
* Cache time in miliseconds.
|
|
188
200
|
*
|
|
@@ -203,9 +215,9 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
|
|
|
203
215
|
*
|
|
204
216
|
* @see https://floppy-disk.vercel.app/docs/query/polling
|
|
205
217
|
*/
|
|
206
|
-
refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError>) => number | false);
|
|
218
|
+
refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number | false);
|
|
207
219
|
};
|
|
208
|
-
export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError>> & {
|
|
220
|
+
export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
|
|
209
221
|
/**
|
|
210
222
|
* Set query's initial response.
|
|
211
223
|
*
|
|
@@ -214,7 +226,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
214
226
|
* IMPORTANT NOTE: Put this on the root component or parent component, before any component subscribed!
|
|
215
227
|
*/
|
|
216
228
|
setInitialResponse: (options: {
|
|
217
|
-
key?: TKey
|
|
229
|
+
key?: Maybe<TKey>;
|
|
218
230
|
response: TResponse;
|
|
219
231
|
skipRevalidation?: boolean;
|
|
220
232
|
}) => void;
|
|
@@ -225,7 +237,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
225
237
|
/**
|
|
226
238
|
* Set query state (data, error, etc) to initial state.
|
|
227
239
|
*/
|
|
228
|
-
resetSpecificKey: (key?: TKey
|
|
240
|
+
resetSpecificKey: (key?: Maybe<TKey>) => void;
|
|
229
241
|
/**
|
|
230
242
|
* Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
|
|
231
243
|
*/
|
|
@@ -233,7 +245,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
233
245
|
/**
|
|
234
246
|
* Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
|
|
235
247
|
*/
|
|
236
|
-
invalidateSpecificKey: (key?: TKey
|
|
248
|
+
invalidateSpecificKey: (key?: Maybe<TKey>) => void;
|
|
237
249
|
/**
|
|
238
250
|
* Optimistic update.
|
|
239
251
|
*
|
|
@@ -242,8 +254,8 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
242
254
|
* IMPORTANT NOTE: This won't work well on infinite query.
|
|
243
255
|
*/
|
|
244
256
|
optimisticUpdate: (options: {
|
|
245
|
-
key?: TKey
|
|
246
|
-
response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse);
|
|
257
|
+
key?: Maybe<TKey>;
|
|
258
|
+
response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse);
|
|
247
259
|
}) => {
|
|
248
260
|
revert: () => void;
|
|
249
261
|
invalidate: () => void;
|
|
@@ -251,11 +263,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
251
263
|
/**
|
|
252
264
|
* Use query with suspense mode.
|
|
253
265
|
*/
|
|
254
|
-
suspend: (key?: TKey
|
|
266
|
+
suspend: (key?: Maybe<TKey>) => Extract<QueryState<TKey, TResponse, TData, TError, TPageParam>, {
|
|
255
267
|
status: 'success';
|
|
256
268
|
}>;
|
|
257
269
|
Render: (props: {
|
|
258
|
-
queryKey?: TKey
|
|
270
|
+
queryKey?: Maybe<TKey>;
|
|
259
271
|
loading?: FunctionComponent<TKey>;
|
|
260
272
|
success?: FunctionComponent<TKey>;
|
|
261
273
|
error?: FunctionComponent<TKey>;
|
|
@@ -264,4 +276,4 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
|
|
|
264
276
|
/**
|
|
265
277
|
* @see https://floppy-disk.vercel.app/docs/api#createquery
|
|
266
278
|
*/
|
|
267
|
-
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>;
|
|
279
|
+
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>;
|
|
@@ -17,9 +17,9 @@ const INITIAL_QUERY_STATE = {
|
|
|
17
17
|
isOptimisticData: false,
|
|
18
18
|
data: undefined,
|
|
19
19
|
response: undefined,
|
|
20
|
-
responseUpdatedAt:
|
|
20
|
+
responseUpdatedAt: undefined,
|
|
21
21
|
error: undefined,
|
|
22
|
-
errorUpdatedAt:
|
|
22
|
+
errorUpdatedAt: undefined,
|
|
23
23
|
retryCount: 0,
|
|
24
24
|
isGoingToRetry: false,
|
|
25
25
|
pageParam: undefined,
|
|
@@ -56,11 +56,11 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
56
56
|
return { shouldRetry: retryCount < maxRetryCount, delay };
|
|
57
57
|
};
|
|
58
58
|
const forceFetch = () => new Promise((resolve) => {
|
|
59
|
+
const state = get();
|
|
59
60
|
const responseAllPages = [];
|
|
60
|
-
const newPageParams = [
|
|
61
|
-
let pageParam =
|
|
61
|
+
const newPageParams = [state.pageParams[0]];
|
|
62
|
+
let pageParam = state.pageParams[0];
|
|
62
63
|
clearTimeout(refetchIntervalTimeoutId.get(keyHash));
|
|
63
|
-
const state = get();
|
|
64
64
|
const { isWaiting, isLoading, pageParams } = state;
|
|
65
65
|
if (isWaiting || !(0, utils_1.getValueOrComputedValue)(enabled, key))
|
|
66
66
|
return resolve(state);
|
|
@@ -115,7 +115,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
115
115
|
response,
|
|
116
116
|
responseUpdatedAt: Date.now(),
|
|
117
117
|
error: undefined,
|
|
118
|
-
errorUpdatedAt:
|
|
118
|
+
errorUpdatedAt: undefined,
|
|
119
119
|
retryCount: 0,
|
|
120
120
|
pageParam: newPageParam,
|
|
121
121
|
pageParams: newPageParams,
|
|
@@ -180,7 +180,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
180
180
|
});
|
|
181
181
|
const fetch = () => {
|
|
182
182
|
const { responseUpdatedAt } = get();
|
|
183
|
-
const isStale = Date.now() >
|
|
183
|
+
const isStale = Date.now() > (responseUpdatedAt || 0) + staleTime;
|
|
184
184
|
if (!isStale)
|
|
185
185
|
return;
|
|
186
186
|
forceFetch();
|
|
@@ -338,7 +338,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
338
338
|
isSuccess: true,
|
|
339
339
|
isError: false,
|
|
340
340
|
response,
|
|
341
|
-
responseUpdatedAt: skipRevalidation ? Date.now() :
|
|
341
|
+
responseUpdatedAt: skipRevalidation ? Date.now() : undefined,
|
|
342
342
|
data: select(response, { key: key, data: undefined }),
|
|
343
343
|
pageParam: newPageParam,
|
|
344
344
|
pageParams: [undefined, newPageParam],
|
|
@@ -358,14 +358,14 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
358
358
|
useQuery.invalidate = () => {
|
|
359
359
|
useQuery.getStores().forEach((store) => {
|
|
360
360
|
const { get, set, getSubscribers } = store;
|
|
361
|
-
set({ responseUpdatedAt:
|
|
361
|
+
set({ responseUpdatedAt: undefined });
|
|
362
362
|
if (getSubscribers().size > 0)
|
|
363
363
|
get().forceFetch();
|
|
364
364
|
});
|
|
365
365
|
};
|
|
366
366
|
useQuery.invalidateSpecificKey = (key) => {
|
|
367
367
|
const { get, set, getSubscribers } = useQuery.getStore(key);
|
|
368
|
-
set({ responseUpdatedAt:
|
|
368
|
+
set({ responseUpdatedAt: undefined });
|
|
369
369
|
if (getSubscribers().size > 0)
|
|
370
370
|
get().forceFetch();
|
|
371
371
|
};
|
|
@@ -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 {};
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export declare const identityFn: <T>(value: T) => T;
|
|
|
3
3
|
export declare const hasValue: (value: any) => boolean;
|
|
4
4
|
export declare const hashStoreKey: (obj?: any) => string;
|
|
5
5
|
export declare const getValueOrComputedValue: <T, P extends any[]>(valueOrComputeValueFn: T | ((...params: P) => T), ...params: P) => T;
|
|
6
|
+
export type Maybe<T> = T | null | undefined;
|