From 1e2c126ad66bad1f11d54394c47b3c72547a3942 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 3 Feb 2017 14:24:23 +0100 Subject: [PATCH] Poll for defaultAccount to update dapp & overlay subscriptions (#4417) * Poll for defaultAccount (Fixes #4413) * Fix nextTimeout on catch * Store timers * Re-enable default updates on change detection --- js/src/api/subscriptions/eth.js | 5 ++-- js/src/api/subscriptions/personal.js | 34 ++++++++++++++++++++--- js/src/api/subscriptions/personal.spec.js | 3 ++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/js/src/api/subscriptions/eth.js b/js/src/api/subscriptions/eth.js index f36d9dc73a9..7369ade49ba 100644 --- a/js/src/api/subscriptions/eth.js +++ b/js/src/api/subscriptions/eth.js @@ -23,6 +23,7 @@ export default class Eth { this._started = false; this._lastBlock = new BigNumber(-1); + this._pollTimerId = null; } get isStarted () { @@ -37,7 +38,7 @@ export default class Eth { _blockNumber = () => { const nextTimeout = (timeout = 1000) => { - setTimeout(() => { + this._pollTimerId = setTimeout(() => { this._blockNumber(); }, timeout); }; @@ -57,6 +58,6 @@ export default class Eth { nextTimeout(); }) - .catch(nextTimeout); + .catch(() => nextTimeout()); } } diff --git a/js/src/api/subscriptions/personal.js b/js/src/api/subscriptions/personal.js index 06766286f81..9c14e95aab2 100644 --- a/js/src/api/subscriptions/personal.js +++ b/js/src/api/subscriptions/personal.js @@ -20,6 +20,9 @@ export default class Personal { this._api = api; this._updateSubscriptions = updateSubscriptions; this._started = false; + + this._lastDefaultAccount = '0x0'; + this._pollTimerId = null; } get isStarted () { @@ -37,12 +40,35 @@ export default class Personal { ]); } - _defaultAccount = () => { + // FIXME: Because of the different API instances, the "wait for valid changes" approach + // doesn't work. Since the defaultAccount is critical to operation, we poll in exactly + // same way we do in ../eth (ala same as eth_blockNumber) and update. This should be moved + // to pub-sub as it becomes available + _defaultAccount = (timerDisabled = false) => { + const nextTimeout = (timeout = 1000) => { + if (!timerDisabled) { + this._pollTimerId = setTimeout(() => { + this._defaultAccount(); + }, timeout); + } + }; + + if (!this._api.transport.isConnected) { + nextTimeout(500); + return; + } + return this._api.parity .defaultAccount() .then((defaultAccount) => { - this._updateSubscriptions('parity_defaultAccount', null, defaultAccount); - }); + if (this._lastDefaultAccount !== defaultAccount) { + this._lastDefaultAccount = defaultAccount; + this._updateSubscriptions('parity_defaultAccount', null, defaultAccount); + } + + nextTimeout(); + }) + .catch(() => nextTimeout()); } _listAccounts = () => { @@ -85,7 +111,7 @@ export default class Personal { case 'parity_setDappsAddresses': case 'parity_setNewDappsWhitelist': - this._defaultAccount(); + this._defaultAccount(true); return; } }); diff --git a/js/src/api/subscriptions/personal.spec.js b/js/src/api/subscriptions/personal.spec.js index 3b8449efcce..7e52e9f9c78 100644 --- a/js/src/api/subscriptions/personal.spec.js +++ b/js/src/api/subscriptions/personal.spec.js @@ -35,6 +35,9 @@ function stubApi (accounts, info) { return { _calls, + transport: { + isConnected: true + }, parity: { allAccountsInfo: () => { const stub = sinon.stub().resolves(info || TEST_INFO)();