-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request token and wait has energy call
- Loading branch information
1 parent
e249710
commit 8d4c709
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useCallback, useEffect, useRef } from 'react' | ||
import { useMyAccount } from 'src/stores/my-account' | ||
import useWrapInRef from './useWrapInRef' | ||
|
||
const DEFAULT_TIMEOUT = 30_000 | ||
export default function useWaitHasEnergy( | ||
isUsingConnectedWallet?: boolean, | ||
timeout = DEFAULT_TIMEOUT, | ||
) { | ||
const hasEnergyResolvers = useRef<(() => void)[]>([]) | ||
const { address, energy, resubscribeEnergy } = useAccountSwitch(isUsingConnectedWallet) | ||
const energyRef = useWrapInRef(energy) | ||
|
||
const generateNewPromise = useCallback(() => { | ||
return new Promise<void>((resolve, reject) => { | ||
const timeoutId = setTimeout(() => { | ||
reject(new Error("You don't have enough energy to perform this action. Please try again")) | ||
resubscribeEnergy() | ||
}, timeout) | ||
hasEnergyResolvers.current.push(() => { | ||
clearTimeout(timeoutId) | ||
resolve() | ||
}) | ||
}) | ||
}, [timeout, resubscribeEnergy]) | ||
|
||
useEffect(() => { | ||
if (!energy || energy <= 0 || hasEnergyResolvers.current.length === 0) return | ||
function resolveAllPending() { | ||
hasEnergyResolvers.current.forEach(resolve => resolve()) | ||
hasEnergyResolvers.current = [] | ||
} | ||
resolveAllPending() | ||
}, [energy, generateNewPromise]) | ||
|
||
useEffect(() => { | ||
return () => { | ||
hasEnergyResolvers.current.forEach(resolve => resolve()) | ||
hasEnergyResolvers.current = [] | ||
} | ||
}, [address]) | ||
|
||
return () => { | ||
// need to use ref because if not it can have stale energy value | ||
return !energyRef.current ? generateNewPromise() : Promise.resolve() | ||
} | ||
} | ||
|
||
function useAccountSwitch(isUsingConnectedWallet = false) { | ||
const address = useMyAccount(state => state.address) | ||
const energy = useMyAccount(state => state.energy) | ||
const resubscribeEnergy = useMyAccount(state => state._subscribeEnergy) | ||
|
||
const connectedWallet = useMyAccount(state => state.connectedWallet) | ||
const resubscribeConnectedWalletEnergy = useMyAccount( | ||
state => state._subscribeConnectedWalletEnergy, | ||
) | ||
|
||
const usedData = { address, energy, resubscribeEnergy } | ||
if (isUsingConnectedWallet) { | ||
usedData.address = connectedWallet?.address ?? null | ||
usedData.energy = connectedWallet?.energy ?? 0 | ||
usedData.resubscribeEnergy = resubscribeConnectedWalletEnergy | ||
} | ||
|
||
return usedData | ||
} |