@kawaiininja/fetch 1.0.44 → 1.0.45

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.
@@ -1,7 +1,7 @@
1
1
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
2
  import { useApiConfig } from "./useApiConfig";
3
3
  import { useCsrf } from "./useCsrf";
4
- import { getCache, isStale, setCache, setPromise } from "./useFetch.cache";
4
+ import { getCache, isStale, setCache } from "./useFetch.cache";
5
5
  import { parseResponse, performFetch } from "./useFetch.executor";
6
6
  import { createApiMethods } from "./utils";
7
7
  export const useFetch = (endpoint, baseOptions = {}) => {
@@ -21,11 +21,8 @@ export const useFetch = (endpoint, baseOptions = {}) => {
21
21
  const safeSet = useCallback((fn) => {
22
22
  if (!mounted.current)
23
23
  return;
24
- setTimeout(() => {
25
- // @ts-ignore
26
- if (mounted.current)
27
- setState((prev) => ({ ...prev, ...fn(prev) }));
28
- }, 0);
24
+ // Immediate update to prevent stale closures and lost updates
25
+ setState((prev) => ({ ...prev, ...fn(prev) }));
29
26
  }, []);
30
27
  const request = useCallback(async (params = {}) => {
31
28
  const isRead = !params.method || params.method === "GET";
@@ -65,7 +62,9 @@ export const useFetch = (endpoint, baseOptions = {}) => {
65
62
  }
66
63
  })();
67
64
  if (isRead) {
68
- setPromise(cacheKey, execution);
65
+ const entry = getCache(cacheKey);
66
+ if (entry)
67
+ entry.promise = execution;
69
68
  }
70
69
  return execution;
71
70
  }, [
@@ -76,7 +75,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
76
75
  onError,
77
76
  debug,
78
77
  safeSet,
79
- JSON.stringify(baseOptions),
78
+ baseOptions,
80
79
  ]);
81
80
  useEffect(() => {
82
81
  if (!enabled)
@@ -85,7 +84,7 @@ export const useFetch = (endpoint, baseOptions = {}) => {
85
84
  if (shouldFetch) {
86
85
  request();
87
86
  }
88
- }, [enabled, cacheKey, swr, staleTime, request]);
87
+ }, [enabled, cacheKey, swr, staleTime, request, cached?.data]);
89
88
  useEffect(() => {
90
89
  if (!baseOptions.revalidateOnFocus || !enabled)
91
90
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kawaiininja/fetch",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "description": "Core fetch utility for Onyx Framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",