floppy-disk 2.0.0 → 2.0.1-beta.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/esm/preact/create-mutation.d.ts +12 -6
- package/esm/preact/create-mutation.js +2 -2
- package/esm/preact/create-query.d.ts +45 -25
- package/esm/preact/create-query.js +7 -2
- package/esm/react/create-mutation.d.ts +12 -6
- package/esm/react/create-mutation.js +2 -2
- package/esm/react/create-query.d.ts +45 -25
- package/esm/react/create-query.js +7 -2
- package/lib/preact/create-mutation.d.ts +12 -6
- package/lib/preact/create-mutation.js +2 -2
- package/lib/preact/create-query.d.ts +45 -25
- package/lib/preact/create-query.js +7 -2
- package/lib/react/create-mutation.d.ts +12 -6
- package/lib/react/create-mutation.js +2 -2
- package/lib/react/create-query.d.ts +45 -25
- package/lib/react/create-query.js +7 -2
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { InitStoreOptions } from '../vanilla';
|
|
2
|
+
import { UseStore } from './create-store';
|
|
2
3
|
export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
3
4
|
/**
|
|
4
5
|
* Network fetching status.
|
|
@@ -13,16 +14,21 @@ export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
|
13
14
|
/**
|
|
14
15
|
* Mutate function is a promise that will always resolve.
|
|
15
16
|
*/
|
|
16
|
-
mutate: (
|
|
17
|
+
mutate: TVar extends undefined ? () => Promise<{
|
|
18
|
+
response?: TResponse;
|
|
19
|
+
error?: TError;
|
|
20
|
+
variables?: TVar;
|
|
21
|
+
}> : (variables: TVar) => Promise<{
|
|
17
22
|
response?: TResponse;
|
|
18
23
|
error?: TError;
|
|
19
24
|
variables?: TVar;
|
|
20
25
|
}>;
|
|
21
26
|
};
|
|
27
|
+
export type UseMutation<TVar, TResponse = any, TError = unknown> = UseStore<MutationState<TVar, TResponse, TError>>;
|
|
22
28
|
export type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = InitStoreOptions<MutationState<TVar, TResponse, TError>> & {
|
|
23
|
-
onMutate?: (variables: TVar
|
|
24
|
-
onSuccess?: (response: TResponse, variables: TVar
|
|
25
|
-
onError?: (error: TError, variables: TVar
|
|
26
|
-
onSettled?: (variables: TVar
|
|
29
|
+
onMutate?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
30
|
+
onSuccess?: (response: TResponse, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
31
|
+
onError?: (error: TError, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
32
|
+
onSettled?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
27
33
|
};
|
|
28
|
-
export declare const createMutation: <TVar, TResponse = any, TError = unknown>(mutationFn: (variables: TVar
|
|
34
|
+
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>;
|
|
@@ -10,7 +10,7 @@ export const createMutation = (mutationFn, options = {}) => {
|
|
|
10
10
|
responseUpdatedAt: null,
|
|
11
11
|
error: null,
|
|
12
12
|
errorUpdatedAt: null,
|
|
13
|
-
mutate: (variables) => {
|
|
13
|
+
mutate: ((variables) => {
|
|
14
14
|
set({ isWaiting: true });
|
|
15
15
|
const stateBeforeMutate = get();
|
|
16
16
|
onMutate(variables, stateBeforeMutate);
|
|
@@ -44,7 +44,7 @@ export const createMutation = (mutationFn, options = {}) => {
|
|
|
44
44
|
onSettled(variables, stateBeforeMutate);
|
|
45
45
|
});
|
|
46
46
|
});
|
|
47
|
-
},
|
|
47
|
+
}),
|
|
48
48
|
}), createStoreOptions);
|
|
49
49
|
return useMutation;
|
|
50
50
|
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
2
|
-
export type QueryStatus = 'loading' | 'success' | 'error';
|
|
3
2
|
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
4
3
|
/**
|
|
5
4
|
* Query store key, an object that will be hashed into a string as a query store identifier.
|
|
@@ -44,6 +43,20 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
44
43
|
* Network fetching status for fetching next page.
|
|
45
44
|
*/
|
|
46
45
|
isWaitingNextPage: boolean;
|
|
46
|
+
isRefetching: boolean;
|
|
47
|
+
isRefetchError: boolean;
|
|
48
|
+
isPreviousData: boolean;
|
|
49
|
+
isOptimisticData: boolean;
|
|
50
|
+
error: TError | null;
|
|
51
|
+
errorUpdatedAt: number | null;
|
|
52
|
+
retryCount: number;
|
|
53
|
+
isGoingToRetry: boolean;
|
|
54
|
+
pageParam: any;
|
|
55
|
+
pageParams: any[];
|
|
56
|
+
hasNextPage: boolean;
|
|
57
|
+
retryNextPageCount: number;
|
|
58
|
+
isGoingToRetryNextPage: boolean;
|
|
59
|
+
} & ({
|
|
47
60
|
/**
|
|
48
61
|
* Status of the data.
|
|
49
62
|
*
|
|
@@ -56,39 +69,46 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
56
69
|
* It has no relation with network fetching state.
|
|
57
70
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
58
71
|
*/
|
|
59
|
-
status:
|
|
72
|
+
status: 'loading';
|
|
60
73
|
/**
|
|
61
74
|
* Data state, will be `true` if the query has no data.
|
|
62
75
|
*
|
|
63
76
|
* It has no relation with network fetching state.
|
|
64
77
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
65
78
|
*/
|
|
66
|
-
isLoading:
|
|
79
|
+
isLoading: true;
|
|
67
80
|
/**
|
|
68
81
|
* Data state, will be `true` if the query has a data.
|
|
69
82
|
*/
|
|
70
|
-
isSuccess:
|
|
71
|
-
/**
|
|
72
|
-
* Error state, will be `true`
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
data:
|
|
80
|
-
response:
|
|
81
|
-
responseUpdatedAt:
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
83
|
+
isSuccess: false;
|
|
84
|
+
/**
|
|
85
|
+
* Error state, will be `true` if the query has no data but has an error.
|
|
86
|
+
*
|
|
87
|
+
* This will only happened if an error occured after first fetch.
|
|
88
|
+
*
|
|
89
|
+
* If data fetched successfully but then an error occured, `isError` will be `false` but `isRefetchError` will be `true`.
|
|
90
|
+
*/
|
|
91
|
+
isError: false;
|
|
92
|
+
data: null;
|
|
93
|
+
response: null;
|
|
94
|
+
responseUpdatedAt: null;
|
|
95
|
+
} | {
|
|
96
|
+
status: 'success';
|
|
97
|
+
isLoading: false;
|
|
98
|
+
isSuccess: true;
|
|
99
|
+
isError: false;
|
|
100
|
+
data: TData;
|
|
101
|
+
response: TResponse;
|
|
102
|
+
responseUpdatedAt: number;
|
|
103
|
+
} | {
|
|
104
|
+
status: 'error';
|
|
105
|
+
isLoading: false;
|
|
106
|
+
isSuccess: false;
|
|
107
|
+
isError: true;
|
|
108
|
+
data: null;
|
|
109
|
+
response: null;
|
|
110
|
+
responseUpdatedAt: null;
|
|
111
|
+
});
|
|
92
112
|
export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError>> & {
|
|
93
113
|
select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError>, 'data' | 'key'>) => TData;
|
|
94
114
|
/**
|
|
@@ -116,7 +116,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
116
116
|
const prevState = get();
|
|
117
117
|
const errorUpdatedAt = Date.now();
|
|
118
118
|
const { shouldRetry, delay } = getRetryProps(error, prevState.retryCount);
|
|
119
|
-
set(prevState.isSuccess
|
|
119
|
+
set(prevState.isSuccess && !prevState.isPreviousData
|
|
120
120
|
? {
|
|
121
121
|
isWaiting: false,
|
|
122
122
|
isRefetching: false,
|
|
@@ -134,7 +134,9 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
134
134
|
}
|
|
135
135
|
: {
|
|
136
136
|
isWaiting: false,
|
|
137
|
+
status: 'error',
|
|
137
138
|
isError: true,
|
|
139
|
+
data: null,
|
|
138
140
|
error,
|
|
139
141
|
errorUpdatedAt,
|
|
140
142
|
isGoingToRetry: shouldRetry,
|
|
@@ -253,9 +255,12 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
253
255
|
const prevData = useQuery.get(prevKey);
|
|
254
256
|
if (prevData.data) {
|
|
255
257
|
useQuery.set(nextKey, {
|
|
258
|
+
status: 'success',
|
|
259
|
+
isLoading: false,
|
|
260
|
+
isSuccess: true,
|
|
261
|
+
isError: false,
|
|
256
262
|
data: prevData.data,
|
|
257
263
|
response: prevData.response,
|
|
258
|
-
isLoading: false,
|
|
259
264
|
isPreviousData: true,
|
|
260
265
|
}, true);
|
|
261
266
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { InitStoreOptions } from '../vanilla';
|
|
2
|
+
import { UseStore } from './create-store';
|
|
2
3
|
export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
3
4
|
/**
|
|
4
5
|
* Network fetching status.
|
|
@@ -13,16 +14,21 @@ export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
|
13
14
|
/**
|
|
14
15
|
* Mutate function is a promise that will always resolve.
|
|
15
16
|
*/
|
|
16
|
-
mutate: (
|
|
17
|
+
mutate: TVar extends undefined ? () => Promise<{
|
|
18
|
+
response?: TResponse;
|
|
19
|
+
error?: TError;
|
|
20
|
+
variables?: TVar;
|
|
21
|
+
}> : (variables: TVar) => Promise<{
|
|
17
22
|
response?: TResponse;
|
|
18
23
|
error?: TError;
|
|
19
24
|
variables?: TVar;
|
|
20
25
|
}>;
|
|
21
26
|
};
|
|
27
|
+
export type UseMutation<TVar, TResponse = any, TError = unknown> = UseStore<MutationState<TVar, TResponse, TError>>;
|
|
22
28
|
export type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = InitStoreOptions<MutationState<TVar, TResponse, TError>> & {
|
|
23
|
-
onMutate?: (variables: TVar
|
|
24
|
-
onSuccess?: (response: TResponse, variables: TVar
|
|
25
|
-
onError?: (error: TError, variables: TVar
|
|
26
|
-
onSettled?: (variables: TVar
|
|
29
|
+
onMutate?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
30
|
+
onSuccess?: (response: TResponse, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
31
|
+
onError?: (error: TError, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
32
|
+
onSettled?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
27
33
|
};
|
|
28
|
-
export declare const createMutation: <TVar, TResponse = any, TError = unknown>(mutationFn: (variables: TVar
|
|
34
|
+
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>;
|
|
@@ -10,7 +10,7 @@ export const createMutation = (mutationFn, options = {}) => {
|
|
|
10
10
|
responseUpdatedAt: null,
|
|
11
11
|
error: null,
|
|
12
12
|
errorUpdatedAt: null,
|
|
13
|
-
mutate: (variables) => {
|
|
13
|
+
mutate: ((variables) => {
|
|
14
14
|
set({ isWaiting: true });
|
|
15
15
|
const stateBeforeMutate = get();
|
|
16
16
|
onMutate(variables, stateBeforeMutate);
|
|
@@ -44,7 +44,7 @@ export const createMutation = (mutationFn, options = {}) => {
|
|
|
44
44
|
onSettled(variables, stateBeforeMutate);
|
|
45
45
|
});
|
|
46
46
|
});
|
|
47
|
-
},
|
|
47
|
+
}),
|
|
48
48
|
}), createStoreOptions);
|
|
49
49
|
return useMutation;
|
|
50
50
|
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
2
|
-
export type QueryStatus = 'loading' | 'success' | 'error';
|
|
3
2
|
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
4
3
|
/**
|
|
5
4
|
* Query store key, an object that will be hashed into a string as a query store identifier.
|
|
@@ -44,6 +43,20 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
44
43
|
* Network fetching status for fetching next page.
|
|
45
44
|
*/
|
|
46
45
|
isWaitingNextPage: boolean;
|
|
46
|
+
isRefetching: boolean;
|
|
47
|
+
isRefetchError: boolean;
|
|
48
|
+
isPreviousData: boolean;
|
|
49
|
+
isOptimisticData: boolean;
|
|
50
|
+
error: TError | null;
|
|
51
|
+
errorUpdatedAt: number | null;
|
|
52
|
+
retryCount: number;
|
|
53
|
+
isGoingToRetry: boolean;
|
|
54
|
+
pageParam: any;
|
|
55
|
+
pageParams: any[];
|
|
56
|
+
hasNextPage: boolean;
|
|
57
|
+
retryNextPageCount: number;
|
|
58
|
+
isGoingToRetryNextPage: boolean;
|
|
59
|
+
} & ({
|
|
47
60
|
/**
|
|
48
61
|
* Status of the data.
|
|
49
62
|
*
|
|
@@ -56,39 +69,46 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
56
69
|
* It has no relation with network fetching state.
|
|
57
70
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
58
71
|
*/
|
|
59
|
-
status:
|
|
72
|
+
status: 'loading';
|
|
60
73
|
/**
|
|
61
74
|
* Data state, will be `true` if the query has no data.
|
|
62
75
|
*
|
|
63
76
|
* It has no relation with network fetching state.
|
|
64
77
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
65
78
|
*/
|
|
66
|
-
isLoading:
|
|
79
|
+
isLoading: true;
|
|
67
80
|
/**
|
|
68
81
|
* Data state, will be `true` if the query has a data.
|
|
69
82
|
*/
|
|
70
|
-
isSuccess:
|
|
71
|
-
/**
|
|
72
|
-
* Error state, will be `true`
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
data:
|
|
80
|
-
response:
|
|
81
|
-
responseUpdatedAt:
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
83
|
+
isSuccess: false;
|
|
84
|
+
/**
|
|
85
|
+
* Error state, will be `true` if the query has no data but has an error.
|
|
86
|
+
*
|
|
87
|
+
* This will only happened if an error occured after first fetch.
|
|
88
|
+
*
|
|
89
|
+
* If data fetched successfully but then an error occured, `isError` will be `false` but `isRefetchError` will be `true`.
|
|
90
|
+
*/
|
|
91
|
+
isError: false;
|
|
92
|
+
data: null;
|
|
93
|
+
response: null;
|
|
94
|
+
responseUpdatedAt: null;
|
|
95
|
+
} | {
|
|
96
|
+
status: 'success';
|
|
97
|
+
isLoading: false;
|
|
98
|
+
isSuccess: true;
|
|
99
|
+
isError: false;
|
|
100
|
+
data: TData;
|
|
101
|
+
response: TResponse;
|
|
102
|
+
responseUpdatedAt: number;
|
|
103
|
+
} | {
|
|
104
|
+
status: 'error';
|
|
105
|
+
isLoading: false;
|
|
106
|
+
isSuccess: false;
|
|
107
|
+
isError: true;
|
|
108
|
+
data: null;
|
|
109
|
+
response: null;
|
|
110
|
+
responseUpdatedAt: null;
|
|
111
|
+
});
|
|
92
112
|
export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError>> & {
|
|
93
113
|
select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError>, 'data' | 'key'>) => TData;
|
|
94
114
|
/**
|
|
@@ -116,7 +116,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
116
116
|
const prevState = get();
|
|
117
117
|
const errorUpdatedAt = Date.now();
|
|
118
118
|
const { shouldRetry, delay } = getRetryProps(error, prevState.retryCount);
|
|
119
|
-
set(prevState.isSuccess
|
|
119
|
+
set(prevState.isSuccess && !prevState.isPreviousData
|
|
120
120
|
? {
|
|
121
121
|
isWaiting: false,
|
|
122
122
|
isRefetching: false,
|
|
@@ -134,7 +134,9 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
134
134
|
}
|
|
135
135
|
: {
|
|
136
136
|
isWaiting: false,
|
|
137
|
+
status: 'error',
|
|
137
138
|
isError: true,
|
|
139
|
+
data: null,
|
|
138
140
|
error,
|
|
139
141
|
errorUpdatedAt,
|
|
140
142
|
isGoingToRetry: shouldRetry,
|
|
@@ -253,9 +255,12 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
253
255
|
const prevData = useQuery.get(prevKey);
|
|
254
256
|
if (prevData.data) {
|
|
255
257
|
useQuery.set(nextKey, {
|
|
258
|
+
status: 'success',
|
|
259
|
+
isLoading: false,
|
|
260
|
+
isSuccess: true,
|
|
261
|
+
isError: false,
|
|
256
262
|
data: prevData.data,
|
|
257
263
|
response: prevData.response,
|
|
258
|
-
isLoading: false,
|
|
259
264
|
isPreviousData: true,
|
|
260
265
|
}, true);
|
|
261
266
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { InitStoreOptions } from '../vanilla';
|
|
2
|
+
import { UseStore } from './create-store';
|
|
2
3
|
export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
3
4
|
/**
|
|
4
5
|
* Network fetching status.
|
|
@@ -13,16 +14,21 @@ export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
|
13
14
|
/**
|
|
14
15
|
* Mutate function is a promise that will always resolve.
|
|
15
16
|
*/
|
|
16
|
-
mutate: (
|
|
17
|
+
mutate: TVar extends undefined ? () => Promise<{
|
|
18
|
+
response?: TResponse;
|
|
19
|
+
error?: TError;
|
|
20
|
+
variables?: TVar;
|
|
21
|
+
}> : (variables: TVar) => Promise<{
|
|
17
22
|
response?: TResponse;
|
|
18
23
|
error?: TError;
|
|
19
24
|
variables?: TVar;
|
|
20
25
|
}>;
|
|
21
26
|
};
|
|
27
|
+
export type UseMutation<TVar, TResponse = any, TError = unknown> = UseStore<MutationState<TVar, TResponse, TError>>;
|
|
22
28
|
export type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = InitStoreOptions<MutationState<TVar, TResponse, TError>> & {
|
|
23
|
-
onMutate?: (variables: TVar
|
|
24
|
-
onSuccess?: (response: TResponse, variables: TVar
|
|
25
|
-
onError?: (error: TError, variables: TVar
|
|
26
|
-
onSettled?: (variables: TVar
|
|
29
|
+
onMutate?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
30
|
+
onSuccess?: (response: TResponse, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
31
|
+
onError?: (error: TError, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
32
|
+
onSettled?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
27
33
|
};
|
|
28
|
-
export declare const createMutation: <TVar, TResponse = any, TError = unknown>(mutationFn: (variables: TVar
|
|
34
|
+
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>;
|
|
@@ -13,7 +13,7 @@ const createMutation = (mutationFn, options = {}) => {
|
|
|
13
13
|
responseUpdatedAt: null,
|
|
14
14
|
error: null,
|
|
15
15
|
errorUpdatedAt: null,
|
|
16
|
-
mutate: (variables) => {
|
|
16
|
+
mutate: ((variables) => {
|
|
17
17
|
set({ isWaiting: true });
|
|
18
18
|
const stateBeforeMutate = get();
|
|
19
19
|
onMutate(variables, stateBeforeMutate);
|
|
@@ -47,7 +47,7 @@ const createMutation = (mutationFn, options = {}) => {
|
|
|
47
47
|
onSettled(variables, stateBeforeMutate);
|
|
48
48
|
});
|
|
49
49
|
});
|
|
50
|
-
},
|
|
50
|
+
}),
|
|
51
51
|
}), createStoreOptions);
|
|
52
52
|
return useMutation;
|
|
53
53
|
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
2
|
-
export type QueryStatus = 'loading' | 'success' | 'error';
|
|
3
2
|
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
4
3
|
/**
|
|
5
4
|
* Query store key, an object that will be hashed into a string as a query store identifier.
|
|
@@ -44,6 +43,20 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
44
43
|
* Network fetching status for fetching next page.
|
|
45
44
|
*/
|
|
46
45
|
isWaitingNextPage: boolean;
|
|
46
|
+
isRefetching: boolean;
|
|
47
|
+
isRefetchError: boolean;
|
|
48
|
+
isPreviousData: boolean;
|
|
49
|
+
isOptimisticData: boolean;
|
|
50
|
+
error: TError | null;
|
|
51
|
+
errorUpdatedAt: number | null;
|
|
52
|
+
retryCount: number;
|
|
53
|
+
isGoingToRetry: boolean;
|
|
54
|
+
pageParam: any;
|
|
55
|
+
pageParams: any[];
|
|
56
|
+
hasNextPage: boolean;
|
|
57
|
+
retryNextPageCount: number;
|
|
58
|
+
isGoingToRetryNextPage: boolean;
|
|
59
|
+
} & ({
|
|
47
60
|
/**
|
|
48
61
|
* Status of the data.
|
|
49
62
|
*
|
|
@@ -56,39 +69,46 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
56
69
|
* It has no relation with network fetching state.
|
|
57
70
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
58
71
|
*/
|
|
59
|
-
status:
|
|
72
|
+
status: 'loading';
|
|
60
73
|
/**
|
|
61
74
|
* Data state, will be `true` if the query has no data.
|
|
62
75
|
*
|
|
63
76
|
* It has no relation with network fetching state.
|
|
64
77
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
65
78
|
*/
|
|
66
|
-
isLoading:
|
|
79
|
+
isLoading: true;
|
|
67
80
|
/**
|
|
68
81
|
* Data state, will be `true` if the query has a data.
|
|
69
82
|
*/
|
|
70
|
-
isSuccess:
|
|
71
|
-
/**
|
|
72
|
-
* Error state, will be `true`
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
data:
|
|
80
|
-
response:
|
|
81
|
-
responseUpdatedAt:
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
83
|
+
isSuccess: false;
|
|
84
|
+
/**
|
|
85
|
+
* Error state, will be `true` if the query has no data but has an error.
|
|
86
|
+
*
|
|
87
|
+
* This will only happened if an error occured after first fetch.
|
|
88
|
+
*
|
|
89
|
+
* If data fetched successfully but then an error occured, `isError` will be `false` but `isRefetchError` will be `true`.
|
|
90
|
+
*/
|
|
91
|
+
isError: false;
|
|
92
|
+
data: null;
|
|
93
|
+
response: null;
|
|
94
|
+
responseUpdatedAt: null;
|
|
95
|
+
} | {
|
|
96
|
+
status: 'success';
|
|
97
|
+
isLoading: false;
|
|
98
|
+
isSuccess: true;
|
|
99
|
+
isError: false;
|
|
100
|
+
data: TData;
|
|
101
|
+
response: TResponse;
|
|
102
|
+
responseUpdatedAt: number;
|
|
103
|
+
} | {
|
|
104
|
+
status: 'error';
|
|
105
|
+
isLoading: false;
|
|
106
|
+
isSuccess: false;
|
|
107
|
+
isError: true;
|
|
108
|
+
data: null;
|
|
109
|
+
response: null;
|
|
110
|
+
responseUpdatedAt: null;
|
|
111
|
+
});
|
|
92
112
|
export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError>> & {
|
|
93
113
|
select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError>, 'data' | 'key'>) => TData;
|
|
94
114
|
/**
|
|
@@ -119,7 +119,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
119
119
|
const prevState = get();
|
|
120
120
|
const errorUpdatedAt = Date.now();
|
|
121
121
|
const { shouldRetry, delay } = getRetryProps(error, prevState.retryCount);
|
|
122
|
-
set(prevState.isSuccess
|
|
122
|
+
set(prevState.isSuccess && !prevState.isPreviousData
|
|
123
123
|
? {
|
|
124
124
|
isWaiting: false,
|
|
125
125
|
isRefetching: false,
|
|
@@ -137,7 +137,9 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
137
137
|
}
|
|
138
138
|
: {
|
|
139
139
|
isWaiting: false,
|
|
140
|
+
status: 'error',
|
|
140
141
|
isError: true,
|
|
142
|
+
data: null,
|
|
141
143
|
error,
|
|
142
144
|
errorUpdatedAt,
|
|
143
145
|
isGoingToRetry: shouldRetry,
|
|
@@ -256,9 +258,12 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
256
258
|
const prevData = useQuery.get(prevKey);
|
|
257
259
|
if (prevData.data) {
|
|
258
260
|
useQuery.set(nextKey, {
|
|
261
|
+
status: 'success',
|
|
262
|
+
isLoading: false,
|
|
263
|
+
isSuccess: true,
|
|
264
|
+
isError: false,
|
|
259
265
|
data: prevData.data,
|
|
260
266
|
response: prevData.response,
|
|
261
|
-
isLoading: false,
|
|
262
267
|
isPreviousData: true,
|
|
263
268
|
}, true);
|
|
264
269
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { InitStoreOptions } from '../vanilla';
|
|
2
|
+
import { UseStore } from './create-store';
|
|
2
3
|
export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
3
4
|
/**
|
|
4
5
|
* Network fetching status.
|
|
@@ -13,16 +14,21 @@ export type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
|
13
14
|
/**
|
|
14
15
|
* Mutate function is a promise that will always resolve.
|
|
15
16
|
*/
|
|
16
|
-
mutate: (
|
|
17
|
+
mutate: TVar extends undefined ? () => Promise<{
|
|
18
|
+
response?: TResponse;
|
|
19
|
+
error?: TError;
|
|
20
|
+
variables?: TVar;
|
|
21
|
+
}> : (variables: TVar) => Promise<{
|
|
17
22
|
response?: TResponse;
|
|
18
23
|
error?: TError;
|
|
19
24
|
variables?: TVar;
|
|
20
25
|
}>;
|
|
21
26
|
};
|
|
27
|
+
export type UseMutation<TVar, TResponse = any, TError = unknown> = UseStore<MutationState<TVar, TResponse, TError>>;
|
|
22
28
|
export type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = InitStoreOptions<MutationState<TVar, TResponse, TError>> & {
|
|
23
|
-
onMutate?: (variables: TVar
|
|
24
|
-
onSuccess?: (response: TResponse, variables: TVar
|
|
25
|
-
onError?: (error: TError, variables: TVar
|
|
26
|
-
onSettled?: (variables: TVar
|
|
29
|
+
onMutate?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
30
|
+
onSuccess?: (response: TResponse, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
31
|
+
onError?: (error: TError, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
32
|
+
onSettled?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
27
33
|
};
|
|
28
|
-
export declare const createMutation: <TVar, TResponse = any, TError = unknown>(mutationFn: (variables: TVar
|
|
34
|
+
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>;
|
|
@@ -13,7 +13,7 @@ const createMutation = (mutationFn, options = {}) => {
|
|
|
13
13
|
responseUpdatedAt: null,
|
|
14
14
|
error: null,
|
|
15
15
|
errorUpdatedAt: null,
|
|
16
|
-
mutate: (variables) => {
|
|
16
|
+
mutate: ((variables) => {
|
|
17
17
|
set({ isWaiting: true });
|
|
18
18
|
const stateBeforeMutate = get();
|
|
19
19
|
onMutate(variables, stateBeforeMutate);
|
|
@@ -47,7 +47,7 @@ const createMutation = (mutationFn, options = {}) => {
|
|
|
47
47
|
onSettled(variables, stateBeforeMutate);
|
|
48
48
|
});
|
|
49
49
|
});
|
|
50
|
-
},
|
|
50
|
+
}),
|
|
51
51
|
}), createStoreOptions);
|
|
52
52
|
return useMutation;
|
|
53
53
|
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
|
|
2
|
-
export type QueryStatus = 'loading' | 'success' | 'error';
|
|
3
2
|
export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
|
|
4
3
|
/**
|
|
5
4
|
* Query store key, an object that will be hashed into a string as a query store identifier.
|
|
@@ -44,6 +43,20 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
44
43
|
* Network fetching status for fetching next page.
|
|
45
44
|
*/
|
|
46
45
|
isWaitingNextPage: boolean;
|
|
46
|
+
isRefetching: boolean;
|
|
47
|
+
isRefetchError: boolean;
|
|
48
|
+
isPreviousData: boolean;
|
|
49
|
+
isOptimisticData: boolean;
|
|
50
|
+
error: TError | null;
|
|
51
|
+
errorUpdatedAt: number | null;
|
|
52
|
+
retryCount: number;
|
|
53
|
+
isGoingToRetry: boolean;
|
|
54
|
+
pageParam: any;
|
|
55
|
+
pageParams: any[];
|
|
56
|
+
hasNextPage: boolean;
|
|
57
|
+
retryNextPageCount: number;
|
|
58
|
+
isGoingToRetryNextPage: boolean;
|
|
59
|
+
} & ({
|
|
47
60
|
/**
|
|
48
61
|
* Status of the data.
|
|
49
62
|
*
|
|
@@ -56,39 +69,46 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
|
|
|
56
69
|
* It has no relation with network fetching state.
|
|
57
70
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
58
71
|
*/
|
|
59
|
-
status:
|
|
72
|
+
status: 'loading';
|
|
60
73
|
/**
|
|
61
74
|
* Data state, will be `true` if the query has no data.
|
|
62
75
|
*
|
|
63
76
|
* It has no relation with network fetching state.
|
|
64
77
|
* If you're looking for network fetching state, use `isWaiting` instead.
|
|
65
78
|
*/
|
|
66
|
-
isLoading:
|
|
79
|
+
isLoading: true;
|
|
67
80
|
/**
|
|
68
81
|
* Data state, will be `true` if the query has a data.
|
|
69
82
|
*/
|
|
70
|
-
isSuccess:
|
|
71
|
-
/**
|
|
72
|
-
* Error state, will be `true`
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
data:
|
|
80
|
-
response:
|
|
81
|
-
responseUpdatedAt:
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
83
|
+
isSuccess: false;
|
|
84
|
+
/**
|
|
85
|
+
* Error state, will be `true` if the query has no data but has an error.
|
|
86
|
+
*
|
|
87
|
+
* This will only happened if an error occured after first fetch.
|
|
88
|
+
*
|
|
89
|
+
* If data fetched successfully but then an error occured, `isError` will be `false` but `isRefetchError` will be `true`.
|
|
90
|
+
*/
|
|
91
|
+
isError: false;
|
|
92
|
+
data: null;
|
|
93
|
+
response: null;
|
|
94
|
+
responseUpdatedAt: null;
|
|
95
|
+
} | {
|
|
96
|
+
status: 'success';
|
|
97
|
+
isLoading: false;
|
|
98
|
+
isSuccess: true;
|
|
99
|
+
isError: false;
|
|
100
|
+
data: TData;
|
|
101
|
+
response: TResponse;
|
|
102
|
+
responseUpdatedAt: number;
|
|
103
|
+
} | {
|
|
104
|
+
status: 'error';
|
|
105
|
+
isLoading: false;
|
|
106
|
+
isSuccess: false;
|
|
107
|
+
isError: true;
|
|
108
|
+
data: null;
|
|
109
|
+
response: null;
|
|
110
|
+
responseUpdatedAt: null;
|
|
111
|
+
});
|
|
92
112
|
export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError>> & {
|
|
93
113
|
select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError>, 'data' | 'key'>) => TData;
|
|
94
114
|
/**
|
|
@@ -119,7 +119,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
119
119
|
const prevState = get();
|
|
120
120
|
const errorUpdatedAt = Date.now();
|
|
121
121
|
const { shouldRetry, delay } = getRetryProps(error, prevState.retryCount);
|
|
122
|
-
set(prevState.isSuccess
|
|
122
|
+
set(prevState.isSuccess && !prevState.isPreviousData
|
|
123
123
|
? {
|
|
124
124
|
isWaiting: false,
|
|
125
125
|
isRefetching: false,
|
|
@@ -137,7 +137,9 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
137
137
|
}
|
|
138
138
|
: {
|
|
139
139
|
isWaiting: false,
|
|
140
|
+
status: 'error',
|
|
140
141
|
isError: true,
|
|
142
|
+
data: null,
|
|
141
143
|
error,
|
|
142
144
|
errorUpdatedAt,
|
|
143
145
|
isGoingToRetry: shouldRetry,
|
|
@@ -256,9 +258,12 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
256
258
|
const prevData = useQuery.get(prevKey);
|
|
257
259
|
if (prevData.data) {
|
|
258
260
|
useQuery.set(nextKey, {
|
|
261
|
+
status: 'success',
|
|
262
|
+
isLoading: false,
|
|
263
|
+
isSuccess: true,
|
|
264
|
+
isError: false,
|
|
259
265
|
data: prevData.data,
|
|
260
266
|
response: prevData.response,
|
|
261
|
-
isLoading: false,
|
|
262
267
|
isPreviousData: true,
|
|
263
268
|
}, true);
|
|
264
269
|
}
|