-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add hook WaitForUtxosToBeSpent
- Loading branch information
1 parent
7fdaa21
commit d1c74b4
Showing
3 changed files
with
116 additions
and
139 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,64 @@ | ||
import { useEffect } from 'react' | ||
import { useReloadCurrentWalletInfo } from '../context/WalletContext' | ||
import { UtxoId } from '../libs/JmWalletApi' | ||
|
||
// Delaying the poll requests gives the wallet some time to synchronize | ||
// the utxo set and reduces amount of http requests | ||
const DEFAUL_DELAY: Milliseconds = 1_000 | ||
|
||
interface WaitForUtxosToBeSpentArgs { | ||
waitForUtxosToBeSpent: UtxoId[] | ||
setWaitForUtxosToBeSpent: (utxos: UtxoId[]) => void | ||
onError: (error: any) => void | ||
delay?: Milliseconds | ||
resetOnErrors?: boolean | ||
} | ||
|
||
// This callback is responsible for updating the utxo array when a | ||
// payment is made. The wallet needs some time after a tx is sent | ||
// to reflect the changes internally. All outputs given in | ||
// `waitForUtxosToBeSpent` must have been removed from the wallet | ||
// for a payment to be considered done. | ||
export const useWaitForUtxosToBeSpent = ({ | ||
waitForUtxosToBeSpent, | ||
setWaitForUtxosToBeSpent, | ||
onError, | ||
delay = DEFAUL_DELAY, | ||
resetOnErrors = true, | ||
}: WaitForUtxosToBeSpentArgs): void => { | ||
const reloadCurrentWalletInfo = useReloadCurrentWalletInfo() | ||
return useEffect(() => { | ||
if (waitForUtxosToBeSpent.length === 0) return | ||
|
||
const abortCtrl = new AbortController() | ||
|
||
const timer = setTimeout(() => { | ||
if (abortCtrl.signal.aborted) return | ||
|
||
reloadCurrentWalletInfo | ||
.reloadUtxos({ signal: abortCtrl.signal }) | ||
.then((res) => { | ||
if (abortCtrl.signal.aborted) return | ||
|
||
const outputs = res.utxos.map((it) => it.utxo) | ||
const utxosStillPresent = waitForUtxosToBeSpent.filter((it) => outputs.includes(it)) | ||
|
||
// updating the utxos array will trigger a recursive call | ||
setWaitForUtxosToBeSpent([...utxosStillPresent]) | ||
}) | ||
.catch((error: any) => { | ||
if (abortCtrl.signal.aborted) return | ||
if (resetOnErrors) { | ||
// Stop waiting for wallet synchronization on errors | ||
setWaitForUtxosToBeSpent([]) | ||
} | ||
onError(error) | ||
}) | ||
}, delay) | ||
|
||
return () => { | ||
abortCtrl.abort() | ||
clearTimeout(timer) | ||
} | ||
}, [waitForUtxosToBeSpent, setWaitForUtxosToBeSpent, resetOnErrors, onError, delay, reloadCurrentWalletInfo]) | ||
} |