floppy-disk 1.1.0 → 1.1.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.
@@ -13,9 +13,9 @@ export declare type MutationState<TVar, TResponse = any, TError = unknown> = {
13
13
  mutate: (variables?: TVar) => Promise<TResponse>;
14
14
  };
15
15
  export declare type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = InitStoreOptions<MutationState<TVar, TResponse, TError>> & {
16
- onMutate?: (variables: TVar | undefined, inputState: MutationState<TVar, TResponse, TError>) => void;
17
- onSuccess?: (response: TResponse, variables: TVar | undefined, inputState: MutationState<TVar, TResponse, TError>) => void;
18
- onError?: (error: TError, variables: TVar | undefined, inputState: MutationState<TVar, TResponse, TError>) => void;
19
- onSettled?: (variables: TVar | undefined, inputState: MutationState<TVar, TResponse, TError>) => void;
16
+ onMutate?: (variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
17
+ onSuccess?: (response: TResponse, variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
18
+ onError?: (error: TError, variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
19
+ onSettled?: (variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
20
20
  };
21
21
  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 state = get();
16
- onMutate(variables, state);
15
+ const stateBeforeMutate = get();
16
+ onMutate(variables, stateBeforeMutate);
17
17
  return new Promise((resolve, reject) => {
18
- mutationFn(variables, state)
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, state);
29
+ onSuccess(response, variables, stateBeforeMutate);
29
30
  resolve(response);
30
31
  })
31
32
  .catch((error) => {
32
33
  set({
33
- isWaiting: true,
34
+ isWaiting: false,
35
+ isSuccess: false,
34
36
  isError: true,
35
37
  error,
36
38
  errorUpdatedAt: Date.now(),
37
39
  });
38
- onError(error, variables, state);
40
+ onError(error, variables, stateBeforeMutate);
39
41
  reject(error);
40
42
  })
41
43
  .finally(() => {
42
- onSettled(variables, state);
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, inputState: QueryState<TKey, TResponse, TData, TError>) => void;
151
- onError?: (error: TError, inputState: QueryState<TKey, TResponse, TData, TError>) => void;
152
- onSettled?: (inputState: QueryState<TKey, TResponse, TData, TError>) => void;
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 inputState = { ...get(), pageParam };
62
- queryFn(key, inputState)
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, inputState);
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, inputState);
123
+ onError(error, stateBeforeCallQuery);
124
124
  })
125
125
  .finally(() => {
126
- onSettled(inputState);
126
+ onSettled(stateBeforeCallQuery);
127
127
  });
128
128
  };
129
129
  callQuery();
@@ -13,9 +13,9 @@ export declare type MutationState<TVar, TResponse = any, TError = unknown> = {
13
13
  mutate: (variables?: TVar) => Promise<TResponse>;
14
14
  };
15
15
  export declare type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = InitStoreOptions<MutationState<TVar, TResponse, TError>> & {
16
- onMutate?: (variables: TVar | undefined, inputState: MutationState<TVar, TResponse, TError>) => void;
17
- onSuccess?: (response: TResponse, variables: TVar | undefined, inputState: MutationState<TVar, TResponse, TError>) => void;
18
- onError?: (error: TError, variables: TVar | undefined, inputState: MutationState<TVar, TResponse, TError>) => void;
19
- onSettled?: (variables: TVar | undefined, inputState: MutationState<TVar, TResponse, TError>) => void;
16
+ onMutate?: (variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
17
+ onSuccess?: (response: TResponse, variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
18
+ onError?: (error: TError, variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
19
+ onSettled?: (variables: TVar | undefined, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
20
20
  };
21
21
  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 state = get();
19
- onMutate(variables, state);
18
+ const stateBeforeMutate = get();
19
+ onMutate(variables, stateBeforeMutate);
20
20
  return new Promise((resolve, reject) => {
21
- mutationFn(variables, state)
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, state);
32
+ onSuccess(response, variables, stateBeforeMutate);
32
33
  resolve(response);
33
34
  })
34
35
  .catch((error) => {
35
36
  set({
36
- isWaiting: true,
37
+ isWaiting: false,
38
+ isSuccess: false,
37
39
  isError: true,
38
40
  error,
39
41
  errorUpdatedAt: Date.now(),
40
42
  });
41
- onError(error, variables, state);
43
+ onError(error, variables, stateBeforeMutate);
42
44
  reject(error);
43
45
  })
44
46
  .finally(() => {
45
- onSettled(variables, state);
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, inputState: QueryState<TKey, TResponse, TData, TError>) => void;
151
- onError?: (error: TError, inputState: QueryState<TKey, TResponse, TData, TError>) => void;
152
- onSettled?: (inputState: QueryState<TKey, TResponse, TData, TError>) => void;
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 inputState = { ...get(), pageParam };
65
- queryFn(key, inputState)
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, inputState);
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, inputState);
126
+ onError(error, stateBeforeCallQuery);
127
127
  })
128
128
  .finally(() => {
129
- onSettled(inputState);
129
+ onSettled(stateBeforeCallQuery);
130
130
  });
131
131
  };
132
132
  callQuery();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",