Skip to content

Commit

Permalink
Merge pull request #1247 from RafaelTaranto/backport/delay-network-down
Browse files Browse the repository at this point in the history
LAM-451 feat: delay network down screen if there's no ongoing tx
  • Loading branch information
RafaelTaranto authored Nov 29, 2024
2 parents 6cdb40e + db8ee09 commit 96f0cd0
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions lib/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ const Brain = function (config) {
this.manualTriggersDataProvided = _.zipObject(AUTOMATABLE_REQUIREMENTS, Array(AUTOMATABLE_REQUIREMENTS.length).fill(false))
this.txBlockedByTrigger = false
this.termsAcceptButtonPressed = false
this.networkDownTimeout = null

this.numberOfCassettes = _.isFinite(parseInt(deviceConfig.billDispenser.cassettes))
? parseInt(deviceConfig.billDispenser.cassettes)
Expand Down Expand Up @@ -2346,6 +2347,9 @@ Brain.prototype._chooseCoinScreen = function _chooseCoinsScreen (localeInfo, cas
}

Brain.prototype._chooseCoin = function _chooseCoin (data) {
// Show networkDown screen now if it's been delayed
if (this.networkDown) return this._forceNetworkDown()

const automatableRequirements = _.keys(this.trader.triggersAutomation)
this.manualTriggersDataProvided = _.zipObject(automatableRequirements, Array(automatableRequirements.length).fill(false))
this.txBlockedByTrigger = false
Expand Down Expand Up @@ -2694,6 +2698,17 @@ Brain.prototype._pollUpdate = function _pollUpdate (needsRefresh) {
if (needsRefresh) this._idle()
}

Brain.prototype._clearNetworkDownTimeout = function _clearNetworkDownTimeout () {
if (this.networkDownTimeout)
this.networkDownTimeout = clearTimeout(this.networkDownTimeout)
}

Brain.prototype._setNetworkDownTimeout = function _setNetworkDownTimeout () {
if (!this.networkDownTimeout)
this.networkDownTimeout = setTimeout(() => this._forceNetworkDown(), 5 * 60 * 1000)
return this.networkDownTimeout
}

Brain.prototype._networkDown = function _networkDown () {
if (this.state === 'networkDown') return

Expand All @@ -2705,15 +2720,19 @@ Brain.prototype._networkDown = function _networkDown () {

const tx = this.tx

const doForceDown = !tx ||
!tx.direction ||
(tx.direction === 'cashIn' && _.isEmpty(tx.bills)) ||
// If there's no ongoing transaction we can postpone the network down screen.
if (!tx || !tx.direction)
return this._setNetworkDownTimeout()

const doForceDown = (tx.direction === 'cashIn' && _.isEmpty(tx.bills)) ||
(tx.direction === 'cashOut' && !tx.toAddress)

if (doForceDown) return this._forceNetworkDown()
if (doForceDown) this._forceNetworkDown()
}

Brain.prototype._forceNetworkDown = function _forceNetworkDown () {
this._clearNetworkDownTimeout()

const self = this

this.trader.clearConfigVersion()
Expand All @@ -2734,6 +2753,8 @@ const isNonTx = state => _.includes(state, NON_TX_STATES)

let firstUp = true
Brain.prototype._networkUp = function _networkUp () {
this._clearNetworkDownTimeout()

// Don't go to start screen yet
if (!this.billValidator.hasDenominations()) return

Expand Down Expand Up @@ -3747,6 +3768,9 @@ Brain.prototype._getFiatButtonResponse = function _getFiatButtonResponse () {
}

Brain.prototype._chooseFiat = function _chooseFiat () {
// Show networkDown screen now if it's been delayed
if (this.networkDown) return this._forceNetworkDown()

const amount = this.complianceAmount()
const triggerTx = { fiat: amount, direction: 'cashOut' }

Expand Down

0 comments on commit 96f0cd0

Please sign in to comment.