From d118e31fe22e0229294604ad95c9691c94b469e9 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Thu, 13 Jun 2024 09:32:28 +0800 Subject: [PATCH] type: update useUniqueMemo params type (#49389) --- .../_util/__tests__/useUniqueMemo.test.tsx | 2 +- components/_util/hooks/useUniqueMemo.ts | 25 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/components/_util/__tests__/useUniqueMemo.test.tsx b/components/_util/__tests__/useUniqueMemo.test.tsx index 7c0a5d134e..1e308c11bb 100644 --- a/components/_util/__tests__/useUniqueMemo.test.tsx +++ b/components/_util/__tests__/useUniqueMemo.test.tsx @@ -19,7 +19,7 @@ describe('Table', () => { let calledTimes = 0; - const Test = ({ depName }: { depName: string }) => { + const Test: React.FC<{ depName?: string }> = ({ depName }) => { useUniqueMemo(() => { calledTimes += 1; return depName; diff --git a/components/_util/hooks/useUniqueMemo.ts b/components/_util/hooks/useUniqueMemo.ts index 972063c7be..a757c48049 100644 --- a/components/_util/hooks/useUniqueMemo.ts +++ b/components/_util/hooks/useUniqueMemo.ts @@ -6,20 +6,20 @@ const BEAT_LIMIT = 1000 * 60 * 10; * A helper class to map keys to values. * It supports both primitive keys and object keys. */ -class ArrayKeyMap { - map = new Map(); +class ArrayKeyMap { + map = new Map(); // Use WeakMap to avoid memory leak - objectIDMap = new WeakMap(); + objectIDMap = new WeakMap(); nextID = 0; - lastAccessBeat = new Map(); + lastAccessBeat = new Map(); // We will clean up the cache when reach the limit accessBeat = 0; - set(keys: any[], value: any) { + set(keys: React.DependencyList, value: any) { // New set will trigger clear this.clear(); @@ -29,7 +29,7 @@ class ArrayKeyMap { this.lastAccessBeat.set(compositeKey, Date.now()); } - get(keys: any[]) { + get(keys: React.DependencyList) { const compositeKey = this.getCompositeKey(keys); const cache = this.map.get(compositeKey); @@ -39,8 +39,8 @@ class ArrayKeyMap { return cache; } - getCompositeKey(keys: any[]) { - const ids = keys.map((key) => { + getCompositeKey(keys: React.DependencyList) { + const ids = keys.map((key) => { if (key && typeof key === 'object') { return `obj_${this.getObjectID(key)}`; } @@ -82,15 +82,16 @@ const uniqueMap = new ArrayKeyMap(); /** * Like `useMemo`, but this hook result will be shared across all instances. */ -export default function useUniqueMemo(memoFn: () => T, deps: any[]): T { - return React.useMemo(() => { +function useUniqueMemo(memoFn: () => T, deps: React.DependencyList) { + return React.useMemo(() => { const cachedValue = uniqueMap.get(deps); if (cachedValue) { - return cachedValue; + return cachedValue as T; } - const newValue = memoFn(); uniqueMap.set(deps, newValue); return newValue; }, deps); } + +export default useUniqueMemo;