@spoosh/react 0.1.1 → 0.1.3

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/dist/index.js CHANGED
@@ -101,7 +101,11 @@ function createUseRead(options) {
101
101
  controller.getState,
102
102
  controller.getState
103
103
  );
104
- const [requestState, setRequestState] = (0, import_react.useState)({ isPending: false, error: void 0 });
104
+ const [requestState, setRequestState] = (0, import_react.useState)(() => {
105
+ const cachedEntry = stateManager.getCache(queryKey);
106
+ const hasCachedData = cachedEntry?.state?.data !== void 0;
107
+ return { isPending: enabled && !hasCachedData, error: void 0 };
108
+ });
105
109
  const abortRef = (0, import_react.useRef)(controller.abort);
106
110
  abortRef.current = controller.abort;
107
111
  const pluginOptsKey = JSON.stringify(pluginOpts);
@@ -436,11 +440,15 @@ function createUseInfiniteRead(options) {
436
440
  controller.getState,
437
441
  controller.getState
438
442
  );
443
+ const [isPending, setIsPending] = (0, import_react3.useState)(() => {
444
+ return enabled && state.data === void 0;
445
+ });
439
446
  const fetchingDirection = controller.getFetchingDirection();
440
447
  const fetching = fetchingDirection !== null;
441
448
  const fetchingNext = fetchingDirection === "next";
442
449
  const fetchingPrev = fetchingDirection === "prev";
443
- const loading = fetching && state.data === void 0;
450
+ const hasData = state.data !== void 0;
451
+ const loading = (isPending || fetching) && !hasData;
444
452
  const lifecycleRef = (0, import_react3.useRef)({
445
453
  initialized: false,
446
454
  prevContext: null
@@ -461,7 +469,8 @@ function createUseInfiniteRead(options) {
461
469
  (tag) => resolvedTags.includes(tag)
462
470
  );
463
471
  if (hasMatch) {
464
- controller.refetch();
472
+ setIsPending(true);
473
+ controller.refetch().finally(() => setIsPending(false));
465
474
  }
466
475
  }
467
476
  );
@@ -475,7 +484,8 @@ function createUseInfiniteRead(options) {
475
484
  const currentState = controller.getState();
476
485
  const isFetching = controller.getFetchingDirection() !== null;
477
486
  if (currentState.data === void 0 && !isFetching) {
478
- controller.fetchNext();
487
+ setIsPending(true);
488
+ controller.fetchNext().finally(() => setIsPending(false));
479
489
  }
480
490
  }
481
491
  }, [enabled]);
package/dist/index.mjs CHANGED
@@ -85,7 +85,11 @@ function createUseRead(options) {
85
85
  controller.getState,
86
86
  controller.getState
87
87
  );
88
- const [requestState, setRequestState] = useState({ isPending: false, error: void 0 });
88
+ const [requestState, setRequestState] = useState(() => {
89
+ const cachedEntry = stateManager.getCache(queryKey);
90
+ const hasCachedData = cachedEntry?.state?.data !== void 0;
91
+ return { isPending: enabled && !hasCachedData, error: void 0 };
92
+ });
89
93
  const abortRef = useRef(controller.abort);
90
94
  abortRef.current = controller.abort;
91
95
  const pluginOptsKey = JSON.stringify(pluginOpts);
@@ -326,7 +330,13 @@ function createUseWrite(options) {
326
330
  }
327
331
 
328
332
  // src/useInfiniteRead/index.ts
329
- import { useRef as useRef3, useEffect as useEffect2, useSyncExternalStore as useSyncExternalStore3, useId as useId3 } from "react";
333
+ import {
334
+ useRef as useRef3,
335
+ useEffect as useEffect2,
336
+ useSyncExternalStore as useSyncExternalStore3,
337
+ useId as useId3,
338
+ useState as useState3
339
+ } from "react";
330
340
  import {
331
341
  createInfiniteReadController,
332
342
  createSelectorProxy as createSelectorProxy3,
@@ -436,11 +446,15 @@ function createUseInfiniteRead(options) {
436
446
  controller.getState,
437
447
  controller.getState
438
448
  );
449
+ const [isPending, setIsPending] = useState3(() => {
450
+ return enabled && state.data === void 0;
451
+ });
439
452
  const fetchingDirection = controller.getFetchingDirection();
440
453
  const fetching = fetchingDirection !== null;
441
454
  const fetchingNext = fetchingDirection === "next";
442
455
  const fetchingPrev = fetchingDirection === "prev";
443
- const loading = fetching && state.data === void 0;
456
+ const hasData = state.data !== void 0;
457
+ const loading = (isPending || fetching) && !hasData;
444
458
  const lifecycleRef = useRef3({
445
459
  initialized: false,
446
460
  prevContext: null
@@ -461,7 +475,8 @@ function createUseInfiniteRead(options) {
461
475
  (tag) => resolvedTags.includes(tag)
462
476
  );
463
477
  if (hasMatch) {
464
- controller.refetch();
478
+ setIsPending(true);
479
+ controller.refetch().finally(() => setIsPending(false));
465
480
  }
466
481
  }
467
482
  );
@@ -475,7 +490,8 @@ function createUseInfiniteRead(options) {
475
490
  const currentState = controller.getState();
476
491
  const isFetching = controller.getFetchingDirection() !== null;
477
492
  if (currentState.data === void 0 && !isFetching) {
478
- controller.fetchNext();
493
+ setIsPending(true);
494
+ controller.fetchNext().finally(() => setIsPending(false));
479
495
  }
480
496
  }
481
497
  }, [enabled]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spoosh/react",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "license": "MIT",
5
5
  "description": "React hooks for Spoosh API client",
6
6
  "keywords": [
@@ -34,7 +34,7 @@
34
34
  }
35
35
  },
36
36
  "dependencies": {
37
- "@spoosh/core": "0.2.0"
37
+ "@spoosh/core": "0.2.1"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "react": "^18 || ^19"