From 36698c5856866bb25790cb98f5392b3283e7be15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Tue, 24 Jul 2018 09:02:58 +0200 Subject: [PATCH] Fix LibcoreBridge#synchronize to be properly unsubscribed --- src/bridge/LibcoreBridge.js | 88 +++++++++++++++---------------------- 1 file changed, 35 insertions(+), 53 deletions(-) diff --git a/src/bridge/LibcoreBridge.js b/src/bridge/LibcoreBridge.js index c725a98d..db25372c 100644 --- a/src/bridge/LibcoreBridge.js +++ b/src/bridge/LibcoreBridge.js @@ -1,9 +1,8 @@ // @flow import React from 'react' import { BigNumber } from 'bignumber.js' -import { Observable } from 'rxjs' -import LRU from 'lru-cache' import { map } from 'rxjs/operators' +import LRU from 'lru-cache' import type { Account } from '@ledgerhq/live-common/lib/types' import { decodeAccount, encodeAccount } from 'reducers/accounts' import FeesBitcoinKind from 'components/FeesField/BitcoinKind' @@ -121,58 +120,41 @@ const LibcoreBridge: WalletBridge = { }, synchronize: account => - Observable.create(o => { - // FIXME TODO: - // - when you implement addPendingOperation you also here need to: - // - if there were pendingOperations that are now in operations, remove them as well. - // - if there are pendingOperations that is older than a threshold (that depends on blockchain speed typically) - // then we probably should trash them out? it's a complex question for UI - ;(async () => { - try { - const rawAccount = encodeAccount(account) - const rawSyncedAccount = await libcoreSyncAccount.send({ rawAccount }).toPromise() - const syncedAccount = decodeAccount(rawSyncedAccount) - o.next(account => { - const accountOps = account.operations - const syncedOps = syncedAccount.operations - const patch: $Shape = { - id: syncedAccount.id, - freshAddress: syncedAccount.freshAddress, - freshAddressPath: syncedAccount.freshAddressPath, - balance: syncedAccount.balance, - blockHeight: syncedAccount.blockHeight, - lastSyncDate: new Date(), - } - - const hasChanged = - accountOps.length !== syncedOps.length || // size change, we do a full refresh for now... - (accountOps.length > 0 && - 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. - accountOps[0].blockHeight !== syncedOps[0].blockHeight)) - - 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) + libcoreSyncAccount.send({ rawAccount: encodeAccount(account) }).pipe( + map(rawSyncedAccount => { + const syncedAccount = decodeAccount(rawSyncedAccount) + return account => { + const accountOps = account.operations + const syncedOps = syncedAccount.operations + const patch: $Shape = { + id: syncedAccount.id, + freshAddress: syncedAccount.freshAddress, + freshAddressPath: syncedAccount.freshAddressPath, + balance: syncedAccount.balance, + blockHeight: syncedAccount.blockHeight, + lastSyncDate: new Date(), + } + + const hasChanged = + accountOps.length !== syncedOps.length || // size change, we do a full refresh for now... + (accountOps.length > 0 && + 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. + accountOps[0].blockHeight !== syncedOps[0].blockHeight)) + + if (hasChanged) { + patch.operations = syncedAccount.operations + patch.pendingOperations = [] // For now, we assume a change will clean the pendings. + } + + return { + ...account, + ...patch, + } } - })() - return { - unsubscribe() { - // LibcoreBridge: unsub sync not currently implemented - }, - } - }), + }), + ), pullMoreOperations: () => Promise.reject(notImplemented),