floppy-disk 2.4.0 → 2.5.0-beta.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.
@@ -26,8 +26,10 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
26
26
  * If the data is empty, it will just fetch the first page.
27
27
  *
28
28
  * You can ignore this if your query is not paginated.
29
+ *
30
+ * @returns Promise that will always get resolved.
29
31
  */
30
- fetchNextPage: () => void;
32
+ fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
31
33
  /**
32
34
  * Set query state (data, error, etc) to initial state.
33
35
  */
@@ -87,7 +87,7 @@ export const createQuery = (queryFn, options = {}) => {
87
87
  .then((response) => {
88
88
  if (preventReplaceResponse.get(keyHash)) {
89
89
  set({ isWaiting: false });
90
- return;
90
+ return resolve(get());
91
91
  }
92
92
  responseAllPages.push(response);
93
93
  const newPageParam = getNextPageParam(response, responseAllPages.length);
@@ -184,18 +184,27 @@ export const createQuery = (queryFn, options = {}) => {
184
184
  return;
185
185
  forceFetch();
186
186
  };
187
- const fetchNextPage = () => {
187
+ const fetchNextPage = () => new Promise((resolve) => {
188
+ const state = get();
188
189
  if (typeof options.getNextPageParam !== 'function') {
189
- return console.warn('fetchNextPage with invalid getNextPageParam option');
190
+ console.warn('fetchNextPage with invalid getNextPageParam option');
191
+ return resolve(state);
190
192
  }
191
- const state = get();
192
193
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
193
194
  if (isLoading)
194
- return forceFetch();
195
+ return resolve(forceFetch());
195
196
  if (isWaitingNextPage || !hasNextPage)
196
- return;
197
+ return resolve(state);
198
+ let shouldcancel = false;
199
+ const cancel = () => {
200
+ shouldcancel = true;
201
+ };
202
+ onBeforeFetch(cancel, state);
203
+ if (shouldcancel)
204
+ return resolve(state);
197
205
  set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
198
206
  clearTimeout(retryNextPageTimeoutId.get(keyHash));
207
+ const stateBeforeCallQuery = get();
199
208
  queryFn(key, { ...state, pageParam })
200
209
  .then((response) => {
201
210
  const newPageParam = getNextPageParam(response, pageParams.length);
@@ -208,6 +217,8 @@ export const createQuery = (queryFn, options = {}) => {
208
217
  pageParams: pageParams.concat(newPageParam),
209
218
  hasNextPage: hasValue(newPageParam),
210
219
  });
220
+ onSuccess(response, stateBeforeCallQuery);
221
+ resolve(get());
211
222
  })
212
223
  .catch((error) => {
213
224
  const prevState = get();
@@ -225,8 +236,13 @@ export const createQuery = (queryFn, options = {}) => {
225
236
  fetchNextPage();
226
237
  }, delay));
227
238
  }
239
+ onError(error, stateBeforeCallQuery);
240
+ resolve(get());
241
+ })
242
+ .finally(() => {
243
+ onSettled(stateBeforeCallQuery);
228
244
  });
229
- };
245
+ });
230
246
  return {
231
247
  ...INITIAL_QUERY_STATE,
232
248
  key,
@@ -25,8 +25,10 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
25
25
  * If the data is empty, it will just fetch the first page.
26
26
  *
27
27
  * You can ignore this if your query is not paginated.
28
+ *
29
+ * @returns Promise that will always get resolved.
28
30
  */
29
- fetchNextPage: () => void;
31
+ fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
30
32
  /**
31
33
  * Set query state (data, error, etc) to initial state.
32
34
  */
@@ -86,7 +86,7 @@ export const createQuery = (queryFn, options = {}) => {
86
86
  .then((response) => {
87
87
  if (preventReplaceResponse.get(keyHash)) {
88
88
  set({ isWaiting: false });
89
- return;
89
+ return resolve(get());
90
90
  }
91
91
  responseAllPages.push(response);
92
92
  const newPageParam = getNextPageParam(response, responseAllPages.length);
@@ -183,18 +183,27 @@ export const createQuery = (queryFn, options = {}) => {
183
183
  return;
184
184
  forceFetch();
185
185
  };
186
- const fetchNextPage = () => {
186
+ const fetchNextPage = () => new Promise((resolve) => {
187
+ const state = get();
187
188
  if (typeof options.getNextPageParam !== 'function') {
188
- return console.warn('fetchNextPage with invalid getNextPageParam option');
189
+ console.warn('fetchNextPage with invalid getNextPageParam option');
190
+ return resolve(state);
189
191
  }
190
- const state = get();
191
192
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
192
193
  if (isLoading)
193
- return forceFetch();
194
+ return resolve(forceFetch());
194
195
  if (isWaitingNextPage || !hasNextPage)
195
- return;
196
+ return resolve(state);
197
+ let shouldcancel = false;
198
+ const cancel = () => {
199
+ shouldcancel = true;
200
+ };
201
+ onBeforeFetch(cancel, state);
202
+ if (shouldcancel)
203
+ return resolve(state);
196
204
  set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
197
205
  clearTimeout(retryNextPageTimeoutId.get(keyHash));
206
+ const stateBeforeCallQuery = get();
198
207
  queryFn(key, { ...state, pageParam })
199
208
  .then((response) => {
200
209
  const newPageParam = getNextPageParam(response, pageParams.length);
@@ -207,6 +216,8 @@ export const createQuery = (queryFn, options = {}) => {
207
216
  pageParams: pageParams.concat(newPageParam),
208
217
  hasNextPage: hasValue(newPageParam),
209
218
  });
219
+ onSuccess(response, stateBeforeCallQuery);
220
+ resolve(get());
210
221
  })
211
222
  .catch((error) => {
212
223
  const prevState = get();
@@ -224,8 +235,13 @@ export const createQuery = (queryFn, options = {}) => {
224
235
  fetchNextPage();
225
236
  }, delay));
226
237
  }
238
+ onError(error, stateBeforeCallQuery);
239
+ resolve(get());
240
+ })
241
+ .finally(() => {
242
+ onSettled(stateBeforeCallQuery);
227
243
  });
228
- };
244
+ });
229
245
  return {
230
246
  ...INITIAL_QUERY_STATE,
231
247
  key,
@@ -26,8 +26,10 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
26
26
  * If the data is empty, it will just fetch the first page.
27
27
  *
28
28
  * You can ignore this if your query is not paginated.
29
+ *
30
+ * @returns Promise that will always get resolved.
29
31
  */
30
- fetchNextPage: () => void;
32
+ fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
31
33
  /**
32
34
  * Set query state (data, error, etc) to initial state.
33
35
  */
@@ -90,7 +90,7 @@ const createQuery = (queryFn, options = {}) => {
90
90
  .then((response) => {
91
91
  if (preventReplaceResponse.get(keyHash)) {
92
92
  set({ isWaiting: false });
93
- return;
93
+ return resolve(get());
94
94
  }
95
95
  responseAllPages.push(response);
96
96
  const newPageParam = getNextPageParam(response, responseAllPages.length);
@@ -187,18 +187,27 @@ const createQuery = (queryFn, options = {}) => {
187
187
  return;
188
188
  forceFetch();
189
189
  };
190
- const fetchNextPage = () => {
190
+ const fetchNextPage = () => new Promise((resolve) => {
191
+ const state = get();
191
192
  if (typeof options.getNextPageParam !== 'function') {
192
- return console.warn('fetchNextPage with invalid getNextPageParam option');
193
+ console.warn('fetchNextPage with invalid getNextPageParam option');
194
+ return resolve(state);
193
195
  }
194
- const state = get();
195
196
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
196
197
  if (isLoading)
197
- return forceFetch();
198
+ return resolve(forceFetch());
198
199
  if (isWaitingNextPage || !hasNextPage)
199
- return;
200
+ return resolve(state);
201
+ let shouldcancel = false;
202
+ const cancel = () => {
203
+ shouldcancel = true;
204
+ };
205
+ onBeforeFetch(cancel, state);
206
+ if (shouldcancel)
207
+ return resolve(state);
200
208
  set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
201
209
  clearTimeout(retryNextPageTimeoutId.get(keyHash));
210
+ const stateBeforeCallQuery = get();
202
211
  queryFn(key, { ...state, pageParam })
203
212
  .then((response) => {
204
213
  const newPageParam = getNextPageParam(response, pageParams.length);
@@ -211,6 +220,8 @@ const createQuery = (queryFn, options = {}) => {
211
220
  pageParams: pageParams.concat(newPageParam),
212
221
  hasNextPage: (0, utils_1.hasValue)(newPageParam),
213
222
  });
223
+ onSuccess(response, stateBeforeCallQuery);
224
+ resolve(get());
214
225
  })
215
226
  .catch((error) => {
216
227
  const prevState = get();
@@ -228,8 +239,13 @@ const createQuery = (queryFn, options = {}) => {
228
239
  fetchNextPage();
229
240
  }, delay));
230
241
  }
242
+ onError(error, stateBeforeCallQuery);
243
+ resolve(get());
244
+ })
245
+ .finally(() => {
246
+ onSettled(stateBeforeCallQuery);
231
247
  });
232
- };
248
+ });
233
249
  return {
234
250
  ...INITIAL_QUERY_STATE,
235
251
  key,
@@ -25,8 +25,10 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
25
25
  * If the data is empty, it will just fetch the first page.
26
26
  *
27
27
  * You can ignore this if your query is not paginated.
28
+ *
29
+ * @returns Promise that will always get resolved.
28
30
  */
29
- fetchNextPage: () => void;
31
+ fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
30
32
  /**
31
33
  * Set query state (data, error, etc) to initial state.
32
34
  */
@@ -89,7 +89,7 @@ const createQuery = (queryFn, options = {}) => {
89
89
  .then((response) => {
90
90
  if (preventReplaceResponse.get(keyHash)) {
91
91
  set({ isWaiting: false });
92
- return;
92
+ return resolve(get());
93
93
  }
94
94
  responseAllPages.push(response);
95
95
  const newPageParam = getNextPageParam(response, responseAllPages.length);
@@ -186,18 +186,27 @@ const createQuery = (queryFn, options = {}) => {
186
186
  return;
187
187
  forceFetch();
188
188
  };
189
- const fetchNextPage = () => {
189
+ const fetchNextPage = () => new Promise((resolve) => {
190
+ const state = get();
190
191
  if (typeof options.getNextPageParam !== 'function') {
191
- return console.warn('fetchNextPage with invalid getNextPageParam option');
192
+ console.warn('fetchNextPage with invalid getNextPageParam option');
193
+ return resolve(state);
192
194
  }
193
- const state = get();
194
195
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
195
196
  if (isLoading)
196
- return forceFetch();
197
+ return resolve(forceFetch());
197
198
  if (isWaitingNextPage || !hasNextPage)
198
- return;
199
+ return resolve(state);
200
+ let shouldcancel = false;
201
+ const cancel = () => {
202
+ shouldcancel = true;
203
+ };
204
+ onBeforeFetch(cancel, state);
205
+ if (shouldcancel)
206
+ return resolve(state);
199
207
  set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
200
208
  clearTimeout(retryNextPageTimeoutId.get(keyHash));
209
+ const stateBeforeCallQuery = get();
201
210
  queryFn(key, { ...state, pageParam })
202
211
  .then((response) => {
203
212
  const newPageParam = getNextPageParam(response, pageParams.length);
@@ -210,6 +219,8 @@ const createQuery = (queryFn, options = {}) => {
210
219
  pageParams: pageParams.concat(newPageParam),
211
220
  hasNextPage: (0, utils_1.hasValue)(newPageParam),
212
221
  });
222
+ onSuccess(response, stateBeforeCallQuery);
223
+ resolve(get());
213
224
  })
214
225
  .catch((error) => {
215
226
  const prevState = get();
@@ -227,8 +238,13 @@ const createQuery = (queryFn, options = {}) => {
227
238
  fetchNextPage();
228
239
  }, delay));
229
240
  }
241
+ onError(error, stateBeforeCallQuery);
242
+ resolve(get());
243
+ })
244
+ .finally(() => {
245
+ onSettled(stateBeforeCallQuery);
230
246
  });
231
- };
247
+ });
232
248
  return {
233
249
  ...INITIAL_QUERY_STATE,
234
250
  key,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "2.4.0",
3
+ "version": "2.5.0-beta.1",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",