floppy-disk 1.1.0 → 1.1.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/react/create-mutation.d.ts +12 -5
- package/esm/react/create-mutation.js +12 -10
- package/esm/react/create-query.d.ts +3 -3
- package/esm/react/create-query.js +5 -5
- package/lib/react/create-mutation.d.ts +12 -5
- package/lib/react/create-mutation.js +12 -10
- package/lib/react/create-query.d.ts +3 -3
- package/lib/react/create-query.js +5 -5
- package/package.json +1 -1
|
@@ -10,12 +10,19 @@ export declare type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
|
10
10
|
responseUpdatedAt: number | null;
|
|
11
11
|
error: TError | null;
|
|
12
12
|
errorUpdatedAt: number | null;
|
|
13
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Mutate function is a promise that will always resolve.
|
|
15
|
+
*/
|
|
16
|
+
mutate: (variables?: TVar) => Promise<{
|
|
17
|
+
response?: TResponse;
|
|
18
|
+
error?: TError;
|
|
19
|
+
variables?: TVar;
|
|
20
|
+
}>;
|
|
14
21
|
};
|
|
15
22
|
export declare type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = InitStoreOptions<MutationState<TVar, TResponse, TError>> & {
|
|
16
|
-
onMutate?: (variables: TVar | undefined,
|
|
17
|
-
onSuccess?: (response: TResponse, variables: TVar | undefined,
|
|
18
|
-
onError?: (error: TError, variables: TVar | undefined,
|
|
19
|
-
onSettled?: (variables: TVar | undefined,
|
|
23
|
+
onMutate?: (variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
24
|
+
onSuccess?: (response: TResponse, variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
25
|
+
onError?: (error: TError, variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
26
|
+
onSettled?: (variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
20
27
|
};
|
|
21
28
|
export declare const createMutation: <TVar, TResponse = any, TError = unknown>(mutationFn: (variables: TVar | undefined, state: MutationState<TVar, TResponse, TError>) => Promise<TResponse>, options?: CreateMutationOptions<TVar, TResponse, TError>) => import("./create-store").UseStore<MutationState<TVar, TResponse, TError>>;
|
|
@@ -12,34 +12,36 @@ export const createMutation = (mutationFn, options = {}) => {
|
|
|
12
12
|
errorUpdatedAt: null,
|
|
13
13
|
mutate: (variables) => {
|
|
14
14
|
set({ isWaiting: true });
|
|
15
|
-
const
|
|
16
|
-
onMutate(variables,
|
|
17
|
-
return new Promise((resolve
|
|
18
|
-
mutationFn(variables,
|
|
15
|
+
const stateBeforeMutate = get();
|
|
16
|
+
onMutate(variables, stateBeforeMutate);
|
|
17
|
+
return new Promise((resolve) => {
|
|
18
|
+
mutationFn(variables, stateBeforeMutate)
|
|
19
19
|
.then((response) => {
|
|
20
20
|
set({
|
|
21
21
|
isWaiting: false,
|
|
22
22
|
isSuccess: true,
|
|
23
|
+
isError: false,
|
|
23
24
|
response,
|
|
24
25
|
responseUpdatedAt: Date.now(),
|
|
25
26
|
error: null,
|
|
26
27
|
errorUpdatedAt: null,
|
|
27
28
|
});
|
|
28
|
-
onSuccess(response, variables,
|
|
29
|
-
resolve(response);
|
|
29
|
+
onSuccess(response, variables, stateBeforeMutate);
|
|
30
|
+
resolve({ response, variables });
|
|
30
31
|
})
|
|
31
32
|
.catch((error) => {
|
|
32
33
|
set({
|
|
33
|
-
isWaiting:
|
|
34
|
+
isWaiting: false,
|
|
35
|
+
isSuccess: false,
|
|
34
36
|
isError: true,
|
|
35
37
|
error,
|
|
36
38
|
errorUpdatedAt: Date.now(),
|
|
37
39
|
});
|
|
38
|
-
onError(error, variables,
|
|
39
|
-
|
|
40
|
+
onError(error, variables, stateBeforeMutate);
|
|
41
|
+
resolve({ error, variables });
|
|
40
42
|
})
|
|
41
43
|
.finally(() => {
|
|
42
|
-
onSettled(variables,
|
|
44
|
+
onSettled(variables, stateBeforeMutate);
|
|
43
45
|
});
|
|
44
46
|
});
|
|
45
47
|
},
|
|
@@ -147,8 +147,8 @@ export declare type CreateQueryOptions<TKey extends StoreKey = StoreKey, TRespon
|
|
|
147
147
|
* This function should return a variable that will be used when fetching next page (`pageParam`).
|
|
148
148
|
*/
|
|
149
149
|
getNextPageParam?: (lastPage: TResponse, index: number) => any;
|
|
150
|
-
onSuccess?: (response: TResponse,
|
|
151
|
-
onError?: (error: TError,
|
|
152
|
-
onSettled?: (
|
|
150
|
+
onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
151
|
+
onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
152
|
+
onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
153
153
|
};
|
|
154
154
|
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>) => import("./create-stores").UseStores<TKey, QueryState<TKey, TResponse, TData, TError>>;
|
|
@@ -58,8 +58,8 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
58
58
|
else
|
|
59
59
|
set({ isGoingToRetry: false, isWaiting: true, isRefetching: true });
|
|
60
60
|
}
|
|
61
|
-
const
|
|
62
|
-
queryFn(key,
|
|
61
|
+
const stateBeforeCallQuery = { ...get(), pageParam };
|
|
62
|
+
queryFn(key, stateBeforeCallQuery)
|
|
63
63
|
.then((response) => {
|
|
64
64
|
responseAllPages.push(response);
|
|
65
65
|
const newPageParam = getNextPageParam(response, responseAllPages.length);
|
|
@@ -90,7 +90,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
90
90
|
pageParams: newPageParams,
|
|
91
91
|
hasNextPage: newPageParam !== undefined,
|
|
92
92
|
});
|
|
93
|
-
onSuccess(response,
|
|
93
|
+
onSuccess(response, stateBeforeCallQuery);
|
|
94
94
|
})
|
|
95
95
|
.catch((error) => {
|
|
96
96
|
const prevState = get();
|
|
@@ -120,10 +120,10 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
120
120
|
set({ retryCount: prevState.retryCount + 1, isGoingToRetry: true });
|
|
121
121
|
callQuery();
|
|
122
122
|
}
|
|
123
|
-
onError(error,
|
|
123
|
+
onError(error, stateBeforeCallQuery);
|
|
124
124
|
})
|
|
125
125
|
.finally(() => {
|
|
126
|
-
onSettled(
|
|
126
|
+
onSettled(stateBeforeCallQuery);
|
|
127
127
|
});
|
|
128
128
|
};
|
|
129
129
|
callQuery();
|
|
@@ -10,12 +10,19 @@ export declare type MutationState<TVar, TResponse = any, TError = unknown> = {
|
|
|
10
10
|
responseUpdatedAt: number | null;
|
|
11
11
|
error: TError | null;
|
|
12
12
|
errorUpdatedAt: number | null;
|
|
13
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Mutate function is a promise that will always resolve.
|
|
15
|
+
*/
|
|
16
|
+
mutate: (variables?: TVar) => Promise<{
|
|
17
|
+
response?: TResponse;
|
|
18
|
+
error?: TError;
|
|
19
|
+
variables?: TVar;
|
|
20
|
+
}>;
|
|
14
21
|
};
|
|
15
22
|
export declare type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = InitStoreOptions<MutationState<TVar, TResponse, TError>> & {
|
|
16
|
-
onMutate?: (variables: TVar | undefined,
|
|
17
|
-
onSuccess?: (response: TResponse, variables: TVar | undefined,
|
|
18
|
-
onError?: (error: TError, variables: TVar | undefined,
|
|
19
|
-
onSettled?: (variables: TVar | undefined,
|
|
23
|
+
onMutate?: (variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
24
|
+
onSuccess?: (response: TResponse, variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
25
|
+
onError?: (error: TError, variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
26
|
+
onSettled?: (variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
|
|
20
27
|
};
|
|
21
28
|
export declare const createMutation: <TVar, TResponse = any, TError = unknown>(mutationFn: (variables: TVar | undefined, state: MutationState<TVar, TResponse, TError>) => Promise<TResponse>, options?: CreateMutationOptions<TVar, TResponse, TError>) => import("./create-store").UseStore<MutationState<TVar, TResponse, TError>>;
|
|
@@ -15,34 +15,36 @@ const createMutation = (mutationFn, options = {}) => {
|
|
|
15
15
|
errorUpdatedAt: null,
|
|
16
16
|
mutate: (variables) => {
|
|
17
17
|
set({ isWaiting: true });
|
|
18
|
-
const
|
|
19
|
-
onMutate(variables,
|
|
20
|
-
return new Promise((resolve
|
|
21
|
-
mutationFn(variables,
|
|
18
|
+
const stateBeforeMutate = get();
|
|
19
|
+
onMutate(variables, stateBeforeMutate);
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
mutationFn(variables, stateBeforeMutate)
|
|
22
22
|
.then((response) => {
|
|
23
23
|
set({
|
|
24
24
|
isWaiting: false,
|
|
25
25
|
isSuccess: true,
|
|
26
|
+
isError: false,
|
|
26
27
|
response,
|
|
27
28
|
responseUpdatedAt: Date.now(),
|
|
28
29
|
error: null,
|
|
29
30
|
errorUpdatedAt: null,
|
|
30
31
|
});
|
|
31
|
-
onSuccess(response, variables,
|
|
32
|
-
resolve(response);
|
|
32
|
+
onSuccess(response, variables, stateBeforeMutate);
|
|
33
|
+
resolve({ response, variables });
|
|
33
34
|
})
|
|
34
35
|
.catch((error) => {
|
|
35
36
|
set({
|
|
36
|
-
isWaiting:
|
|
37
|
+
isWaiting: false,
|
|
38
|
+
isSuccess: false,
|
|
37
39
|
isError: true,
|
|
38
40
|
error,
|
|
39
41
|
errorUpdatedAt: Date.now(),
|
|
40
42
|
});
|
|
41
|
-
onError(error, variables,
|
|
42
|
-
|
|
43
|
+
onError(error, variables, stateBeforeMutate);
|
|
44
|
+
resolve({ error, variables });
|
|
43
45
|
})
|
|
44
46
|
.finally(() => {
|
|
45
|
-
onSettled(variables,
|
|
47
|
+
onSettled(variables, stateBeforeMutate);
|
|
46
48
|
});
|
|
47
49
|
});
|
|
48
50
|
},
|
|
@@ -147,8 +147,8 @@ export declare type CreateQueryOptions<TKey extends StoreKey = StoreKey, TRespon
|
|
|
147
147
|
* This function should return a variable that will be used when fetching next page (`pageParam`).
|
|
148
148
|
*/
|
|
149
149
|
getNextPageParam?: (lastPage: TResponse, index: number) => any;
|
|
150
|
-
onSuccess?: (response: TResponse,
|
|
151
|
-
onError?: (error: TError,
|
|
152
|
-
onSettled?: (
|
|
150
|
+
onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
151
|
+
onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
152
|
+
onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
|
|
153
153
|
};
|
|
154
154
|
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>) => import("./create-stores").UseStores<TKey, QueryState<TKey, TResponse, TData, TError>>;
|
|
@@ -61,8 +61,8 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
61
61
|
else
|
|
62
62
|
set({ isGoingToRetry: false, isWaiting: true, isRefetching: true });
|
|
63
63
|
}
|
|
64
|
-
const
|
|
65
|
-
queryFn(key,
|
|
64
|
+
const stateBeforeCallQuery = { ...get(), pageParam };
|
|
65
|
+
queryFn(key, stateBeforeCallQuery)
|
|
66
66
|
.then((response) => {
|
|
67
67
|
responseAllPages.push(response);
|
|
68
68
|
const newPageParam = getNextPageParam(response, responseAllPages.length);
|
|
@@ -93,7 +93,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
93
93
|
pageParams: newPageParams,
|
|
94
94
|
hasNextPage: newPageParam !== undefined,
|
|
95
95
|
});
|
|
96
|
-
onSuccess(response,
|
|
96
|
+
onSuccess(response, stateBeforeCallQuery);
|
|
97
97
|
})
|
|
98
98
|
.catch((error) => {
|
|
99
99
|
const prevState = get();
|
|
@@ -123,10 +123,10 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
123
123
|
set({ retryCount: prevState.retryCount + 1, isGoingToRetry: true });
|
|
124
124
|
callQuery();
|
|
125
125
|
}
|
|
126
|
-
onError(error,
|
|
126
|
+
onError(error, stateBeforeCallQuery);
|
|
127
127
|
})
|
|
128
128
|
.finally(() => {
|
|
129
|
-
onSettled(
|
|
129
|
+
onSettled(stateBeforeCallQuery);
|
|
130
130
|
});
|
|
131
131
|
};
|
|
132
132
|
callQuery();
|