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

Poll for defaultAccount to update dapp & overlay subscriptions #4417

Merged
merged 4 commits into from
Feb 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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 @@ -89,7 +115,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 @@ -36,6 +36,9 @@ function stubApi (accounts, info) {

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