Skip to content

Commit

Permalink
fix(sweep): reload wallet info after scheduled sweep (#530)
Browse files Browse the repository at this point in the history
* fix(sweep): wait for scheduler start/stop

* fix(sweep): reload wallet info after scheduled sweep
  • Loading branch information
theborakompanioni authored Oct 5, 2022
1 parent 59c6f4a commit 0757280
Showing 1 changed file with 46 additions and 34 deletions.
80 changes: 46 additions & 34 deletions src/components/Jam.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function Jam() {

const [alert, setAlert] = useState(null)
const [isLoading, setIsLoading] = useState(true)
const [schedule, setSchedule] = useState(null)
const [isWaitingSchedulerStart, setIsWaitingSchedulerStart] = useState(false)
const [isWaitingSchedulerStop, setIsWaitingSchedulerStop] = useState(false)
const collaborativeOperationRunning = useMemo(
Expand Down Expand Up @@ -103,44 +104,70 @@ export default function Jam() {
}, [useInsecureTestingSettings, getNewAddressesForAccounts])

useEffect(() => {
const abortCtrl = new AbortController()

setAlert(null)
setIsLoading(true)

const abortCtrl = new AbortController()
const loadingServiceInfo = reloadServiceInfo({ signal: abortCtrl.signal }).catch((err) => {
if (abortCtrl.signal.aborted) return
const message = err.message || t('send.error_loading_wallet_failed')
!abortCtrl.signal.aborted && setAlert({ variant: 'danger', message })
setAlert({ variant: 'danger', message })
})

const loadingWalletInfo = reloadCurrentWalletInfo({ signal: abortCtrl.signal }).catch((err) => {
if (abortCtrl.signal.aborted) return
const message = err.message || t('send.error_loading_wallet_failed')
!abortCtrl.signal.aborted && setAlert({ variant: 'danger', message })
setAlert({ variant: 'danger', message })
})

Promise.all([loadingServiceInfo, loadingWalletInfo]).finally(() => !abortCtrl.signal.aborted && setIsLoading(false))
Promise.all([loadingServiceInfo, loadingWalletInfo]).finally(() => {
if (abortCtrl.signal.aborted) return
setIsLoading(false)
})

return () => abortCtrl.abort()
return () => {
abortCtrl.abort()
}
}, [reloadServiceInfo, reloadCurrentWalletInfo, t])

useEffect(() => {
if (!serviceInfo) return

setIsWaitingSchedulerStart((current) => (current && serviceInfo.schedule ? false : current))
setIsWaitingSchedulerStop((current) => (current && !serviceInfo.schedule ? false : current))
const scheduleUpdate = serviceInfo.schedule
setSchedule(scheduleUpdate)

setIsWaitingSchedulerStart((current) => (current && scheduleUpdate ? false : current))
setIsWaitingSchedulerStop((current) => (current && !scheduleUpdate ? false : current))

if (serviceInfo.schedule && process.env.NODE_ENV === 'development') {
console.table(serviceInfo.schedule)
if (scheduleUpdate && process.env.NODE_ENV === 'development') {
console.table(scheduleUpdate)
}
}, [serviceInfo])

useEffect(() => {
// Due to polling, using `collaborativeOperationRunning` instead of
// `schedule` here, as a schedule object might still be present when
// the scheduler is actually not running anymore. Reload wallet data
// only when no collaborative operation is running anymore.
if (collaborativeOperationRunning) return

setIsLoading(true)
const abortCtrl = new AbortController()
reloadCurrentWalletInfo({ signal: abortCtrl.signal }).finally(() => {
if (abortCtrl.signal.aborted) return
setIsLoading(false)
})
return () => {
abortCtrl.abort()
}
}, [collaborativeOperationRunning, reloadCurrentWalletInfo])

const startSchedule = async (values) => {
if (isLoading || collaborativeOperationRunning) {
return
}

setAlert(null)
setIsLoading(true)
setIsWaitingSchedulerStart(true)

const destinations = [values.dest1, values.dest2, values.dest3]
Expand Down Expand Up @@ -172,10 +199,6 @@ export default function Jam() {
setAlert({ variant: 'danger', message: err.message })
setIsWaitingSchedulerStart(false)
})
.finally(() => {
if (abortCtrl.signal.aborted) return
setIsLoading(false)
})
}

const stopSchedule = async () => {
Expand All @@ -184,27 +207,17 @@ export default function Jam() {
}

setAlert(null)
setIsLoading(true)
setIsWaitingSchedulerStop(true)

const abortCtrl = new AbortController()
return Api.getTakerStop({ signal: abortCtrl.signal, walletName: wallet.name, token: wallet.token })
.then((res) => (res.ok ? true : Api.Helper.throwError(res, t('scheduler.error_stopping_schedule_failed'))))
.then((_) =>
Promise.all([
reloadServiceInfo({ signal: abortCtrl.signal }),
reloadCurrentWalletInfo({ signal: abortCtrl.signal }),
])
)
.then((_) => reloadServiceInfo({ signal: abortCtrl.signal }))
.catch((err) => {
if (abortCtrl.signal.aborted) return
setAlert({ variant: 'danger', message: err.message })
setIsWaitingSchedulerStop(false)
})
.finally(() => {
if (abortCtrl.signal.aborted) return
setIsLoading(false)
})
}

return (
Expand All @@ -218,16 +231,15 @@ export default function Jam() {
</rb.Placeholder>
) : (
<>
{collaborativeOperationRunning && serviceInfo?.schedule && (
{collaborativeOperationRunning && (
<div className="mb-4">
<ScheduleProgress schedule={serviceInfo.schedule} />
{schedule ? (
<ScheduleProgress schedule={schedule} />
) : (
<rb.Alert variant="info">{t('send.text_coinjoin_already_running')}</rb.Alert>
)}
</div>
)}
{collaborativeOperationRunning && serviceInfo && !serviceInfo.schedule && (
<rb.Alert variant="info" className="mb-4">
{t('send.text_coinjoin_already_running')}
</rb.Alert>
)}
<rb.Fade
in={!collaborativeOperationRunning && !schedulerPreconditionSummary.isFulfilled}
mountOnEnter={true}
Expand Down Expand Up @@ -263,7 +275,7 @@ export default function Jam() {
</>
)}
{((!collaborativeOperationRunning && walletInfo && serviceInfo) ||
(collaborativeOperationRunning && serviceInfo?.schedule)) && (
(collaborativeOperationRunning && schedule)) && (
<Formik
initialValues={initialFormValues}
validate={(values) => {
Expand Down

0 comments on commit 0757280

Please sign in to comment.