Browse Source

Correct way to unsub (eheh) - thx @gre

master
meriadec 7 years ago
parent
commit
99bf4073af
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 26
      src/components/modals/AddAccounts/steps/03-step-import.js

26
src/components/modals/AddAccounts/steps/03-step-import.js

@ -37,12 +37,10 @@ class StepImport extends PureComponent<StepProps> {
} }
scanSubscription = null scanSubscription = null
_unsubscribed = false
unsub = () => { unsub = () => {
if (this.scanSubscription && !this._unsubscribed) { if (this.scanSubscription) {
this.scanSubscription.unsubscribe() this.scanSubscription.unsubscribe()
this._unsubscribed = true
} }
} }
@ -63,6 +61,7 @@ class StepImport extends PureComponent<StepProps> {
} }
startScanAccountsDevice() { startScanAccountsDevice() {
this.unsub()
const { currency, currentDevice, setState } = this.props const { currency, currentDevice, setState } = this.props
try { try {
invariant(currency, 'No currency to scan') invariant(currency, 'No currency to scan')
@ -75,12 +74,8 @@ class StepImport extends PureComponent<StepProps> {
setState({ scanStatus: 'scanning' }) setState({ scanStatus: 'scanning' })
this._unsubscribed = false
this.scanSubscription = bridge.scanAccountsOnDevice(currency, devicePath, { this.scanSubscription = bridge.scanAccountsOnDevice(currency, devicePath, {
next: account => { next: account => {
// FIXME: this is called even if we unsubscribed
if (this._unsubscribed) return
const { scannedAccounts, checkedAccountsIds, existingAccounts } = this.props const { scannedAccounts, checkedAccountsIds, existingAccounts } = this.props
const hasAlreadyBeenScanned = !!scannedAccounts.find(a => account.id === a.id) const hasAlreadyBeenScanned = !!scannedAccounts.find(a => account.id === a.id)
const hasAlreadyBeenImported = !!existingAccounts.find(a => account.id === a.id) const hasAlreadyBeenImported = !!existingAccounts.find(a => account.id === a.id)
@ -95,16 +90,8 @@ class StepImport extends PureComponent<StepProps> {
}) })
} }
}, },
complete: () => { complete: () => setState({ scanStatus: 'finished' }),
// FIXME: this is called even if we unsubscribed error: err => setState({ scanStatus: 'error', err }),
if (this._unsubscribed) return
setState({ scanStatus: 'finished' })
},
error: err => {
// FIXME: this is called even if we unsubscribed
if (this._unsubscribed) return
setState({ scanStatus: 'error', err })
},
}) })
} catch (err) { } catch (err) {
setState({ scanStatus: 'error', err }) setState({ scanStatus: 'error', err })
@ -112,10 +99,7 @@ class StepImport extends PureComponent<StepProps> {
} }
handleRetry = () => { handleRetry = () => {
if (this.scanSubscription) { this.unsub()
this.scanSubscription.unsubscribe()
this.scanSubscription = null
}
this.handleResetState() this.handleResetState()
this.startScanAccountsDevice() this.startScanAccountsDevice()
} }

Loading…
Cancel
Save