floppy-disk 2.3.1 → 2.4.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.
@@ -72,7 +72,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
72
72
  *
73
73
  * `"success"` = has data.
74
74
  *
75
- * `"error"` = has error.
75
+ * `"error"` = has error and no data.
76
76
  *
77
77
  * It has no relation with network fetching state.
78
78
  * If you're looking for network fetching state, use `isWaiting` instead.
@@ -181,6 +181,17 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
181
181
  onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
182
182
  onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
183
183
  onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
184
+ /**
185
+ * Cache time in miliseconds.
186
+ *
187
+ * When a query becomes inactive (no longer have subscribers), it will be reset after this duration,
188
+ * and the cache data will be garbage collected.
189
+ *
190
+ * Set it to `Infinity` to disable garbage collection.
191
+ *
192
+ * Defaults to `5 * 60 * 1000` (5 minutes).
193
+ */
194
+ cacheTime?: number;
184
195
  };
185
196
  export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError>> & {
186
197
  /**
@@ -44,9 +44,10 @@ export const createQuery = (queryFn, options = {}) => {
44
44
  const defaultFetchOnWindowFocus = options.fetchOnMount ?? true;
45
45
  const { onFirstSubscribe = noop, onSubscribe = noop, onLastUnsubscribe = noop, onBeforeChangeKey = noop, defaultDeps = useQueryDefaultDeps, select = identityFn, staleTime = 3000, // 3 seconds
46
46
  fetchOnMount = true, fetchOnWindowFocus = defaultFetchOnWindowFocus, enabled = true, retry = 1, retryDelay = 2000, // 2 seconds
47
- keepPreviousData, getNextPageParam = () => undefined, onSuccess = noop, onError = noop, onSettled = noop, ...createStoresOptions } = options;
47
+ keepPreviousData, getNextPageParam = () => undefined, onSuccess = noop, onError = noop, onSettled = noop, cacheTime = 5 * 60 * 1000, ...createStoresOptions } = options;
48
48
  const retryTimeoutId = new Map();
49
49
  const retryNextPageTimeoutId = new Map();
50
+ const resetTimeoutId = new Map();
50
51
  const preventReplaceResponse = new Map(); // Prevent optimistic data to be replaced
51
52
  const useQuery = createStores(({ get, set, key: _key, keyHash }) => {
52
53
  const key = _key;
@@ -240,6 +241,7 @@ export const createQuery = (queryFn, options = {}) => {
240
241
  if (typeof window !== 'undefined' && fetchOnWindowFocus) {
241
242
  window.addEventListener('focus', fetchWindowFocusHandler);
242
243
  }
244
+ clearTimeout(resetTimeoutId.get(state.keyHash));
243
245
  onFirstSubscribe(state);
244
246
  },
245
247
  onSubscribe: (state) => {
@@ -256,6 +258,11 @@ export const createQuery = (queryFn, options = {}) => {
256
258
  useQuery.set(state.key, { retryCount: 0, retryNextPageCount: 0 }, true);
257
259
  clearTimeout(retryTimeoutId.get(state.keyHash));
258
260
  clearTimeout(retryNextPageTimeoutId.get(state.keyHash));
261
+ if (typeof window !== 'undefined' && cacheTime !== Infinity) {
262
+ resetTimeoutId.set(state.keyHash, window.setTimeout(() => {
263
+ useQuery.set(state.key, INITIAL_QUERY_STATE);
264
+ }, cacheTime));
265
+ }
259
266
  onLastUnsubscribe(state);
260
267
  },
261
268
  onBeforeChangeKey: (nextKey, prevKey) => {
@@ -71,7 +71,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
71
71
  *
72
72
  * `"success"` = has data.
73
73
  *
74
- * `"error"` = has error.
74
+ * `"error"` = has error and no data.
75
75
  *
76
76
  * It has no relation with network fetching state.
77
77
  * If you're looking for network fetching state, use `isWaiting` instead.
@@ -180,6 +180,17 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
180
180
  onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
181
181
  onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
182
182
  onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
183
+ /**
184
+ * Cache time in miliseconds.
185
+ *
186
+ * When a query becomes inactive (no longer have subscribers), it will be reset after this duration,
187
+ * and the cache data will be garbage collected.
188
+ *
189
+ * Set it to `Infinity` to disable garbage collection.
190
+ *
191
+ * Defaults to `5 * 60 * 1000` (5 minutes).
192
+ */
193
+ cacheTime?: number;
183
194
  };
184
195
  export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError>> & {
185
196
  /**
@@ -43,9 +43,10 @@ export const createQuery = (queryFn, options = {}) => {
43
43
  const defaultFetchOnWindowFocus = options.fetchOnMount ?? true;
44
44
  const { onFirstSubscribe = noop, onSubscribe = noop, onLastUnsubscribe = noop, onBeforeChangeKey = noop, defaultDeps = useQueryDefaultDeps, select = identityFn, staleTime = 3000, // 3 seconds
45
45
  fetchOnMount = true, fetchOnWindowFocus = defaultFetchOnWindowFocus, enabled = true, retry = 1, retryDelay = 2000, // 2 seconds
46
- keepPreviousData, getNextPageParam = () => undefined, onSuccess = noop, onError = noop, onSettled = noop, ...createStoresOptions } = options;
46
+ keepPreviousData, getNextPageParam = () => undefined, onSuccess = noop, onError = noop, onSettled = noop, cacheTime = 5 * 60 * 1000, ...createStoresOptions } = options;
47
47
  const retryTimeoutId = new Map();
48
48
  const retryNextPageTimeoutId = new Map();
49
+ const resetTimeoutId = new Map();
49
50
  const preventReplaceResponse = new Map(); // Prevent optimistic data to be replaced
50
51
  const useQuery = createStores(({ get, set, key: _key, keyHash }) => {
51
52
  const key = _key;
@@ -239,6 +240,7 @@ export const createQuery = (queryFn, options = {}) => {
239
240
  if (typeof window !== 'undefined' && fetchOnWindowFocus) {
240
241
  window.addEventListener('focus', fetchWindowFocusHandler);
241
242
  }
243
+ clearTimeout(resetTimeoutId.get(state.keyHash));
242
244
  onFirstSubscribe(state);
243
245
  },
244
246
  onSubscribe: (state) => {
@@ -255,6 +257,11 @@ export const createQuery = (queryFn, options = {}) => {
255
257
  useQuery.set(state.key, { retryCount: 0, retryNextPageCount: 0 }, true);
256
258
  clearTimeout(retryTimeoutId.get(state.keyHash));
257
259
  clearTimeout(retryNextPageTimeoutId.get(state.keyHash));
260
+ if (typeof window !== 'undefined' && cacheTime !== Infinity) {
261
+ resetTimeoutId.set(state.keyHash, window.setTimeout(() => {
262
+ useQuery.set(state.key, INITIAL_QUERY_STATE);
263
+ }, cacheTime));
264
+ }
258
265
  onLastUnsubscribe(state);
259
266
  },
260
267
  onBeforeChangeKey: (nextKey, prevKey) => {
@@ -72,7 +72,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
72
72
  *
73
73
  * `"success"` = has data.
74
74
  *
75
- * `"error"` = has error.
75
+ * `"error"` = has error and no data.
76
76
  *
77
77
  * It has no relation with network fetching state.
78
78
  * If you're looking for network fetching state, use `isWaiting` instead.
@@ -181,6 +181,17 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
181
181
  onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
182
182
  onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
183
183
  onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
184
+ /**
185
+ * Cache time in miliseconds.
186
+ *
187
+ * When a query becomes inactive (no longer have subscribers), it will be reset after this duration,
188
+ * and the cache data will be garbage collected.
189
+ *
190
+ * Set it to `Infinity` to disable garbage collection.
191
+ *
192
+ * Defaults to `5 * 60 * 1000` (5 minutes).
193
+ */
194
+ cacheTime?: number;
184
195
  };
185
196
  export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError>> & {
186
197
  /**
@@ -47,9 +47,10 @@ const createQuery = (queryFn, options = {}) => {
47
47
  const defaultFetchOnWindowFocus = options.fetchOnMount ?? true;
48
48
  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
49
49
  fetchOnMount = true, fetchOnWindowFocus = defaultFetchOnWindowFocus, enabled = true, retry = 1, retryDelay = 2000, // 2 seconds
50
- keepPreviousData, getNextPageParam = () => undefined, onSuccess = utils_1.noop, onError = utils_1.noop, onSettled = utils_1.noop, ...createStoresOptions } = options;
50
+ keepPreviousData, getNextPageParam = () => undefined, onSuccess = utils_1.noop, onError = utils_1.noop, onSettled = utils_1.noop, cacheTime = 5 * 60 * 1000, ...createStoresOptions } = options;
51
51
  const retryTimeoutId = new Map();
52
52
  const retryNextPageTimeoutId = new Map();
53
+ const resetTimeoutId = new Map();
53
54
  const preventReplaceResponse = new Map(); // Prevent optimistic data to be replaced
54
55
  const useQuery = (0, create_stores_1.createStores)(({ get, set, key: _key, keyHash }) => {
55
56
  const key = _key;
@@ -243,6 +244,7 @@ const createQuery = (queryFn, options = {}) => {
243
244
  if (typeof window !== 'undefined' && fetchOnWindowFocus) {
244
245
  window.addEventListener('focus', fetchWindowFocusHandler);
245
246
  }
247
+ clearTimeout(resetTimeoutId.get(state.keyHash));
246
248
  onFirstSubscribe(state);
247
249
  },
248
250
  onSubscribe: (state) => {
@@ -259,6 +261,11 @@ const createQuery = (queryFn, options = {}) => {
259
261
  useQuery.set(state.key, { retryCount: 0, retryNextPageCount: 0 }, true);
260
262
  clearTimeout(retryTimeoutId.get(state.keyHash));
261
263
  clearTimeout(retryNextPageTimeoutId.get(state.keyHash));
264
+ if (typeof window !== 'undefined' && cacheTime !== Infinity) {
265
+ resetTimeoutId.set(state.keyHash, window.setTimeout(() => {
266
+ useQuery.set(state.key, INITIAL_QUERY_STATE);
267
+ }, cacheTime));
268
+ }
262
269
  onLastUnsubscribe(state);
263
270
  },
264
271
  onBeforeChangeKey: (nextKey, prevKey) => {
@@ -71,7 +71,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
71
71
  *
72
72
  * `"success"` = has data.
73
73
  *
74
- * `"error"` = has error.
74
+ * `"error"` = has error and no data.
75
75
  *
76
76
  * It has no relation with network fetching state.
77
77
  * If you're looking for network fetching state, use `isWaiting` instead.
@@ -180,6 +180,17 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
180
180
  onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
181
181
  onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
182
182
  onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
183
+ /**
184
+ * Cache time in miliseconds.
185
+ *
186
+ * When a query becomes inactive (no longer have subscribers), it will be reset after this duration,
187
+ * and the cache data will be garbage collected.
188
+ *
189
+ * Set it to `Infinity` to disable garbage collection.
190
+ *
191
+ * Defaults to `5 * 60 * 1000` (5 minutes).
192
+ */
193
+ cacheTime?: number;
183
194
  };
184
195
  export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError>> & {
185
196
  /**
@@ -46,9 +46,10 @@ const createQuery = (queryFn, options = {}) => {
46
46
  const defaultFetchOnWindowFocus = options.fetchOnMount ?? true;
47
47
  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
48
48
  fetchOnMount = true, fetchOnWindowFocus = defaultFetchOnWindowFocus, enabled = true, retry = 1, retryDelay = 2000, // 2 seconds
49
- keepPreviousData, getNextPageParam = () => undefined, onSuccess = utils_1.noop, onError = utils_1.noop, onSettled = utils_1.noop, ...createStoresOptions } = options;
49
+ keepPreviousData, getNextPageParam = () => undefined, onSuccess = utils_1.noop, onError = utils_1.noop, onSettled = utils_1.noop, cacheTime = 5 * 60 * 1000, ...createStoresOptions } = options;
50
50
  const retryTimeoutId = new Map();
51
51
  const retryNextPageTimeoutId = new Map();
52
+ const resetTimeoutId = new Map();
52
53
  const preventReplaceResponse = new Map(); // Prevent optimistic data to be replaced
53
54
  const useQuery = (0, create_stores_1.createStores)(({ get, set, key: _key, keyHash }) => {
54
55
  const key = _key;
@@ -242,6 +243,7 @@ const createQuery = (queryFn, options = {}) => {
242
243
  if (typeof window !== 'undefined' && fetchOnWindowFocus) {
243
244
  window.addEventListener('focus', fetchWindowFocusHandler);
244
245
  }
246
+ clearTimeout(resetTimeoutId.get(state.keyHash));
245
247
  onFirstSubscribe(state);
246
248
  },
247
249
  onSubscribe: (state) => {
@@ -258,6 +260,11 @@ const createQuery = (queryFn, options = {}) => {
258
260
  useQuery.set(state.key, { retryCount: 0, retryNextPageCount: 0 }, true);
259
261
  clearTimeout(retryTimeoutId.get(state.keyHash));
260
262
  clearTimeout(retryNextPageTimeoutId.get(state.keyHash));
263
+ if (typeof window !== 'undefined' && cacheTime !== Infinity) {
264
+ resetTimeoutId.set(state.keyHash, window.setTimeout(() => {
265
+ useQuery.set(state.key, INITIAL_QUERY_STATE);
266
+ }, cacheTime));
267
+ }
261
268
  onLastUnsubscribe(state);
262
269
  },
263
270
  onBeforeChangeKey: (nextKey, prevKey) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "2.3.1",
3
+ "version": "2.4.0-beta.1",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",