floppy-disk 2.0.0-beta.1 → 2.0.0-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.
@@ -25,6 +25,17 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
25
25
  * Set query state (data, error, etc) to initial state.
26
26
  */
27
27
  reset: () => void;
28
+ /**
29
+ * Optimistic update.
30
+ *
31
+ * @returns function to revert the changes & function to invalidate the query
32
+ *
33
+ * IMPORTANT NOTE: This won't work well on infinite query.
34
+ */
35
+ optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse)) => {
36
+ revert: () => void;
37
+ invalidate: () => void;
38
+ };
28
39
  /**
29
40
  * Network fetching status.
30
41
  */
@@ -76,8 +76,10 @@ export const createQuery = (queryFn, options = {}) => {
76
76
  preventReplaceResponse.set(hashKeyFn(key), false);
77
77
  queryFn(key, stateBeforeCallQuery)
78
78
  .then((response) => {
79
- if (preventReplaceResponse.get(hashKeyFn(key)))
79
+ if (preventReplaceResponse.get(hashKeyFn(key))) {
80
+ set({ isWaiting: false });
80
81
  return;
82
+ }
81
83
  responseAllPages.push(response);
82
84
  const newPageParam = getNextPageParam(response, responseAllPages.length);
83
85
  newPageParams.push(newPageParam);
@@ -207,6 +209,7 @@ export const createQuery = (queryFn, options = {}) => {
207
209
  forceFetch,
208
210
  fetchNextPage,
209
211
  reset: () => set(INITIAL_QUERY_STATE),
212
+ optimisticUpdate: (response) => useQuery.optimisticUpdate({ key, response }),
210
213
  };
211
214
  }, (() => {
212
215
  const fetchWindowFocusHandler = () => {
@@ -25,6 +25,17 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
25
25
  * Set query state (data, error, etc) to initial state.
26
26
  */
27
27
  reset: () => void;
28
+ /**
29
+ * Optimistic update.
30
+ *
31
+ * @returns function to revert the changes & function to invalidate the query
32
+ *
33
+ * IMPORTANT NOTE: This won't work well on infinite query.
34
+ */
35
+ optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse)) => {
36
+ revert: () => void;
37
+ invalidate: () => void;
38
+ };
28
39
  /**
29
40
  * Network fetching status.
30
41
  */
@@ -79,8 +79,10 @@ const createQuery = (queryFn, options = {}) => {
79
79
  preventReplaceResponse.set(hashKeyFn(key), false);
80
80
  queryFn(key, stateBeforeCallQuery)
81
81
  .then((response) => {
82
- if (preventReplaceResponse.get(hashKeyFn(key)))
82
+ if (preventReplaceResponse.get(hashKeyFn(key))) {
83
+ set({ isWaiting: false });
83
84
  return;
85
+ }
84
86
  responseAllPages.push(response);
85
87
  const newPageParam = getNextPageParam(response, responseAllPages.length);
86
88
  newPageParams.push(newPageParam);
@@ -210,6 +212,7 @@ const createQuery = (queryFn, options = {}) => {
210
212
  forceFetch,
211
213
  fetchNextPage,
212
214
  reset: () => set(INITIAL_QUERY_STATE),
215
+ optimisticUpdate: (response) => useQuery.optimisticUpdate({ key, response }),
213
216
  };
214
217
  }, (() => {
215
218
  const fetchWindowFocusHandler = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.2",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",