Skip to content

Commit

Permalink
optimize caching
Browse files Browse the repository at this point in the history
  • Loading branch information
namgold committed Oct 17, 2023
1 parent afa33d1 commit d45a0ca
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,18 @@ const wrapProvider = (provider: Web3Provider, blackjackData: BlackjackCheck): We
return target[prop as keyof Web3Provider]
},
})
const cache = new Map<Web3Provider, Web3Provider>()
const cacheProvider = new WeakMap<Web3Provider, Web3Provider>()
const useWrappedProvider = () => {
const { provider, account } = useWeb3ReactCore<Web3Provider>()
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
}

Expand Down

0 comments on commit d45a0ca

Please sign in to comment.