diff --git a/js/src/api/subscriptions/eth.js b/js/src/api/subscriptions/eth.js index 506201e6b..8e56f335f 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 bbc3c0876..1574dcacc 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 = () => { @@ -95,7 +121,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 e4d683766..ac046d250 100644 --- a/js/src/api/subscriptions/personal.spec.js +++ b/js/src/api/subscriptions/personal.spec.js @@ -36,6 +36,9 @@ function stubApi (accounts, info) { return { _calls, + transport: { + isConnected: true + }, parity: { accountsInfo: () => { const stub = sinon.stub().resolves(info || TEST_INFO)();