Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Poll for defaultAccount to update dapp & overlay subscriptions (#4417)
Browse files Browse the repository at this point in the history
* Poll for defaultAccount (Fixes #4413)

* Fix nextTimeout on catch

* Store timers

* Re-enable default updates on change detection
  • Loading branch information
jacogr committed Feb 3, 2017
1 parent e947c55 commit 1e2c126
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 3 additions & 2 deletions js/src/api/subscriptions/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class Eth {
this._started = false;

this._lastBlock = new BigNumber(-1);
this._pollTimerId = null;
}

get isStarted () {
Expand All @@ -37,7 +38,7 @@ export default class Eth {

_blockNumber = () => {
const nextTimeout = (timeout = 1000) => {
setTimeout(() => {
this._pollTimerId = setTimeout(() => {
this._blockNumber();
}, timeout);
};
Expand All @@ -57,6 +58,6 @@ export default class Eth {

nextTimeout();
})
.catch(nextTimeout);
.catch(() => nextTimeout());
}
}
34 changes: 30 additions & 4 deletions js/src/api/subscriptions/personal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -85,7 +111,7 @@ export default class Personal {

case 'parity_setDappsAddresses':
case 'parity_setNewDappsWhitelist':
this._defaultAccount();
this._defaultAccount(true);
return;
}
});
Expand Down
3 changes: 3 additions & 0 deletions js/src/api/subscriptions/personal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function stubApi (accounts, info) {

return {
_calls,
transport: {
isConnected: true
},
parity: {
allAccountsInfo: () => {
const stub = sinon.stub().resolves(info || TEST_INFO)();
Expand Down

0 comments on commit 1e2c126

Please sign in to comment.