floppy-disk 2.4.0-beta.3 → 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.
@@ -33,4 +33,7 @@ export type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = Ini
33
33
  onError?: (error: TError, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
34
34
  onSettled?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
35
35
  };
36
+ /**
37
+ * @see https://floppy-disk.vercel.app/docs/api#createmutation
38
+ */
36
39
  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>;
@@ -1,5 +1,8 @@
1
1
  import { noop } from '../utils';
2
2
  import { createStore } from './create-store';
3
+ /**
4
+ * @see https://floppy-disk.vercel.app/docs/api#createmutation
5
+ */
3
6
  export const createMutation = (mutationFn, options = {}) => {
4
7
  const { onMutate = noop, onSuccess = noop, onError = noop, onSettled = noop, ...createStoreOptions } = options;
5
8
  const useMutation = createStore(({ set, get }) => ({
@@ -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
  */
@@ -198,7 +200,9 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
198
200
  *
199
201
  * Disabled by default.
200
202
  *
201
- * If the query is on error state, the polling interval will be disabled, and it will use `retry` instead.
203
+ * If last data fetching is failed, the polling interval will be disabled, and it will use `retry` mechanism instead.
204
+ *
205
+ * @see https://floppy-disk.vercel.app/docs/query/polling
202
206
  */
203
207
  refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError>) => number | false);
204
208
  };
@@ -258,4 +262,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
258
262
  error?: FunctionComponent<TKey>;
259
263
  }) => JSX.Element;
260
264
  };
265
+ /**
266
+ * @see https://floppy-disk.vercel.app/docs/api#createquery
267
+ */
261
268
  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>) => UseQuery<TKey, TResponse, TData, TError>;
@@ -32,6 +32,9 @@ const useQueryDefaultDeps = (state) => [
32
32
  state.isWaitingNextPage,
33
33
  state.hasNextPage,
34
34
  ];
35
+ /**
36
+ * @see https://floppy-disk.vercel.app/docs/api#createquery
37
+ */
35
38
  export const createQuery = (queryFn, options = {}) => {
36
39
  const defaultFetchOnWindowFocus = options.fetchOnMount ?? true;
37
40
  const { onFirstSubscribe = noop, onSubscribe = noop, onLastUnsubscribe = noop, onBeforeChangeKey = noop, defaultDeps = useQueryDefaultDeps, select = identityFn, staleTime = 3000, // 3 seconds
@@ -84,7 +87,7 @@ export const createQuery = (queryFn, options = {}) => {
84
87
  .then((response) => {
85
88
  if (preventReplaceResponse.get(keyHash)) {
86
89
  set({ isWaiting: false });
87
- return;
90
+ return resolve(get());
88
91
  }
89
92
  responseAllPages.push(response);
90
93
  const newPageParam = getNextPageParam(response, responseAllPages.length);
@@ -181,18 +184,27 @@ export const createQuery = (queryFn, options = {}) => {
181
184
  return;
182
185
  forceFetch();
183
186
  };
184
- const fetchNextPage = () => {
187
+ const fetchNextPage = () => new Promise((resolve) => {
188
+ const state = get();
185
189
  if (typeof options.getNextPageParam !== 'function') {
186
- return console.warn('fetchNextPage with invalid getNextPageParam option');
190
+ console.warn('fetchNextPage with invalid getNextPageParam option');
191
+ return resolve(state);
187
192
  }
188
- const state = get();
189
193
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
190
194
  if (isLoading)
191
- return forceFetch();
195
+ return resolve(forceFetch());
192
196
  if (isWaitingNextPage || !hasNextPage)
193
- 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);
194
205
  set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
195
206
  clearTimeout(retryNextPageTimeoutId.get(keyHash));
207
+ const stateBeforeCallQuery = get();
196
208
  queryFn(key, { ...state, pageParam })
197
209
  .then((response) => {
198
210
  const newPageParam = getNextPageParam(response, pageParams.length);
@@ -205,6 +217,8 @@ export const createQuery = (queryFn, options = {}) => {
205
217
  pageParams: pageParams.concat(newPageParam),
206
218
  hasNextPage: hasValue(newPageParam),
207
219
  });
220
+ onSuccess(response, stateBeforeCallQuery);
221
+ resolve(get());
208
222
  })
209
223
  .catch((error) => {
210
224
  const prevState = get();
@@ -222,8 +236,13 @@ export const createQuery = (queryFn, options = {}) => {
222
236
  fetchNextPage();
223
237
  }, delay));
224
238
  }
239
+ onError(error, stateBeforeCallQuery);
240
+ resolve(get());
241
+ })
242
+ .finally(() => {
243
+ onSettled(stateBeforeCallQuery);
225
244
  });
226
- };
245
+ });
227
246
  return {
228
247
  ...INITIAL_QUERY_STATE,
229
248
  key,
@@ -23,6 +23,9 @@ export type UseStore<T extends StoreData> = {
23
23
  setDefaultValues: (values: SetStoreData<T>) => void;
24
24
  Watch: (props: WatchProps<T>) => any;
25
25
  };
26
+ /**
27
+ * @see https://floppy-disk.vercel.app/docs/api#createstore
28
+ */
26
29
  export declare const createStore: <T extends StoreData>(initializer: StoreInitializer<T>, options?: InitStoreOptions<T> & {
27
30
  defaultDeps?: SelectDeps<T>;
28
31
  }) => UseStore<T>;
@@ -1,5 +1,8 @@
1
1
  import { useEffect, useState } from 'preact/hooks';
2
2
  import { initStore, } from '../vanilla';
3
+ /**
4
+ * @see https://floppy-disk.vercel.app/docs/api#createstore
5
+ */
3
6
  export const createStore = (initializer, options = {}) => {
4
7
  const { get, set, subscribe, getSubscribers } = initStore(initializer, options);
5
8
  const { defaultDeps } = options;
@@ -46,5 +46,8 @@ export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends Stor
46
46
  defaultDeps?: SelectDeps<T>;
47
47
  hashKeyFn?: (obj: TKey) => string;
48
48
  };
49
+ /**
50
+ * @see https://floppy-disk.vercel.app/docs/api#createstores
51
+ */
49
52
  export declare const createStores: <TKey extends StoreKey = StoreKey, T extends StoreData = StoreData>(initializer: StoresInitializer<TKey, T>, options?: CreateStoresOptions<TKey, T>) => UseStores<TKey, T>;
50
53
  export {};
@@ -1,6 +1,9 @@
1
1
  import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
2
2
  import { hashStoreKey, noop } from '../utils';
3
3
  import { initStore, } from '../vanilla';
4
+ /**
5
+ * @see https://floppy-disk.vercel.app/docs/api#createstores
6
+ */
4
7
  export const createStores = (initializer, options = {}) => {
5
8
  const { onBeforeChangeKey = noop, onStoreInitialized = noop, defaultDeps, hashKeyFn = hashStoreKey, } = options;
6
9
  const stores = new Map();
@@ -33,4 +33,7 @@ export type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = Ini
33
33
  onError?: (error: TError, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
34
34
  onSettled?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
35
35
  };
36
+ /**
37
+ * @see https://floppy-disk.vercel.app/docs/api#createmutation
38
+ */
36
39
  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>;
@@ -1,5 +1,8 @@
1
1
  import { noop } from '../utils';
2
2
  import { createStore } from './create-store';
3
+ /**
4
+ * @see https://floppy-disk.vercel.app/docs/api#createmutation
5
+ */
3
6
  export const createMutation = (mutationFn, options = {}) => {
4
7
  const { onMutate = noop, onSuccess = noop, onError = noop, onSettled = noop, ...createStoreOptions } = options;
5
8
  const useMutation = createStore(({ set, get }) => ({
@@ -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
  */
@@ -197,7 +199,9 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
197
199
  *
198
200
  * Disabled by default.
199
201
  *
200
- * If the query is on error state, the polling interval will be disabled, and it will use `retry` instead.
202
+ * If last data fetching is failed, the polling interval will be disabled, and it will use `retry` mechanism instead.
203
+ *
204
+ * @see https://floppy-disk.vercel.app/docs/query/polling
201
205
  */
202
206
  refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError>) => number | false);
203
207
  };
@@ -257,4 +261,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
257
261
  error?: FunctionComponent<TKey>;
258
262
  }) => JSX.Element;
259
263
  };
264
+ /**
265
+ * @see https://floppy-disk.vercel.app/docs/api#createquery
266
+ */
260
267
  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>) => UseQuery<TKey, TResponse, TData, TError>;
@@ -31,6 +31,9 @@ const useQueryDefaultDeps = (state) => [
31
31
  state.isWaitingNextPage,
32
32
  state.hasNextPage,
33
33
  ];
34
+ /**
35
+ * @see https://floppy-disk.vercel.app/docs/api#createquery
36
+ */
34
37
  export const createQuery = (queryFn, options = {}) => {
35
38
  const defaultFetchOnWindowFocus = options.fetchOnMount ?? true;
36
39
  const { onFirstSubscribe = noop, onSubscribe = noop, onLastUnsubscribe = noop, onBeforeChangeKey = noop, defaultDeps = useQueryDefaultDeps, select = identityFn, staleTime = 3000, // 3 seconds
@@ -83,7 +86,7 @@ export const createQuery = (queryFn, options = {}) => {
83
86
  .then((response) => {
84
87
  if (preventReplaceResponse.get(keyHash)) {
85
88
  set({ isWaiting: false });
86
- return;
89
+ return resolve(get());
87
90
  }
88
91
  responseAllPages.push(response);
89
92
  const newPageParam = getNextPageParam(response, responseAllPages.length);
@@ -180,18 +183,27 @@ export const createQuery = (queryFn, options = {}) => {
180
183
  return;
181
184
  forceFetch();
182
185
  };
183
- const fetchNextPage = () => {
186
+ const fetchNextPage = () => new Promise((resolve) => {
187
+ const state = get();
184
188
  if (typeof options.getNextPageParam !== 'function') {
185
- return console.warn('fetchNextPage with invalid getNextPageParam option');
189
+ console.warn('fetchNextPage with invalid getNextPageParam option');
190
+ return resolve(state);
186
191
  }
187
- const state = get();
188
192
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
189
193
  if (isLoading)
190
- return forceFetch();
194
+ return resolve(forceFetch());
191
195
  if (isWaitingNextPage || !hasNextPage)
192
- 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);
193
204
  set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
194
205
  clearTimeout(retryNextPageTimeoutId.get(keyHash));
206
+ const stateBeforeCallQuery = get();
195
207
  queryFn(key, { ...state, pageParam })
196
208
  .then((response) => {
197
209
  const newPageParam = getNextPageParam(response, pageParams.length);
@@ -204,6 +216,8 @@ export const createQuery = (queryFn, options = {}) => {
204
216
  pageParams: pageParams.concat(newPageParam),
205
217
  hasNextPage: hasValue(newPageParam),
206
218
  });
219
+ onSuccess(response, stateBeforeCallQuery);
220
+ resolve(get());
207
221
  })
208
222
  .catch((error) => {
209
223
  const prevState = get();
@@ -221,8 +235,13 @@ export const createQuery = (queryFn, options = {}) => {
221
235
  fetchNextPage();
222
236
  }, delay));
223
237
  }
238
+ onError(error, stateBeforeCallQuery);
239
+ resolve(get());
240
+ })
241
+ .finally(() => {
242
+ onSettled(stateBeforeCallQuery);
224
243
  });
225
- };
244
+ });
226
245
  return {
227
246
  ...INITIAL_QUERY_STATE,
228
247
  key,
@@ -23,6 +23,9 @@ export type UseStore<T extends StoreData> = {
23
23
  setDefaultValues: (values: SetStoreData<T>) => void;
24
24
  Watch: (props: WatchProps<T>) => any;
25
25
  };
26
+ /**
27
+ * @see https://floppy-disk.vercel.app/docs/api#createstore
28
+ */
26
29
  export declare const createStore: <T extends StoreData>(initializer: StoreInitializer<T>, options?: InitStoreOptions<T> & {
27
30
  defaultDeps?: SelectDeps<T>;
28
31
  }) => UseStore<T>;
@@ -1,5 +1,8 @@
1
1
  import { useEffect, useState } from 'react';
2
2
  import { initStore, } from '../vanilla';
3
+ /**
4
+ * @see https://floppy-disk.vercel.app/docs/api#createstore
5
+ */
3
6
  export const createStore = (initializer, options = {}) => {
4
7
  const { get, set, subscribe, getSubscribers } = initStore(initializer, options);
5
8
  const { defaultDeps } = options;
@@ -46,5 +46,8 @@ export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends Stor
46
46
  defaultDeps?: SelectDeps<T>;
47
47
  hashKeyFn?: (obj: TKey) => string;
48
48
  };
49
+ /**
50
+ * @see https://floppy-disk.vercel.app/docs/api#createstores
51
+ */
49
52
  export declare const createStores: <TKey extends StoreKey = StoreKey, T extends StoreData = StoreData>(initializer: StoresInitializer<TKey, T>, options?: CreateStoresOptions<TKey, T>) => UseStores<TKey, T>;
50
53
  export {};
@@ -1,6 +1,9 @@
1
1
  import { useEffect, useMemo, useRef, useState } from 'react';
2
2
  import { hashStoreKey, noop } from '../utils';
3
3
  import { initStore, } from '../vanilla';
4
+ /**
5
+ * @see https://floppy-disk.vercel.app/docs/api#createstores
6
+ */
4
7
  export const createStores = (initializer, options = {}) => {
5
8
  const { onBeforeChangeKey = noop, onStoreInitialized = noop, defaultDeps, hashKeyFn = hashStoreKey, } = options;
6
9
  const stores = new Map();
@@ -33,4 +33,7 @@ export type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = Ini
33
33
  onError?: (error: TError, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
34
34
  onSettled?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
35
35
  };
36
+ /**
37
+ * @see https://floppy-disk.vercel.app/docs/api#createmutation
38
+ */
36
39
  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>;
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createMutation = void 0;
4
4
  const utils_1 = require("../utils");
5
5
  const create_store_1 = require("./create-store");
6
+ /**
7
+ * @see https://floppy-disk.vercel.app/docs/api#createmutation
8
+ */
6
9
  const createMutation = (mutationFn, options = {}) => {
7
10
  const { onMutate = utils_1.noop, onSuccess = utils_1.noop, onError = utils_1.noop, onSettled = utils_1.noop, ...createStoreOptions } = options;
8
11
  const useMutation = (0, create_store_1.createStore)(({ set, get }) => ({
@@ -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
  */
@@ -198,7 +200,9 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
198
200
  *
199
201
  * Disabled by default.
200
202
  *
201
- * If the query is on error state, the polling interval will be disabled, and it will use `retry` instead.
203
+ * If last data fetching is failed, the polling interval will be disabled, and it will use `retry` mechanism instead.
204
+ *
205
+ * @see https://floppy-disk.vercel.app/docs/query/polling
202
206
  */
203
207
  refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError>) => number | false);
204
208
  };
@@ -258,4 +262,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
258
262
  error?: FunctionComponent<TKey>;
259
263
  }) => JSX.Element;
260
264
  };
265
+ /**
266
+ * @see https://floppy-disk.vercel.app/docs/api#createquery
267
+ */
261
268
  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>) => UseQuery<TKey, TResponse, TData, TError>;
@@ -35,6 +35,9 @@ const useQueryDefaultDeps = (state) => [
35
35
  state.isWaitingNextPage,
36
36
  state.hasNextPage,
37
37
  ];
38
+ /**
39
+ * @see https://floppy-disk.vercel.app/docs/api#createquery
40
+ */
38
41
  const createQuery = (queryFn, options = {}) => {
39
42
  const defaultFetchOnWindowFocus = options.fetchOnMount ?? true;
40
43
  const { onFirstSubscribe = utils_1.noop, onSubscribe = utils_1.noop, onLastUnsubscribe = utils_1.noop, onBeforeChangeKey = utils_1.noop, defaultDeps = useQueryDefaultDeps, select = utils_1.identityFn, staleTime = 3000, // 3 seconds
@@ -87,7 +90,7 @@ const createQuery = (queryFn, options = {}) => {
87
90
  .then((response) => {
88
91
  if (preventReplaceResponse.get(keyHash)) {
89
92
  set({ isWaiting: false });
90
- return;
93
+ return resolve(get());
91
94
  }
92
95
  responseAllPages.push(response);
93
96
  const newPageParam = getNextPageParam(response, responseAllPages.length);
@@ -184,18 +187,27 @@ const createQuery = (queryFn, options = {}) => {
184
187
  return;
185
188
  forceFetch();
186
189
  };
187
- const fetchNextPage = () => {
190
+ const fetchNextPage = () => new Promise((resolve) => {
191
+ const state = get();
188
192
  if (typeof options.getNextPageParam !== 'function') {
189
- return console.warn('fetchNextPage with invalid getNextPageParam option');
193
+ console.warn('fetchNextPage with invalid getNextPageParam option');
194
+ return resolve(state);
190
195
  }
191
- const state = get();
192
196
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
193
197
  if (isLoading)
194
- return forceFetch();
198
+ return resolve(forceFetch());
195
199
  if (isWaitingNextPage || !hasNextPage)
196
- 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);
197
208
  set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
198
209
  clearTimeout(retryNextPageTimeoutId.get(keyHash));
210
+ const stateBeforeCallQuery = get();
199
211
  queryFn(key, { ...state, pageParam })
200
212
  .then((response) => {
201
213
  const newPageParam = getNextPageParam(response, pageParams.length);
@@ -208,6 +220,8 @@ const createQuery = (queryFn, options = {}) => {
208
220
  pageParams: pageParams.concat(newPageParam),
209
221
  hasNextPage: (0, utils_1.hasValue)(newPageParam),
210
222
  });
223
+ onSuccess(response, stateBeforeCallQuery);
224
+ resolve(get());
211
225
  })
212
226
  .catch((error) => {
213
227
  const prevState = get();
@@ -225,8 +239,13 @@ const createQuery = (queryFn, options = {}) => {
225
239
  fetchNextPage();
226
240
  }, delay));
227
241
  }
242
+ onError(error, stateBeforeCallQuery);
243
+ resolve(get());
244
+ })
245
+ .finally(() => {
246
+ onSettled(stateBeforeCallQuery);
228
247
  });
229
- };
248
+ });
230
249
  return {
231
250
  ...INITIAL_QUERY_STATE,
232
251
  key,
@@ -23,6 +23,9 @@ export type UseStore<T extends StoreData> = {
23
23
  setDefaultValues: (values: SetStoreData<T>) => void;
24
24
  Watch: (props: WatchProps<T>) => any;
25
25
  };
26
+ /**
27
+ * @see https://floppy-disk.vercel.app/docs/api#createstore
28
+ */
26
29
  export declare const createStore: <T extends StoreData>(initializer: StoreInitializer<T>, options?: InitStoreOptions<T> & {
27
30
  defaultDeps?: SelectDeps<T>;
28
31
  }) => UseStore<T>;
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createStore = void 0;
4
4
  const hooks_1 = require("preact/hooks");
5
5
  const vanilla_1 = require("../vanilla");
6
+ /**
7
+ * @see https://floppy-disk.vercel.app/docs/api#createstore
8
+ */
6
9
  const createStore = (initializer, options = {}) => {
7
10
  const { get, set, subscribe, getSubscribers } = (0, vanilla_1.initStore)(initializer, options);
8
11
  const { defaultDeps } = options;
@@ -46,5 +46,8 @@ export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends Stor
46
46
  defaultDeps?: SelectDeps<T>;
47
47
  hashKeyFn?: (obj: TKey) => string;
48
48
  };
49
+ /**
50
+ * @see https://floppy-disk.vercel.app/docs/api#createstores
51
+ */
49
52
  export declare const createStores: <TKey extends StoreKey = StoreKey, T extends StoreData = StoreData>(initializer: StoresInitializer<TKey, T>, options?: CreateStoresOptions<TKey, T>) => UseStores<TKey, T>;
50
53
  export {};
@@ -4,6 +4,9 @@ exports.createStores = void 0;
4
4
  const hooks_1 = require("preact/hooks");
5
5
  const utils_1 = require("../utils");
6
6
  const vanilla_1 = require("../vanilla");
7
+ /**
8
+ * @see https://floppy-disk.vercel.app/docs/api#createstores
9
+ */
7
10
  const createStores = (initializer, options = {}) => {
8
11
  const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey, } = options;
9
12
  const stores = new Map();
@@ -33,4 +33,7 @@ export type CreateMutationOptions<TVar, TResponse = any, TError = unknown> = Ini
33
33
  onError?: (error: TError, variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
34
34
  onSettled?: (variables: TVar, stateBeforeMutate: MutationState<TVar, TResponse, TError>) => void;
35
35
  };
36
+ /**
37
+ * @see https://floppy-disk.vercel.app/docs/api#createmutation
38
+ */
36
39
  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>;
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createMutation = void 0;
4
4
  const utils_1 = require("../utils");
5
5
  const create_store_1 = require("./create-store");
6
+ /**
7
+ * @see https://floppy-disk.vercel.app/docs/api#createmutation
8
+ */
6
9
  const createMutation = (mutationFn, options = {}) => {
7
10
  const { onMutate = utils_1.noop, onSuccess = utils_1.noop, onError = utils_1.noop, onSettled = utils_1.noop, ...createStoreOptions } = options;
8
11
  const useMutation = (0, create_store_1.createStore)(({ set, get }) => ({
@@ -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
  */
@@ -197,7 +199,9 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
197
199
  *
198
200
  * Disabled by default.
199
201
  *
200
- * If the query is on error state, the polling interval will be disabled, and it will use `retry` instead.
202
+ * If last data fetching is failed, the polling interval will be disabled, and it will use `retry` mechanism instead.
203
+ *
204
+ * @see https://floppy-disk.vercel.app/docs/query/polling
201
205
  */
202
206
  refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError>) => number | false);
203
207
  };
@@ -257,4 +261,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
257
261
  error?: FunctionComponent<TKey>;
258
262
  }) => JSX.Element;
259
263
  };
264
+ /**
265
+ * @see https://floppy-disk.vercel.app/docs/api#createquery
266
+ */
260
267
  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>) => UseQuery<TKey, TResponse, TData, TError>;
@@ -34,6 +34,9 @@ const useQueryDefaultDeps = (state) => [
34
34
  state.isWaitingNextPage,
35
35
  state.hasNextPage,
36
36
  ];
37
+ /**
38
+ * @see https://floppy-disk.vercel.app/docs/api#createquery
39
+ */
37
40
  const createQuery = (queryFn, options = {}) => {
38
41
  const defaultFetchOnWindowFocus = options.fetchOnMount ?? true;
39
42
  const { onFirstSubscribe = utils_1.noop, onSubscribe = utils_1.noop, onLastUnsubscribe = utils_1.noop, onBeforeChangeKey = utils_1.noop, defaultDeps = useQueryDefaultDeps, select = utils_1.identityFn, staleTime = 3000, // 3 seconds
@@ -86,7 +89,7 @@ const createQuery = (queryFn, options = {}) => {
86
89
  .then((response) => {
87
90
  if (preventReplaceResponse.get(keyHash)) {
88
91
  set({ isWaiting: false });
89
- return;
92
+ return resolve(get());
90
93
  }
91
94
  responseAllPages.push(response);
92
95
  const newPageParam = getNextPageParam(response, responseAllPages.length);
@@ -183,18 +186,27 @@ const createQuery = (queryFn, options = {}) => {
183
186
  return;
184
187
  forceFetch();
185
188
  };
186
- const fetchNextPage = () => {
189
+ const fetchNextPage = () => new Promise((resolve) => {
190
+ const state = get();
187
191
  if (typeof options.getNextPageParam !== 'function') {
188
- return console.warn('fetchNextPage with invalid getNextPageParam option');
192
+ console.warn('fetchNextPage with invalid getNextPageParam option');
193
+ return resolve(state);
189
194
  }
190
- const state = get();
191
195
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
192
196
  if (isLoading)
193
- return forceFetch();
197
+ return resolve(forceFetch());
194
198
  if (isWaitingNextPage || !hasNextPage)
195
- 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);
196
207
  set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
197
208
  clearTimeout(retryNextPageTimeoutId.get(keyHash));
209
+ const stateBeforeCallQuery = get();
198
210
  queryFn(key, { ...state, pageParam })
199
211
  .then((response) => {
200
212
  const newPageParam = getNextPageParam(response, pageParams.length);
@@ -207,6 +219,8 @@ const createQuery = (queryFn, options = {}) => {
207
219
  pageParams: pageParams.concat(newPageParam),
208
220
  hasNextPage: (0, utils_1.hasValue)(newPageParam),
209
221
  });
222
+ onSuccess(response, stateBeforeCallQuery);
223
+ resolve(get());
210
224
  })
211
225
  .catch((error) => {
212
226
  const prevState = get();
@@ -224,8 +238,13 @@ const createQuery = (queryFn, options = {}) => {
224
238
  fetchNextPage();
225
239
  }, delay));
226
240
  }
241
+ onError(error, stateBeforeCallQuery);
242
+ resolve(get());
243
+ })
244
+ .finally(() => {
245
+ onSettled(stateBeforeCallQuery);
227
246
  });
228
- };
247
+ });
229
248
  return {
230
249
  ...INITIAL_QUERY_STATE,
231
250
  key,
@@ -23,6 +23,9 @@ export type UseStore<T extends StoreData> = {
23
23
  setDefaultValues: (values: SetStoreData<T>) => void;
24
24
  Watch: (props: WatchProps<T>) => any;
25
25
  };
26
+ /**
27
+ * @see https://floppy-disk.vercel.app/docs/api#createstore
28
+ */
26
29
  export declare const createStore: <T extends StoreData>(initializer: StoreInitializer<T>, options?: InitStoreOptions<T> & {
27
30
  defaultDeps?: SelectDeps<T>;
28
31
  }) => UseStore<T>;
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createStore = void 0;
4
4
  const react_1 = require("react");
5
5
  const vanilla_1 = require("../vanilla");
6
+ /**
7
+ * @see https://floppy-disk.vercel.app/docs/api#createstore
8
+ */
6
9
  const createStore = (initializer, options = {}) => {
7
10
  const { get, set, subscribe, getSubscribers } = (0, vanilla_1.initStore)(initializer, options);
8
11
  const { defaultDeps } = options;
@@ -46,5 +46,8 @@ export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends Stor
46
46
  defaultDeps?: SelectDeps<T>;
47
47
  hashKeyFn?: (obj: TKey) => string;
48
48
  };
49
+ /**
50
+ * @see https://floppy-disk.vercel.app/docs/api#createstores
51
+ */
49
52
  export declare const createStores: <TKey extends StoreKey = StoreKey, T extends StoreData = StoreData>(initializer: StoresInitializer<TKey, T>, options?: CreateStoresOptions<TKey, T>) => UseStores<TKey, T>;
50
53
  export {};
@@ -4,6 +4,9 @@ exports.createStores = void 0;
4
4
  const react_1 = require("react");
5
5
  const utils_1 = require("../utils");
6
6
  const vanilla_1 = require("../vanilla");
7
+ /**
8
+ * @see https://floppy-disk.vercel.app/docs/api#createstores
9
+ */
7
10
  const createStores = (initializer, options = {}) => {
8
11
  const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey, } = options;
9
12
  const stores = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "2.4.0-beta.3",
3
+ "version": "2.5.0-beta.1",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",