From ef3859ff775e9f36869fcce80eee388039adb2af Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Wed, 21 Aug 2019 16:12:14 -0230 Subject: Add toggle for incoming transactions (#7049) --- app/_locales/en/messages.json | 6 +++ app/scripts/controllers/incoming-transactions.js | 58 ++++++++++++++++++++++-- app/scripts/controllers/preferences.js | 1 + 3 files changed, 60 insertions(+), 5 deletions(-) (limited to 'app') diff --git a/app/_locales/en/messages.json b/app/_locales/en/messages.json index 2b957129d..86bc1397b 100644 --- a/app/_locales/en/messages.json +++ b/app/_locales/en/messages.json @@ -1,4 +1,10 @@ { + "showIncomingTransactions": { + "message": "Show Incoming Transactions" + }, + "showIncomingTransactionsDescription": { + "message": "Select this to use Etherscan to show incoming transactions in the transactions list" + }, "exposeAccounts": { "message": "Expose Accounts" }, diff --git a/app/scripts/controllers/incoming-transactions.js b/app/scripts/controllers/incoming-transactions.js index 1a970cdfe..032ef8a77 100644 --- a/app/scripts/controllers/incoming-transactions.js +++ b/app/scripts/controllers/incoming-transactions.js @@ -56,6 +56,35 @@ class IncomingTransactionsController { }, opts.initState) this.store = new ObservableStore(initState) + this.preferencesController.store.subscribe(pairwise((prevState, currState) => { + const { featureFlags: { showIncomingTransactions: prevShowIncomingTransactions } = {} } = prevState + const { featureFlags: { showIncomingTransactions: currShowIncomingTransactions } = {} } = currState + + if (currShowIncomingTransactions === prevShowIncomingTransactions) { + return + } + + if (prevShowIncomingTransactions && !currShowIncomingTransactions) { + this.stop() + return + } + + this.start() + })) + + this.preferencesController.store.subscribe(pairwise(async (prevState, currState) => { + const { selectedAddress: prevSelectedAddress } = prevState + const { selectedAddress: currSelectedAddress } = currState + + if (currSelectedAddress === prevSelectedAddress) { + return + } + + await this._update({ + address: currSelectedAddress, + }) + })) + this.networkController.on('networkDidChange', async (newType) => { const address = this.preferencesController.getSelectedAddress() await this._update({ @@ -63,14 +92,16 @@ class IncomingTransactionsController { networkType: newType, }) }) - this.preferencesController.store.subscribe(async ({ selectedAddress }) => { - await this._update({ - address: selectedAddress, - }) - }) } start () { + const { featureFlags = {} } = this.preferencesController.store.getState() + const { showIncomingTransactions } = featureFlags + + if (!showIncomingTransactions) { + return + } + this.blockTracker.removeListener('latest', this._onLatestBlock) this.blockTracker.addListener('latest', this._onLatestBlock) } @@ -234,3 +265,20 @@ class IncomingTransactionsController { } module.exports = IncomingTransactionsController + +function pairwise (fn) { + let first = true + let cache + return (value) => { + try { + if (first) { + first = false + return fn(value, value) + } else { + return fn(cache, value) + } + } finally { + cache = value + } + } +} diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js index f02708d40..4ed3afb6c 100644 --- a/app/scripts/controllers/preferences.js +++ b/app/scripts/controllers/preferences.js @@ -41,6 +41,7 @@ class PreferencesController { // for convenient testing of pre-release features, and should never // perform sensitive operations. featureFlags: { + showIncomingTransactions: true, }, knownMethodData: {}, participateInMetaMetrics: null, -- cgit