Skip to content

Commit

Permalink
refactor: leverage session request for polling schedule (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni authored Aug 18, 2022
1 parent 0a7b709 commit 3d777ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 58 deletions.
72 changes: 15 additions & 57 deletions src/components/Jam.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import styles from './Jam.module.css'
// Length of this array must be 3 for now.
const INTERNAL_DEST_ACCOUNTS = [0, 1, 2]
// Interval in milliseconds between requests to reload the schedule.
const SCHEDULE_REQUEST_INTERVAL = process.env.NODE_ENV === 'development' ? 10_000 : 60_000
const SCHEDULER_STOP_RESPONSE_DELAY_MS = 2_000

const SCHEDULER_START_ACCOUNT = 0
Expand Down Expand Up @@ -51,7 +50,6 @@ export default function Jam() {
const [isLoading, setIsLoading] = useState(true)
const [destinationIsExternal, setDestinationIsExternal] = useState(false)
const [collaborativeOperationRunning, setCollaborativeOperationRunning] = useState(false)
const [schedule, setSchedule] = useState(null)

const startJarUtxos = useMemo(() => {
if (!walletInfo) return null
Expand Down Expand Up @@ -135,56 +133,12 @@ export default function Jam() {
}, [reloadServiceInfo, reloadCurrentWalletInfo, t])

useEffect(() => {
const coinjoinInProgress = serviceInfo && serviceInfo.coinjoinInProgress
const makerRunning = serviceInfo && serviceInfo.makerRunning
setCollaborativeOperationRunning(serviceInfo?.coinjoinInProgress || serviceInfo?.makerRunning || false)

setCollaborativeOperationRunning(coinjoinInProgress || makerRunning)
}, [serviceInfo])

const reloadSchedule = useCallback(
({ signal }) => {
return Api.getSchedule({ walletName: wallet.name, token: wallet.token, signal })
.then((res) => (res.ok ? res.json() : Api.Helper.throwError(res)))
.then((data) => {
if (!signal.aborted) {
process.env.NODE_ENV === 'development' && console.log(data.schedule)
setSchedule(data.schedule)
}
})
.catch((err) => {
if (err.response?.status === 404) {
// Not finding a schedule is not an error.
// It means a single collaborative transaction is running.
// Those have no schedule.
return
}

const message = err.message || t('scheduler.error_loading_schedule_failed')
!signal.aborted && setAlert({ variant: 'danger', message })
})
},
[wallet, t]
)

useEffect(() => {
if (!collaborativeOperationRunning) {
return
if (serviceInfo?.schedule && process.env.NODE_ENV === 'development') {
console.table(serviceInfo.schedule)
}

const abortCtrl = new AbortController()

const load = () => {
reloadSchedule({ signal: abortCtrl.signal }).catch((err) => console.error(err))
}

load()

const interval = setInterval(load, SCHEDULE_REQUEST_INTERVAL)
return () => {
clearInterval(interval)
abortCtrl.abort()
}
}, [collaborativeOperationRunning, reloadSchedule])
}, [serviceInfo])

const startSchedule = async (values) => {
if (isLoading || collaborativeOperationRunning || !isSchedulerPreconditionsFulfilled) {
Expand Down Expand Up @@ -214,8 +168,10 @@ export default function Jam() {
}
}

return Api.postSchedulerStart({ walletName: wallet.name, token: wallet.token }, body)
const abortCtrl = new AbortController()
return Api.postSchedulerStart({ signal: abortCtrl.signal, walletName: wallet.name, token: wallet.token }, body)
.then((res) => (res.ok ? true : Api.Helper.throwError(res, t('scheduler.error_starting_schedule_failed'))))
.then((_) => reloadServiceInfo({ signal: abortCtrl.signal }))
.then((_) => setCollaborativeOperationRunning(true))
.catch((err) => {
setAlert({ variant: 'danger', message: err.message })
Expand All @@ -231,10 +187,12 @@ export default function Jam() {
setAlert(null)
setIsLoading(true)

return Api.getSchedulerStop({ walletName: wallet.name, token: wallet.token })
const abortCtrl = new AbortController()
return Api.getSchedulerStop({ 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((_) => setCollaborativeOperationRunning(false))
.then((_) => new Promise((r) => setTimeout(r, SCHEDULER_STOP_RESPONSE_DELAY_MS)))
.then((_) => reloadServiceInfo({ signal: abortCtrl.signal }))
.then((_) => setCollaborativeOperationRunning(false))
.catch((err) => {
setAlert({ variant: 'danger', message: err.message })
})
Expand All @@ -252,12 +210,12 @@ export default function Jam() {
</rb.Placeholder>
) : (
<>
{collaborativeOperationRunning && schedule && (
{collaborativeOperationRunning && serviceInfo?.schedule && (
<div className="mb-4">
<ScheduleProgress schedule={schedule} />
<ScheduleProgress schedule={serviceInfo.schedule} />
</div>
)}
{collaborativeOperationRunning && !schedule && (
{collaborativeOperationRunning && serviceInfo && !serviceInfo.schedule && (
<rb.Alert variant="info" className="mb-4">
{t('send.text_coinjoin_already_running')}
</rb.Alert>
Expand Down Expand Up @@ -336,7 +294,7 @@ export default function Jam() {
</>
)}
{((!collaborativeOperationRunning && walletInfo && serviceInfo) ||
(collaborativeOperationRunning && schedule)) && (
(collaborativeOperationRunning && serviceInfo?.schedule)) && (
<Formik
initialValues={initialFormValues}
validate={(values) => {
Expand Down
1 change: 0 additions & 1 deletion src/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@
"progress_description": "This estimate is the minimum waiting time. Additional delays due to network communication or transaction confirmation are not considered.",
"progress_current_state": "Waiting for transaction <1>{{ current }}</1> of <3>{{ total }}</3> to process...",
"progress_done": "All transactions completed successfully. The scheduler will stop soon.",
"error_loading_schedule_failed": "Loading schedule progress failed.",
"precondition": {
"hint_missing_utxos": "To run the scheduler you need at least one UTXO with <2>{{ minConfirmations }}</2> confirmations in Jar #0. Fund your wallet and wait for <6>{{ minConfirmations }}</6> blocks.",
"hint_missing_confirmations": "The scheduler requires one of your UTXOs in Jar #0 to have <2>{{ minConfirmations }}</2> or more confirmations. Wait for <6>{{ amountOfMissingConfirmations }}</6> more block(s).",
Expand Down

0 comments on commit 3d777ec

Please sign in to comment.