From a3ad738f3c9c3175bd456a3c7e745c8067d4755f Mon Sep 17 00:00:00 2001 From: wuxue Date: Wed, 21 Jun 2023 16:18:27 +0800 Subject: [PATCH 1/2] style: change the default import to import on demand --- .../useRequest/src/plugins/useCachePlugin.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/hooks/src/useRequest/src/plugins/useCachePlugin.ts b/packages/hooks/src/useRequest/src/plugins/useCachePlugin.ts index fd14eb0501..ff3d6f59ba 100644 --- a/packages/hooks/src/useRequest/src/plugins/useCachePlugin.ts +++ b/packages/hooks/src/useRequest/src/plugins/useCachePlugin.ts @@ -2,10 +2,10 @@ import { useRef } from 'react'; import useCreation from '../../../useCreation'; import useUnmount from '../../../useUnmount'; import type { Plugin } from '../types'; -import * as cache from '../utils/cache'; +import { setCache, getCache } from '../utils/cache'; import type { CachedData } from '../utils/cache'; -import * as cachePromise from '../utils/cachePromise'; -import * as cacheSubscribe from '../utils/cacheSubscribe'; +import { setCachePromise, getCachePromise } from '../utils/cachePromise'; +import { trigger, subscribe } from '../utils/cacheSubscribe'; const useCachePlugin: Plugin = ( fetchInstance, @@ -25,16 +25,16 @@ const useCachePlugin: Plugin = ( if (customSetCache) { customSetCache(cachedData); } else { - cache.setCache(key, cacheTime, cachedData); + setCache(key, cacheTime, cachedData); } - cacheSubscribe.trigger(key, cachedData.data); + trigger(key, cachedData.data); }; const _getCache = (key: string, params: any[] = []) => { if (customGetCache) { return customGetCache(params); } - return cache.getCache(key); + return getCache(key); }; useCreation(() => { @@ -53,7 +53,7 @@ const useCachePlugin: Plugin = ( } // subscribe same cachekey update, trigger update - unSubscribeRef.current = cacheSubscribe.subscribe(cacheKey, (data) => { + unSubscribeRef.current = subscribe(cacheKey, (data) => { fetchInstance.setState({ data }); }); }, []); @@ -91,7 +91,7 @@ const useCachePlugin: Plugin = ( } }, onRequest: (service, args) => { - let servicePromise = cachePromise.getCachePromise(cacheKey); + let servicePromise = getCachePromise(cacheKey); // If has servicePromise, and is not trigger by self, then use it if (servicePromise && servicePromise !== currentPromiseRef.current) { @@ -100,7 +100,7 @@ const useCachePlugin: Plugin = ( servicePromise = service(...args); currentPromiseRef.current = servicePromise; - cachePromise.setCachePromise(cacheKey, servicePromise); + setCachePromise(cacheKey, servicePromise); return { servicePromise }; }, onSuccess: (data, params) => { @@ -113,7 +113,7 @@ const useCachePlugin: Plugin = ( time: new Date().getTime(), }); // resubscribe - unSubscribeRef.current = cacheSubscribe.subscribe(cacheKey, (d) => { + unSubscribeRef.current = subscribe(cacheKey, (d) => { fetchInstance.setState({ data: d }); }); } @@ -128,7 +128,7 @@ const useCachePlugin: Plugin = ( time: new Date().getTime(), }); // resubscribe - unSubscribeRef.current = cacheSubscribe.subscribe(cacheKey, (d) => { + unSubscribeRef.current = subscribe(cacheKey, (d) => { fetchInstance.setState({ data: d }); }); } From acc0c983e386c95238b8970476d42da4fb6daaff Mon Sep 17 00:00:00 2001 From: wuxue Date: Thu, 11 Jul 2024 10:29:38 +0800 Subject: [PATCH 2/2] refactor: Wrap the second parameter of the return value from useSetState with useMemoriedFn --- packages/hooks/src/useSetState/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/hooks/src/useSetState/index.ts b/packages/hooks/src/useSetState/index.ts index 495bcfd243..ca8b0db2ba 100644 --- a/packages/hooks/src/useSetState/index.ts +++ b/packages/hooks/src/useSetState/index.ts @@ -1,4 +1,5 @@ -import { useCallback, useState } from 'react'; +import { useState } from 'react'; +import useMemoizedFn from '../useMemoizedFn'; import { isFunction } from '../utils'; export type SetState> = ( @@ -10,12 +11,12 @@ const useSetState = >( ): [S, SetState] => { const [state, setState] = useState(initialState); - const setMergeState = useCallback((patch) => { + const setMergeState = useMemoizedFn((patch) => { setState((prevState) => { const newState = isFunction(patch) ? patch(prevState) : patch; return newState ? { ...prevState, ...newState } : prevState; }); - }, []); + }); return [state, setMergeState]; };