Browse Source

Fix LibcoreBridge#synchronize to be properly unsubscribed

master
Gaëtan Renaudeau 7 years ago
parent
commit
36698c5856
  1. 88
      src/bridge/LibcoreBridge.js

88
src/bridge/LibcoreBridge.js

@ -1,9 +1,8 @@
// @flow // @flow
import React from 'react' import React from 'react'
import { BigNumber } from 'bignumber.js' import { BigNumber } from 'bignumber.js'
import { Observable } from 'rxjs'
import LRU from 'lru-cache'
import { map } from 'rxjs/operators' import { map } from 'rxjs/operators'
import LRU from 'lru-cache'
import type { Account } from '@ledgerhq/live-common/lib/types' import type { Account } from '@ledgerhq/live-common/lib/types'
import { decodeAccount, encodeAccount } from 'reducers/accounts' import { decodeAccount, encodeAccount } from 'reducers/accounts'
import FeesBitcoinKind from 'components/FeesField/BitcoinKind' import FeesBitcoinKind from 'components/FeesField/BitcoinKind'
@ -121,58 +120,41 @@ const LibcoreBridge: WalletBridge<Transaction> = {
}, },
synchronize: account => synchronize: account =>
Observable.create(o => { libcoreSyncAccount.send({ rawAccount: encodeAccount(account) }).pipe(
// FIXME TODO: map(rawSyncedAccount => {
// - when you implement addPendingOperation you also here need to: const syncedAccount = decodeAccount(rawSyncedAccount)
// - if there were pendingOperations that are now in operations, remove them as well. return account => {
// - if there are pendingOperations that is older than a threshold (that depends on blockchain speed typically) const accountOps = account.operations
// then we probably should trash them out? it's a complex question for UI const syncedOps = syncedAccount.operations
;(async () => { const patch: $Shape<Account> = {
try { id: syncedAccount.id,
const rawAccount = encodeAccount(account) freshAddress: syncedAccount.freshAddress,
const rawSyncedAccount = await libcoreSyncAccount.send({ rawAccount }).toPromise() freshAddressPath: syncedAccount.freshAddressPath,
const syncedAccount = decodeAccount(rawSyncedAccount) balance: syncedAccount.balance,
o.next(account => { blockHeight: syncedAccount.blockHeight,
const accountOps = account.operations lastSyncDate: new Date(),
const syncedOps = syncedAccount.operations }
const patch: $Shape<Account> = {
id: syncedAccount.id, const hasChanged =
freshAddress: syncedAccount.freshAddress, accountOps.length !== syncedOps.length || // size change, we do a full refresh for now...
freshAddressPath: syncedAccount.freshAddressPath, (accountOps.length > 0 &&
balance: syncedAccount.balance, syncedOps.length > 0 &&
blockHeight: syncedAccount.blockHeight, (accountOps[0].accountId !== syncedOps[0].accountId ||
lastSyncDate: new Date(), accountOps[0].id !== syncedOps[0].id || // if same size, only check if the last item has changed.
} accountOps[0].blockHeight !== syncedOps[0].blockHeight))
const hasChanged = if (hasChanged) {
accountOps.length !== syncedOps.length || // size change, we do a full refresh for now... patch.operations = syncedAccount.operations
(accountOps.length > 0 && patch.pendingOperations = [] // For now, we assume a change will clean the pendings.
syncedOps.length > 0 && }
(accountOps[0].accountId !== syncedOps[0].accountId ||
accountOps[0].id !== syncedOps[0].id || // if same size, only check if the last item has changed. return {
accountOps[0].blockHeight !== syncedOps[0].blockHeight)) ...account,
...patch,
if (hasChanged) { }
patch.operations = syncedAccount.operations
patch.pendingOperations = [] // For now, we assume a change will clean the pendings.
}
return {
...account,
...patch,
}
})
o.complete()
} catch (e) {
o.error(e)
} }
})() }),
return { ),
unsubscribe() {
// LibcoreBridge: unsub sync not currently implemented
},
}
}),
pullMoreOperations: () => Promise.reject(notImplemented), pullMoreOperations: () => Promise.reject(notImplemented),

Loading…
Cancel
Save