floppy-disk 2.1.0-beta.2 → 2.1.0

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.
package/README.md CHANGED
@@ -18,13 +18,13 @@ import {
18
18
  useInfiniteQuery,
19
19
  useMutation,
20
20
  } from '@tanstack/react-query'; // 41 kB (gzipped: 11 kB)
21
- import { createQuery, createMutation } from 'floppy-disk'; // 8.2 kB (gzipped: 2.8 kB) 🎉
21
+ import { createQuery, createMutation } from 'floppy-disk'; // 8.4 kB (gzipped: 2.8 kB) 🎉
22
22
  ```
23
23
 
24
24
  - Using Zustand & React-Query: https://demo-zustand-react-query.vercel.app/
25
25
  👉 Total: **309.21 kB**
26
26
  - Using Floppy Disk: https://demo-floppy-disk.vercel.app/
27
- 👉 Total: **270.87 kB** 🎉
27
+ 👉 Total: **271.15 kB** 🎉
28
28
 
29
29
  ## Key Features
30
30
 
@@ -51,7 +51,17 @@ import { createQuery, createMutation } from 'floppy-disk'; // 8.2 kB (gzipped: 2
51
51
  - Can be used with literally any asynchronous data fetching client, including GraphQL ✅
52
52
  - Create mutation ✅
53
53
 
54
- **View official documentation on [floppy-disk.vercel.app](https://floppy-disk.vercel.app/)**
54
+ <br>
55
+
56
+ ---
57
+
58
+ <p align="center">
59
+ View official documentation on <a href="https://floppy-disk.vercel.app/">floppy-disk.vercel.app</a>
60
+ </p>
61
+
62
+ ---
63
+
64
+ <br>
55
65
 
56
66
  ## Table of Contents
57
67
 
@@ -103,7 +103,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
103
103
  isError: false;
104
104
  data: TData;
105
105
  response: TResponse;
106
- responseUpdatedAt: number;
106
+ responseUpdatedAt: number | null;
107
107
  } | {
108
108
  status: 'error';
109
109
  isLoading: false;
@@ -186,6 +186,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
186
186
  setInitialResponse: (options: {
187
187
  key?: TKey;
188
188
  response: TResponse;
189
+ skipRevalidation?: boolean;
189
190
  }) => void;
190
191
  /**
191
192
  * Set query state (data, error, etc) to initial state.
@@ -166,6 +166,9 @@ export const createQuery = (queryFn, options = {}) => {
166
166
  forceFetch();
167
167
  };
168
168
  const fetchNextPage = () => {
169
+ if (typeof options.getNextPageParam !== 'function') {
170
+ return console.warn('fetchNextPage with invalid getNextPageParam option');
171
+ }
169
172
  const state = get();
170
173
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
171
174
  if (isLoading)
@@ -271,7 +274,7 @@ export const createQuery = (queryFn, options = {}) => {
271
274
  },
272
275
  };
273
276
  })());
274
- useQuery.setInitialResponse = ({ key, response }) => {
277
+ useQuery.setInitialResponse = ({ key, response, skipRevalidation }) => {
275
278
  // eslint-disable-next-line react-hooks/rules-of-hooks
276
279
  useState(() => {
277
280
  if (response === undefined || useQuery.get(key).data)
@@ -283,7 +286,7 @@ export const createQuery = (queryFn, options = {}) => {
283
286
  isSuccess: true,
284
287
  isError: false,
285
288
  response,
286
- responseUpdatedAt: Date.now(),
289
+ responseUpdatedAt: skipRevalidation ? Date.now() : null,
287
290
  data: select(response, { key: key, data: null }),
288
291
  pageParam: newPageParam,
289
292
  pageParams: [undefined, newPageParam],
@@ -302,17 +305,17 @@ export const createQuery = (queryFn, options = {}) => {
302
305
  };
303
306
  useQuery.invalidate = () => {
304
307
  useQuery.getStores().forEach((store) => {
305
- const { set, getSubscribers } = store;
308
+ const { get, set, getSubscribers } = store;
306
309
  set({ responseUpdatedAt: null });
307
310
  if (getSubscribers().size > 0)
308
- store.get().forceFetch();
311
+ get().forceFetch();
309
312
  });
310
313
  };
311
314
  useQuery.invalidateSpecificKey = (key) => {
312
- const { set, getSubscribers } = useQuery.getStore(key);
315
+ const { get, set, getSubscribers } = useQuery.getStore(key);
313
316
  set({ responseUpdatedAt: null });
314
317
  if (getSubscribers().size > 0)
315
- useQuery.get(key).forceFetch();
318
+ get().forceFetch();
316
319
  };
317
320
  useQuery.optimisticUpdate = ({ key, response }) => {
318
321
  const prevState = useQuery.get(key);
@@ -103,7 +103,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
103
103
  isError: false;
104
104
  data: TData;
105
105
  response: TResponse;
106
- responseUpdatedAt: number;
106
+ responseUpdatedAt: number | null;
107
107
  } | {
108
108
  status: 'error';
109
109
  isLoading: false;
@@ -186,6 +186,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
186
186
  setInitialResponse: (options: {
187
187
  key?: TKey;
188
188
  response: TResponse;
189
+ skipRevalidation?: boolean;
189
190
  }) => void;
190
191
  /**
191
192
  * Set query state (data, error, etc) to initial state.
@@ -166,6 +166,9 @@ export const createQuery = (queryFn, options = {}) => {
166
166
  forceFetch();
167
167
  };
168
168
  const fetchNextPage = () => {
169
+ if (typeof options.getNextPageParam !== 'function') {
170
+ return console.warn('fetchNextPage with invalid getNextPageParam option');
171
+ }
169
172
  const state = get();
170
173
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
171
174
  if (isLoading)
@@ -271,7 +274,7 @@ export const createQuery = (queryFn, options = {}) => {
271
274
  },
272
275
  };
273
276
  })());
274
- useQuery.setInitialResponse = ({ key, response }) => {
277
+ useQuery.setInitialResponse = ({ key, response, skipRevalidation }) => {
275
278
  // eslint-disable-next-line react-hooks/rules-of-hooks
276
279
  useState(() => {
277
280
  if (response === undefined || useQuery.get(key).data)
@@ -283,7 +286,7 @@ export const createQuery = (queryFn, options = {}) => {
283
286
  isSuccess: true,
284
287
  isError: false,
285
288
  response,
286
- responseUpdatedAt: Date.now(),
289
+ responseUpdatedAt: skipRevalidation ? Date.now() : null,
287
290
  data: select(response, { key: key, data: null }),
288
291
  pageParam: newPageParam,
289
292
  pageParams: [undefined, newPageParam],
@@ -302,17 +305,17 @@ export const createQuery = (queryFn, options = {}) => {
302
305
  };
303
306
  useQuery.invalidate = () => {
304
307
  useQuery.getStores().forEach((store) => {
305
- const { set, getSubscribers } = store;
308
+ const { get, set, getSubscribers } = store;
306
309
  set({ responseUpdatedAt: null });
307
310
  if (getSubscribers().size > 0)
308
- store.get().forceFetch();
311
+ get().forceFetch();
309
312
  });
310
313
  };
311
314
  useQuery.invalidateSpecificKey = (key) => {
312
- const { set, getSubscribers } = useQuery.getStore(key);
315
+ const { get, set, getSubscribers } = useQuery.getStore(key);
313
316
  set({ responseUpdatedAt: null });
314
317
  if (getSubscribers().size > 0)
315
- useQuery.get(key).forceFetch();
318
+ get().forceFetch();
316
319
  };
317
320
  useQuery.optimisticUpdate = ({ key, response }) => {
318
321
  const prevState = useQuery.get(key);
@@ -103,7 +103,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
103
103
  isError: false;
104
104
  data: TData;
105
105
  response: TResponse;
106
- responseUpdatedAt: number;
106
+ responseUpdatedAt: number | null;
107
107
  } | {
108
108
  status: 'error';
109
109
  isLoading: false;
@@ -186,6 +186,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
186
186
  setInitialResponse: (options: {
187
187
  key?: TKey;
188
188
  response: TResponse;
189
+ skipRevalidation?: boolean;
189
190
  }) => void;
190
191
  /**
191
192
  * Set query state (data, error, etc) to initial state.
@@ -169,6 +169,9 @@ const createQuery = (queryFn, options = {}) => {
169
169
  forceFetch();
170
170
  };
171
171
  const fetchNextPage = () => {
172
+ if (typeof options.getNextPageParam !== 'function') {
173
+ return console.warn('fetchNextPage with invalid getNextPageParam option');
174
+ }
172
175
  const state = get();
173
176
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
174
177
  if (isLoading)
@@ -274,7 +277,7 @@ const createQuery = (queryFn, options = {}) => {
274
277
  },
275
278
  };
276
279
  })());
277
- useQuery.setInitialResponse = ({ key, response }) => {
280
+ useQuery.setInitialResponse = ({ key, response, skipRevalidation }) => {
278
281
  // eslint-disable-next-line react-hooks/rules-of-hooks
279
282
  (0, hooks_1.useState)(() => {
280
283
  if (response === undefined || useQuery.get(key).data)
@@ -286,7 +289,7 @@ const createQuery = (queryFn, options = {}) => {
286
289
  isSuccess: true,
287
290
  isError: false,
288
291
  response,
289
- responseUpdatedAt: Date.now(),
292
+ responseUpdatedAt: skipRevalidation ? Date.now() : null,
290
293
  data: select(response, { key: key, data: null }),
291
294
  pageParam: newPageParam,
292
295
  pageParams: [undefined, newPageParam],
@@ -305,17 +308,17 @@ const createQuery = (queryFn, options = {}) => {
305
308
  };
306
309
  useQuery.invalidate = () => {
307
310
  useQuery.getStores().forEach((store) => {
308
- const { set, getSubscribers } = store;
311
+ const { get, set, getSubscribers } = store;
309
312
  set({ responseUpdatedAt: null });
310
313
  if (getSubscribers().size > 0)
311
- store.get().forceFetch();
314
+ get().forceFetch();
312
315
  });
313
316
  };
314
317
  useQuery.invalidateSpecificKey = (key) => {
315
- const { set, getSubscribers } = useQuery.getStore(key);
318
+ const { get, set, getSubscribers } = useQuery.getStore(key);
316
319
  set({ responseUpdatedAt: null });
317
320
  if (getSubscribers().size > 0)
318
- useQuery.get(key).forceFetch();
321
+ get().forceFetch();
319
322
  };
320
323
  useQuery.optimisticUpdate = ({ key, response }) => {
321
324
  const prevState = useQuery.get(key);
@@ -103,7 +103,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
103
103
  isError: false;
104
104
  data: TData;
105
105
  response: TResponse;
106
- responseUpdatedAt: number;
106
+ responseUpdatedAt: number | null;
107
107
  } | {
108
108
  status: 'error';
109
109
  isLoading: false;
@@ -186,6 +186,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
186
186
  setInitialResponse: (options: {
187
187
  key?: TKey;
188
188
  response: TResponse;
189
+ skipRevalidation?: boolean;
189
190
  }) => void;
190
191
  /**
191
192
  * Set query state (data, error, etc) to initial state.
@@ -169,6 +169,9 @@ const createQuery = (queryFn, options = {}) => {
169
169
  forceFetch();
170
170
  };
171
171
  const fetchNextPage = () => {
172
+ if (typeof options.getNextPageParam !== 'function') {
173
+ return console.warn('fetchNextPage with invalid getNextPageParam option');
174
+ }
172
175
  const state = get();
173
176
  const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
174
177
  if (isLoading)
@@ -274,7 +277,7 @@ const createQuery = (queryFn, options = {}) => {
274
277
  },
275
278
  };
276
279
  })());
277
- useQuery.setInitialResponse = ({ key, response }) => {
280
+ useQuery.setInitialResponse = ({ key, response, skipRevalidation }) => {
278
281
  // eslint-disable-next-line react-hooks/rules-of-hooks
279
282
  (0, react_1.useState)(() => {
280
283
  if (response === undefined || useQuery.get(key).data)
@@ -286,7 +289,7 @@ const createQuery = (queryFn, options = {}) => {
286
289
  isSuccess: true,
287
290
  isError: false,
288
291
  response,
289
- responseUpdatedAt: Date.now(),
292
+ responseUpdatedAt: skipRevalidation ? Date.now() : null,
290
293
  data: select(response, { key: key, data: null }),
291
294
  pageParam: newPageParam,
292
295
  pageParams: [undefined, newPageParam],
@@ -305,17 +308,17 @@ const createQuery = (queryFn, options = {}) => {
305
308
  };
306
309
  useQuery.invalidate = () => {
307
310
  useQuery.getStores().forEach((store) => {
308
- const { set, getSubscribers } = store;
311
+ const { get, set, getSubscribers } = store;
309
312
  set({ responseUpdatedAt: null });
310
313
  if (getSubscribers().size > 0)
311
- store.get().forceFetch();
314
+ get().forceFetch();
312
315
  });
313
316
  };
314
317
  useQuery.invalidateSpecificKey = (key) => {
315
- const { set, getSubscribers } = useQuery.getStore(key);
318
+ const { get, set, getSubscribers } = useQuery.getStore(key);
316
319
  set({ responseUpdatedAt: null });
317
320
  if (getSubscribers().size > 0)
318
- useQuery.get(key).forceFetch();
321
+ get().forceFetch();
319
322
  };
320
323
  useQuery.optimisticUpdate = ({ key, response }) => {
321
324
  const prevState = useQuery.get(key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "2.1.0-beta.2",
3
+ "version": "2.1.0",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",