From d45a0ca7ad82dee68fd8c61481d52ab77550fdce Mon Sep 17 00:00:00 2001 From: Nam Nguyen Date: Tue, 17 Oct 2023 09:56:24 +0700 Subject: [PATCH] optimize caching --- src/hooks/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/hooks/index.ts b/src/hooks/index.ts index dad2829559..207904c8c3 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -132,15 +132,18 @@ const wrapProvider = (provider: Web3Provider, blackjackData: BlackjackCheck): We return target[prop as keyof Web3Provider] }, }) -const cache = new Map() +const cacheProvider = new WeakMap() const useWrappedProvider = () => { const { provider, account } = useWeb3ReactCore() const { data: blackjackData } = useCheckBlackjackQuery(account ?? '', { skip: !account }) if (!provider) return undefined if (!blackjackData) return undefined - const wrappedProvider = cache.get(provider) || wrapProvider(provider, blackjackData) - cache.set(provider, wrappedProvider) + let wrappedProvider = cacheProvider.get(provider) + if (!wrappedProvider) { + wrappedProvider = wrapProvider(provider, blackjackData) + cacheProvider.set(provider, wrappedProvider) + } return wrappedProvider }