floppy-disk 2.11.1-experimental.2 → 2.11.1
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/package.json +1 -10
- package/esm/preact/create-bi-direction-query.d.ts +0 -167
- package/esm/preact/create-bi-direction-query.js +0 -74
- package/esm/preact/create-mutation.d.ts +0 -39
- package/esm/preact/create-mutation.js +0 -53
- package/esm/preact/create-query.d.ts +0 -300
- package/esm/preact/create-query.js +0 -429
- package/esm/preact/create-store.d.ts +0 -31
- package/esm/preact/create-store.js +0 -38
- package/esm/preact/create-stores.d.ts +0 -52
- package/esm/preact/create-stores.js +0 -98
- package/esm/preact/index.d.ts +0 -6
- package/esm/preact/index.js +0 -6
- package/esm/preact/with-context.d.ts +0 -8
- package/esm/preact/with-context.js +0 -15
- package/lib/preact/create-bi-direction-query.d.ts +0 -167
- package/lib/preact/create-bi-direction-query.js +0 -78
- package/lib/preact/create-mutation.d.ts +0 -39
- package/lib/preact/create-mutation.js +0 -57
- package/lib/preact/create-query.d.ts +0 -300
- package/lib/preact/create-query.js +0 -433
- package/lib/preact/create-store.d.ts +0 -31
- package/lib/preact/create-store.js +0 -42
- package/lib/preact/create-stores.d.ts +0 -52
- package/lib/preact/create-stores.js +0 -102
- package/lib/preact/index.d.ts +0 -6
- package/lib/preact/index.js +0 -9
- package/lib/preact/with-context.d.ts +0 -8
- package/lib/preact/with-context.js +0 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "floppy-disk",
|
|
3
|
-
"version": "2.11.1
|
|
3
|
+
"version": "2.11.1",
|
|
4
4
|
"description": "FloppyDisk - lightweight, simple, and powerful state management library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state",
|
|
@@ -41,15 +41,6 @@
|
|
|
41
41
|
},
|
|
42
42
|
"module": "./esm/index.js",
|
|
43
43
|
"default": "./lib/index.js"
|
|
44
|
-
},
|
|
45
|
-
"./preact": {
|
|
46
|
-
"types": "./lib/preact/index.d.ts",
|
|
47
|
-
"import": {
|
|
48
|
-
"types": "./esm/preact/index.d.ts",
|
|
49
|
-
"default": "./esm/preact/index.js"
|
|
50
|
-
},
|
|
51
|
-
"module": "./esm/preact/index.js",
|
|
52
|
-
"default": "./lib/preact/index.js"
|
|
53
44
|
}
|
|
54
45
|
},
|
|
55
46
|
"scripts": {
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import { Maybe } from '../utils';
|
|
2
|
-
import { SelectDeps } from '../vanilla';
|
|
3
|
-
import { CreateQueryOptions, QueryState } from './create-query';
|
|
4
|
-
import { StoreKey } from './create-stores';
|
|
5
|
-
export declare const createBiDirectionQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData extends any[] = any[], TError = unknown, TPageParam = any>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError, TPageParam>, direction: 'prev' | 'next') => Promise<TResponse>, options: Omit<CreateQueryOptions<TKey, TResponse, TData, TError, TPageParam>, "select" | "getNextPageParam"> & {
|
|
6
|
-
getPrevPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
7
|
-
getNextPageParam: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
8
|
-
select: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, "data" | "key">, direction: 'prev' | 'next') => TData;
|
|
9
|
-
}) => {
|
|
10
|
-
(...args: [Maybe<TKey>, SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?] | [SelectDeps<QueryState<TKey, TResponse, TData, TError, TPageParam>>?]): {
|
|
11
|
-
data: (never[] | TData)[number][];
|
|
12
|
-
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
13
|
-
hasPrevPage: boolean;
|
|
14
|
-
isWaitingPrevPage: boolean;
|
|
15
|
-
key: TKey;
|
|
16
|
-
keyHash: string;
|
|
17
|
-
fetch: () => void;
|
|
18
|
-
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
19
|
-
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
20
|
-
reset: () => void;
|
|
21
|
-
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
22
|
-
revert: () => void;
|
|
23
|
-
invalidate: () => void;
|
|
24
|
-
};
|
|
25
|
-
isWaiting: boolean;
|
|
26
|
-
isWaitingNextPage: boolean;
|
|
27
|
-
isRefetching: boolean;
|
|
28
|
-
isRefetchError: boolean;
|
|
29
|
-
isPreviousData: boolean;
|
|
30
|
-
isOptimisticData: boolean;
|
|
31
|
-
error: TError | undefined;
|
|
32
|
-
errorUpdatedAt: number | undefined;
|
|
33
|
-
retryCount: number;
|
|
34
|
-
isGoingToRetry: boolean;
|
|
35
|
-
pageParam: Maybe<TPageParam>;
|
|
36
|
-
pageParams: Maybe<TPageParam>[];
|
|
37
|
-
hasNextPage: boolean;
|
|
38
|
-
retryNextPageCount: number;
|
|
39
|
-
isGoingToRetryNextPage: boolean;
|
|
40
|
-
status: "loading";
|
|
41
|
-
isLoading: true;
|
|
42
|
-
isSuccess: false;
|
|
43
|
-
isError: false;
|
|
44
|
-
response: undefined;
|
|
45
|
-
responseUpdatedAt: undefined;
|
|
46
|
-
} | {
|
|
47
|
-
data: (never[] | TData)[number][];
|
|
48
|
-
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
49
|
-
hasPrevPage: boolean;
|
|
50
|
-
isWaitingPrevPage: boolean;
|
|
51
|
-
key: TKey;
|
|
52
|
-
keyHash: string;
|
|
53
|
-
fetch: () => void;
|
|
54
|
-
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
55
|
-
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
56
|
-
reset: () => void;
|
|
57
|
-
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
58
|
-
revert: () => void;
|
|
59
|
-
invalidate: () => void;
|
|
60
|
-
};
|
|
61
|
-
isWaiting: boolean;
|
|
62
|
-
isWaitingNextPage: boolean;
|
|
63
|
-
isRefetching: boolean;
|
|
64
|
-
isRefetchError: boolean;
|
|
65
|
-
isPreviousData: boolean;
|
|
66
|
-
isOptimisticData: boolean;
|
|
67
|
-
error: TError | undefined;
|
|
68
|
-
errorUpdatedAt: number | undefined;
|
|
69
|
-
retryCount: number;
|
|
70
|
-
isGoingToRetry: boolean;
|
|
71
|
-
pageParam: Maybe<TPageParam>;
|
|
72
|
-
pageParams: Maybe<TPageParam>[];
|
|
73
|
-
hasNextPage: boolean;
|
|
74
|
-
retryNextPageCount: number;
|
|
75
|
-
isGoingToRetryNextPage: boolean;
|
|
76
|
-
status: "error";
|
|
77
|
-
isLoading: false;
|
|
78
|
-
isSuccess: false;
|
|
79
|
-
isError: true;
|
|
80
|
-
response: undefined;
|
|
81
|
-
responseUpdatedAt: undefined;
|
|
82
|
-
} | {
|
|
83
|
-
data: (never[] | TData)[number][];
|
|
84
|
-
fetchPrevPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
85
|
-
hasPrevPage: boolean;
|
|
86
|
-
isWaitingPrevPage: boolean;
|
|
87
|
-
key: TKey;
|
|
88
|
-
keyHash: string;
|
|
89
|
-
fetch: () => void;
|
|
90
|
-
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
91
|
-
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
92
|
-
reset: () => void;
|
|
93
|
-
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
94
|
-
revert: () => void;
|
|
95
|
-
invalidate: () => void;
|
|
96
|
-
};
|
|
97
|
-
isWaiting: boolean;
|
|
98
|
-
isWaitingNextPage: boolean;
|
|
99
|
-
isRefetching: boolean;
|
|
100
|
-
isRefetchError: boolean;
|
|
101
|
-
isPreviousData: boolean;
|
|
102
|
-
isOptimisticData: boolean;
|
|
103
|
-
error: TError | undefined;
|
|
104
|
-
errorUpdatedAt: number | undefined;
|
|
105
|
-
retryCount: number;
|
|
106
|
-
isGoingToRetry: boolean;
|
|
107
|
-
pageParam: Maybe<TPageParam>;
|
|
108
|
-
pageParams: Maybe<TPageParam>[];
|
|
109
|
-
hasNextPage: boolean;
|
|
110
|
-
retryNextPageCount: number;
|
|
111
|
-
isGoingToRetryNextPage: boolean;
|
|
112
|
-
status: "success";
|
|
113
|
-
isLoading: false;
|
|
114
|
-
isSuccess: true;
|
|
115
|
-
isError: false;
|
|
116
|
-
response: TResponse;
|
|
117
|
-
responseUpdatedAt: number | undefined;
|
|
118
|
-
};
|
|
119
|
-
get(): {
|
|
120
|
-
prev: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
121
|
-
next: QueryState<TKey, TResponse, TData, TError, TPageParam>;
|
|
122
|
-
};
|
|
123
|
-
setInitialResponse: (options: {
|
|
124
|
-
key?: Maybe<TKey>;
|
|
125
|
-
response: TResponse;
|
|
126
|
-
skipRevalidation?: boolean | undefined;
|
|
127
|
-
}) => void;
|
|
128
|
-
reset(): void;
|
|
129
|
-
resetSpecificKey(key: Maybe<TKey>): void;
|
|
130
|
-
invalidate: () => void;
|
|
131
|
-
invalidateSpecificKey: (key?: Maybe<TKey>) => void;
|
|
132
|
-
suspend: (key?: Maybe<TKey>) => {
|
|
133
|
-
key: TKey;
|
|
134
|
-
keyHash: string;
|
|
135
|
-
fetch: () => void;
|
|
136
|
-
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
137
|
-
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
138
|
-
reset: () => void;
|
|
139
|
-
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
140
|
-
revert: () => void;
|
|
141
|
-
invalidate: () => void;
|
|
142
|
-
};
|
|
143
|
-
isWaiting: boolean;
|
|
144
|
-
isWaitingNextPage: boolean;
|
|
145
|
-
isRefetching: boolean;
|
|
146
|
-
isRefetchError: boolean;
|
|
147
|
-
isPreviousData: boolean;
|
|
148
|
-
isOptimisticData: boolean;
|
|
149
|
-
error: TError | undefined;
|
|
150
|
-
errorUpdatedAt: number | undefined;
|
|
151
|
-
retryCount: number;
|
|
152
|
-
isGoingToRetry: boolean;
|
|
153
|
-
pageParam: Maybe<TPageParam>;
|
|
154
|
-
pageParams: Maybe<TPageParam>[];
|
|
155
|
-
hasNextPage: boolean;
|
|
156
|
-
retryNextPageCount: number;
|
|
157
|
-
isGoingToRetryNextPage: boolean;
|
|
158
|
-
} & {
|
|
159
|
-
status: "success";
|
|
160
|
-
isLoading: false;
|
|
161
|
-
isSuccess: true;
|
|
162
|
-
isError: false;
|
|
163
|
-
data: TData;
|
|
164
|
-
response: TResponse;
|
|
165
|
-
responseUpdatedAt: number | undefined;
|
|
166
|
-
};
|
|
167
|
-
};
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { hasValue } from '../utils';
|
|
2
|
-
import { createQuery } from './create-query';
|
|
3
|
-
export const createBiDirectionQuery = (queryFn, options) => {
|
|
4
|
-
const { getPrevPageParam, getNextPageParam, select, ...restOptions } = options;
|
|
5
|
-
const usePrevPagesQuery = createQuery((key, state) => queryFn(key, state, 'prev'), {
|
|
6
|
-
defaultDeps: (state) => [
|
|
7
|
-
state.isWaiting,
|
|
8
|
-
state.data,
|
|
9
|
-
state.error,
|
|
10
|
-
state.isWaitingNextPage,
|
|
11
|
-
state.hasNextPage,
|
|
12
|
-
],
|
|
13
|
-
fetchOnMount: false,
|
|
14
|
-
getNextPageParam: getPrevPageParam,
|
|
15
|
-
select: (response, state) => select(response, state, 'prev'),
|
|
16
|
-
...restOptions,
|
|
17
|
-
});
|
|
18
|
-
const useNextPagesQuery = createQuery(async (key, state) => {
|
|
19
|
-
const isInitialPage = state.pageParam === undefined;
|
|
20
|
-
const pQuery = usePrevPagesQuery.get(key);
|
|
21
|
-
try {
|
|
22
|
-
const response = await queryFn(key, state, 'next');
|
|
23
|
-
if (isInitialPage) {
|
|
24
|
-
const prevPageParam = getPrevPageParam(response, 1, pQuery);
|
|
25
|
-
usePrevPagesQuery.set(key, (prev) => ({
|
|
26
|
-
pageParams: [prevPageParam, ...prev.pageParams.slice(1)],
|
|
27
|
-
hasNextPage: prev.isLoading ? hasValue(prevPageParam) : prev.hasNextPage,
|
|
28
|
-
}));
|
|
29
|
-
if (!pQuery.isLoading)
|
|
30
|
-
pQuery.forceFetch();
|
|
31
|
-
}
|
|
32
|
-
return response;
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
if (isInitialPage && !pQuery.isLoading)
|
|
36
|
-
pQuery.reset();
|
|
37
|
-
throw error;
|
|
38
|
-
}
|
|
39
|
-
}, {
|
|
40
|
-
getNextPageParam: getNextPageParam,
|
|
41
|
-
select: (response, state) => select(response, state, 'next'),
|
|
42
|
-
...restOptions,
|
|
43
|
-
});
|
|
44
|
-
const useBiDirectionQuery = (...args) => {
|
|
45
|
-
const pQuery = usePrevPagesQuery(...args);
|
|
46
|
-
const nQuery = useNextPagesQuery(...args);
|
|
47
|
-
return {
|
|
48
|
-
...nQuery,
|
|
49
|
-
data: [...(pQuery.data || []), ...(nQuery.data || [])],
|
|
50
|
-
fetchPrevPage: pQuery.fetchNextPage,
|
|
51
|
-
hasPrevPage: pQuery.hasNextPage,
|
|
52
|
-
isWaitingPrevPage: pQuery.isWaitingNextPage || (pQuery.isLoading && pQuery.isWaiting),
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
useBiDirectionQuery.get = () => {
|
|
56
|
-
return {
|
|
57
|
-
prev: usePrevPagesQuery.get(),
|
|
58
|
-
next: useNextPagesQuery.get(),
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
useBiDirectionQuery.setInitialResponse = useNextPagesQuery.setInitialResponse;
|
|
62
|
-
useBiDirectionQuery.reset = () => {
|
|
63
|
-
usePrevPagesQuery.reset();
|
|
64
|
-
useNextPagesQuery.reset();
|
|
65
|
-
};
|
|
66
|
-
useBiDirectionQuery.resetSpecificKey = (key) => {
|
|
67
|
-
usePrevPagesQuery.resetSpecificKey(key);
|
|
68
|
-
useNextPagesQuery.resetSpecificKey(key);
|
|
69
|
-
};
|
|
70
|
-
useBiDirectionQuery.invalidate = useNextPagesQuery.invalidate;
|
|
71
|
-
useBiDirectionQuery.invalidateSpecificKey = useNextPagesQuery.invalidateSpecificKey;
|
|
72
|
-
useBiDirectionQuery.suspend = useNextPagesQuery.suspend;
|
|
73
|
-
return useBiDirectionQuery;
|
|
74
|
-
};
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { InitStoreOptions } from '../vanilla';
|
|
2
|
-
import { UseStore } from './create-store';
|
|
3
|
-
export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
4
|
-
/**
|
|
5
|
-
* Network fetching status.
|
|
6
|
-
*/
|
|
7
|
-
isWaiting: boolean;
|
|
8
|
-
isSuccess: boolean;
|
|
9
|
-
isError: boolean;
|
|
10
|
-
response: TResponse | undefined;
|
|
11
|
-
responseUpdatedAt: number | undefined;
|
|
12
|
-
error: TError | undefined;
|
|
13
|
-
errorUpdatedAt: number | undefined;
|
|
14
|
-
/**
|
|
15
|
-
* Mutate function.
|
|
16
|
-
*
|
|
17
|
-
* @returns Promise that will always get resolved.
|
|
18
|
-
*/
|
|
19
|
-
mutate: TVar extends undefined ? () => Promise<{
|
|
20
|
-
response?: TResponse;
|
|
21
|
-
error?: TError;
|
|
22
|
-
variables?: TVar;
|
|
23
|
-
}> : (variables: TVar) => Promise<{
|
|
24
|
-
response?: TResponse;
|
|
25
|
-
error?: TError;
|
|
26
|
-
variables?: TVar;
|
|
27
|
-
}>;
|
|
28
|
-
};
|
|
29
|
-
export type UseMutation<TVar, TResponse = any, TError = unknown> = UseStore<MutationState<TVar, TResponse, TError>>;
|
|
30
|
-
export type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = InitStoreOptions<MutationState<TVar, TResponse, TError>> & {
|
|
31
|
-
onMutate?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
32
|
-
onSuccess?: (response: TResponse, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
33
|
-
onError?: (error: TError, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
34
|
-
onSettled?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* @see https://floppy-disk.vercel.app/docs/api#createmutation
|
|
38
|
-
*/
|
|
39
|
-
export declare const createMutation: <TVar, TResponse = any, TError = unknown>(mutationFn: (variables: TVar, state: MutationState<TVar, TResponse, TError>) => Promise<TResponse>, options?: CreateMutationOptions<TVar, TResponse, TError>) => UseMutation<TVar, TResponse, TError>;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { noop } from '../utils';
|
|
2
|
-
import { createStore } from './create-store';
|
|
3
|
-
/**
|
|
4
|
-
* @see https://floppy-disk.vercel.app/docs/api#createmutation
|
|
5
|
-
*/
|
|
6
|
-
export const createMutation = (mutationFn, options = {}) => {
|
|
7
|
-
const { onMutate = noop, onSuccess = noop, onError = noop, onSettled = noop, ...createStoreOptions } = options;
|
|
8
|
-
const useMutation = createStore(({ set, get }) => ({
|
|
9
|
-
isWaiting: false,
|
|
10
|
-
isSuccess: false,
|
|
11
|
-
isError: false,
|
|
12
|
-
response: undefined,
|
|
13
|
-
responseUpdatedAt: undefined,
|
|
14
|
-
error: undefined,
|
|
15
|
-
errorUpdatedAt: undefined,
|
|
16
|
-
mutate: ((variables) => {
|
|
17
|
-
set({ isWaiting: true });
|
|
18
|
-
const stateBeforeMutate = get();
|
|
19
|
-
onMutate(variables, stateBeforeMutate);
|
|
20
|
-
return new Promise((resolve) => {
|
|
21
|
-
mutationFn(variables, stateBeforeMutate)
|
|
22
|
-
.then((response) => {
|
|
23
|
-
set({
|
|
24
|
-
isWaiting: false,
|
|
25
|
-
isSuccess: true,
|
|
26
|
-
isError: false,
|
|
27
|
-
response,
|
|
28
|
-
responseUpdatedAt: Date.now(),
|
|
29
|
-
error: undefined,
|
|
30
|
-
errorUpdatedAt: undefined,
|
|
31
|
-
});
|
|
32
|
-
onSuccess(response, variables, stateBeforeMutate);
|
|
33
|
-
resolve({ response, variables });
|
|
34
|
-
})
|
|
35
|
-
.catch((error) => {
|
|
36
|
-
set({
|
|
37
|
-
isWaiting: false,
|
|
38
|
-
isSuccess: false,
|
|
39
|
-
isError: true,
|
|
40
|
-
error,
|
|
41
|
-
errorUpdatedAt: Date.now(),
|
|
42
|
-
});
|
|
43
|
-
onError(error, variables, stateBeforeMutate);
|
|
44
|
-
resolve({ error, variables });
|
|
45
|
-
})
|
|
46
|
-
.finally(() => {
|
|
47
|
-
onSettled(variables, stateBeforeMutate);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
}),
|
|
51
|
-
}), createStoreOptions);
|
|
52
|
-
return useMutation;
|
|
53
|
-
};
|
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
import { FunctionComponent } from 'preact';
|
|
3
|
-
import { Maybe } from '../utils';
|
|
4
|
-
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
5
|
-
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = {
|
|
6
|
-
/**
|
|
7
|
-
* Query store key, an object that will be hashed into a string as a query store identifier.
|
|
8
|
-
*/
|
|
9
|
-
key: TKey;
|
|
10
|
-
/**
|
|
11
|
-
* Query store key hash, a string used as a query store identifier.
|
|
12
|
-
*/
|
|
13
|
-
keyHash: string;
|
|
14
|
-
/**
|
|
15
|
-
* Will only be called if the data is stale or empty.
|
|
16
|
-
*/
|
|
17
|
-
fetch: () => void;
|
|
18
|
-
/**
|
|
19
|
-
* Will be called even if the data is still fresh (not stale).
|
|
20
|
-
*
|
|
21
|
-
* @returns Promise that will always get resolved.
|
|
22
|
-
*/
|
|
23
|
-
forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
24
|
-
/**
|
|
25
|
-
* Fetch next page if has next page.
|
|
26
|
-
*
|
|
27
|
-
* If the data is empty, it will just fetch the first page.
|
|
28
|
-
*
|
|
29
|
-
* You can ignore this if your query is not an infinite query.
|
|
30
|
-
*
|
|
31
|
-
* @returns Promise that will always get resolved.
|
|
32
|
-
*/
|
|
33
|
-
fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
|
|
34
|
-
/**
|
|
35
|
-
* Set query state (data, error, etc) to initial state.
|
|
36
|
-
*/
|
|
37
|
-
reset: () => void;
|
|
38
|
-
/**
|
|
39
|
-
* Optimistic update.
|
|
40
|
-
*
|
|
41
|
-
* @returns function to revert the changes & function to invalidate the query
|
|
42
|
-
*
|
|
43
|
-
* IMPORTANT NOTE: This won't work well on infinite query.
|
|
44
|
-
*/
|
|
45
|
-
optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
|
|
46
|
-
revert: () => void;
|
|
47
|
-
invalidate: () => void;
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* Network fetching status.
|
|
51
|
-
*/
|
|
52
|
-
isWaiting: boolean;
|
|
53
|
-
/**
|
|
54
|
-
* Network fetching status for fetching next page.
|
|
55
|
-
*/
|
|
56
|
-
isWaitingNextPage: boolean;
|
|
57
|
-
isRefetching: boolean;
|
|
58
|
-
isRefetchError: boolean;
|
|
59
|
-
/**
|
|
60
|
-
* Will be `true` if the response/data comes from the previous query key.
|
|
61
|
-
*
|
|
62
|
-
* @see `keepPreviousData` option
|
|
63
|
-
*/
|
|
64
|
-
isPreviousData: boolean;
|
|
65
|
-
isOptimisticData: boolean;
|
|
66
|
-
error: TError | undefined;
|
|
67
|
-
errorUpdatedAt: number | undefined;
|
|
68
|
-
retryCount: number;
|
|
69
|
-
isGoingToRetry: boolean;
|
|
70
|
-
pageParam: Maybe<TPageParam>;
|
|
71
|
-
pageParams: Maybe<TPageParam>[];
|
|
72
|
-
hasNextPage: boolean;
|
|
73
|
-
retryNextPageCount: number;
|
|
74
|
-
isGoingToRetryNextPage: boolean;
|
|
75
|
-
} & ({
|
|
76
|
-
/**
|
|
77
|
-
* Status of the data.
|
|
78
|
-
*
|
|
79
|
-
* `"loading"` = no data.
|
|
80
|
-
*
|
|
81
|
-
* `"success"` = has data.
|
|
82
|
-
*
|
|
83
|
-
* `"error"` = has error and no data.
|
|
84
|
-
*
|
|
85
|
-
* It has no relation with network fetching state.
|
|
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
|
|
89
|
-
*/
|
|
90
|
-
status: 'loading';
|
|
91
|
-
/**
|
|
92
|
-
* Data state, will be `true` if the query has no data.
|
|
93
|
-
*
|
|
94
|
-
* It has no relation with network fetching state.
|
|
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
|
|
98
|
-
*/
|
|
99
|
-
isLoading: true;
|
|
100
|
-
/**
|
|
101
|
-
* Data state, will be `true` if the query has a data.
|
|
102
|
-
*
|
|
103
|
-
* @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
|
|
104
|
-
*/
|
|
105
|
-
isSuccess: false;
|
|
106
|
-
/**
|
|
107
|
-
* Error state, will be `true` if the query has no data but has an error.
|
|
108
|
-
*
|
|
109
|
-
* This will only happened if an error occured after first fetch.
|
|
110
|
-
*
|
|
111
|
-
* If data fetched successfully but then an error occured, `isError` will be `false` but `isRefetchError` will be `true`.
|
|
112
|
-
*
|
|
113
|
-
* @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
|
|
114
|
-
*/
|
|
115
|
-
isError: false;
|
|
116
|
-
data: undefined;
|
|
117
|
-
response: undefined;
|
|
118
|
-
responseUpdatedAt: undefined;
|
|
119
|
-
} | {
|
|
120
|
-
status: 'success';
|
|
121
|
-
isLoading: false;
|
|
122
|
-
isSuccess: true;
|
|
123
|
-
isError: false;
|
|
124
|
-
data: TData;
|
|
125
|
-
response: TResponse;
|
|
126
|
-
responseUpdatedAt: number | undefined;
|
|
127
|
-
} | {
|
|
128
|
-
status: 'error';
|
|
129
|
-
isLoading: false;
|
|
130
|
-
isSuccess: false;
|
|
131
|
-
isError: true;
|
|
132
|
-
data: undefined;
|
|
133
|
-
response: undefined;
|
|
134
|
-
responseUpdatedAt: undefined;
|
|
135
|
-
});
|
|
136
|
-
export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
|
|
137
|
-
select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, 'data' | 'key'>) => TData;
|
|
138
|
-
/**
|
|
139
|
-
* Stale time in miliseconds.
|
|
140
|
-
*
|
|
141
|
-
* Defaults to `3000` (3 seconds).
|
|
142
|
-
*/
|
|
143
|
-
staleTime?: number;
|
|
144
|
-
/**
|
|
145
|
-
* Auto call the query when the component is mounted.
|
|
146
|
-
*
|
|
147
|
-
* Defaults to `true`.
|
|
148
|
-
*
|
|
149
|
-
* - If set to `true`, the query will be called on mount focus **if the data is stale**.
|
|
150
|
-
* - If set to `false`, the query won't be called on mount focus.
|
|
151
|
-
* - If set to `"always"`, the query will be called on mount focus.
|
|
152
|
-
*/
|
|
153
|
-
fetchOnMount?: boolean | 'always' | ((key: TKey) => boolean | 'always');
|
|
154
|
-
/**
|
|
155
|
-
* Defaults to follow the value of `fetchOnMount`.
|
|
156
|
-
*
|
|
157
|
-
* `fetchOnMount` and `fetchOnWindowFocus` can be set to different values.
|
|
158
|
-
* However, if `fetchOnWindowFocus` is not explicitly set, it will mimic the value of `fetchOnMount`.
|
|
159
|
-
*
|
|
160
|
-
* - If set to `true`, the query will be called on window focus **if the data is stale**.
|
|
161
|
-
* - If set to `false`, the query won't be called on window focus.
|
|
162
|
-
* - If set to `"always"`, the query will be called on window focus.
|
|
163
|
-
*/
|
|
164
|
-
fetchOnWindowFocus?: boolean | 'always' | ((key: TKey) => boolean | 'always');
|
|
165
|
-
/**
|
|
166
|
-
* Defaults to follow the value of `fetchOnMount`.
|
|
167
|
-
*
|
|
168
|
-
* `fetchOnMount` and `fetchOnReconnect` can be set to different values.
|
|
169
|
-
* However, if `fetchOnReconnect` is not explicitly set, it will mimic the value of `fetchOnMount`.
|
|
170
|
-
*
|
|
171
|
-
* - If set to `true`, the query will be called on window focus **if the data is stale**.
|
|
172
|
-
* - If set to `false`, the query won't be called on window focus.
|
|
173
|
-
* - If set to `"always"`, the query will be called on window focus.
|
|
174
|
-
*/
|
|
175
|
-
fetchOnReconnect?: boolean | 'always' | ((key: TKey) => boolean | 'always');
|
|
176
|
-
/**
|
|
177
|
-
* If set to `false` or return `false`, the query won't be called in any condition.
|
|
178
|
-
* Auto fetch on mount will be disabled.
|
|
179
|
-
* Manually trigger `fetch` method (returned from `createQuery`) won't work too.
|
|
180
|
-
*
|
|
181
|
-
* Defaults to `true`.
|
|
182
|
-
*/
|
|
183
|
-
enabled?: boolean | ((key: TKey) => boolean);
|
|
184
|
-
/**
|
|
185
|
-
* Number of maximum error retries.
|
|
186
|
-
*
|
|
187
|
-
* Defaults to `1`.
|
|
188
|
-
*/
|
|
189
|
-
retry?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
|
|
190
|
-
/**
|
|
191
|
-
* Error retry delay in miliseconds.
|
|
192
|
-
*
|
|
193
|
-
* Defaults to `2000` (2 seconds).
|
|
194
|
-
*/
|
|
195
|
-
retryDelay?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
|
|
196
|
-
/**
|
|
197
|
-
* Used for lagged query.
|
|
198
|
-
*
|
|
199
|
-
* If set to `true`, then:
|
|
200
|
-
* when the query key changed and there is no `data` in the next query key cache,
|
|
201
|
-
* the previous query key cache `data` will be used while fetching new data.
|
|
202
|
-
*/
|
|
203
|
-
keepPreviousData?: boolean;
|
|
204
|
-
/**
|
|
205
|
-
* Only set this if you have an infinite query.
|
|
206
|
-
*
|
|
207
|
-
* This function should return a variable that will be stored as `pageParam` that can be used when fetching next page.
|
|
208
|
-
*/
|
|
209
|
-
getNextPageParam?: (lastPage: TResponse, index: number, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Maybe<TPageParam>;
|
|
210
|
-
onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
211
|
-
onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
212
|
-
onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
|
|
213
|
-
/**
|
|
214
|
-
* Cache time in miliseconds.
|
|
215
|
-
*
|
|
216
|
-
* When a query becomes inactive (no longer have subscribers), it will be reset after this duration,
|
|
217
|
-
* and the cache data will be garbage collected.
|
|
218
|
-
*
|
|
219
|
-
* Set it to `Infinity` to disable garbage collection.
|
|
220
|
-
*
|
|
221
|
-
* Defaults to `5 * 60 * 1000` (5 minutes).
|
|
222
|
-
*/
|
|
223
|
-
cacheTime?: number;
|
|
224
|
-
/**
|
|
225
|
-
* Polling interval in milliseconds.
|
|
226
|
-
*
|
|
227
|
-
* Disabled by default.
|
|
228
|
-
*
|
|
229
|
-
* If last data fetching is failed, the polling interval will be disabled, and it will use `retry` mechanism instead.
|
|
230
|
-
*
|
|
231
|
-
* @see https://floppy-disk.vercel.app/docs/query/polling
|
|
232
|
-
*/
|
|
233
|
-
refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number | false);
|
|
234
|
-
};
|
|
235
|
-
export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
|
|
236
|
-
/**
|
|
237
|
-
* Set query's initial response.
|
|
238
|
-
*
|
|
239
|
-
* This is used for server-side rendered page or static page.
|
|
240
|
-
*
|
|
241
|
-
* IMPORTANT NOTE: Put this on the root component or parent component, before any component subscribed!
|
|
242
|
-
*/
|
|
243
|
-
setInitialResponse: (options: {
|
|
244
|
-
key?: Maybe<TKey>;
|
|
245
|
-
response: TResponse;
|
|
246
|
-
skipRevalidation?: boolean;
|
|
247
|
-
}) => void;
|
|
248
|
-
/**
|
|
249
|
-
* Set query state (data, error, etc) to initial state.
|
|
250
|
-
*/
|
|
251
|
-
reset: () => void;
|
|
252
|
-
/**
|
|
253
|
-
* Set query state (data, error, etc) to initial state.
|
|
254
|
-
*/
|
|
255
|
-
resetSpecificKey: (key?: Maybe<TKey>) => void;
|
|
256
|
-
/**
|
|
257
|
-
* Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
|
|
258
|
-
*/
|
|
259
|
-
invalidate: () => void;
|
|
260
|
-
/**
|
|
261
|
-
* Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
|
|
262
|
-
*/
|
|
263
|
-
invalidateSpecificKey: (key?: Maybe<TKey>) => void;
|
|
264
|
-
/**
|
|
265
|
-
* Optimistic update.
|
|
266
|
-
*
|
|
267
|
-
* @returns function to revert the changes & function to invalidate the query
|
|
268
|
-
*
|
|
269
|
-
* IMPORTANT NOTE: This won't work well on infinite query.
|
|
270
|
-
*/
|
|
271
|
-
optimisticUpdate: (options: {
|
|
272
|
-
key?: Maybe<TKey>;
|
|
273
|
-
response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse);
|
|
274
|
-
}) => {
|
|
275
|
-
revert: () => void;
|
|
276
|
-
invalidate: () => void;
|
|
277
|
-
};
|
|
278
|
-
/**
|
|
279
|
-
* Use query with suspense mode.
|
|
280
|
-
*/
|
|
281
|
-
suspend: (key?: Maybe<TKey>) => Extract<QueryState<TKey, TResponse, TData, TError, TPageParam>, {
|
|
282
|
-
status: 'success';
|
|
283
|
-
}>;
|
|
284
|
-
Render: (props: {
|
|
285
|
-
queryKey?: Maybe<TKey>;
|
|
286
|
-
loading?: FunctionComponent<Extract<QueryState<TKey, TResponse, TData, TError, TPageParam>, {
|
|
287
|
-
status: 'loading';
|
|
288
|
-
}>>;
|
|
289
|
-
success?: FunctionComponent<Extract<QueryState<TKey, TResponse, TData, TError, TPageParam>, {
|
|
290
|
-
status: 'success';
|
|
291
|
-
}>>;
|
|
292
|
-
error?: FunctionComponent<Extract<QueryState<TKey, TResponse, TData, TError, TPageParam>, {
|
|
293
|
-
status: 'error';
|
|
294
|
-
}>>;
|
|
295
|
-
}) => JSX.Element;
|
|
296
|
-
};
|
|
297
|
-
/**
|
|
298
|
-
* @see https://floppy-disk.vercel.app/docs/api#createquery
|
|
299
|
-
*/
|
|
300
|
-
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>;
|