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
This commit is contained in:
parent
d1728cca28
commit
f48a2df6e5
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
});
|
||||
|
@ -36,6 +36,9 @@ function stubApi (accounts, info) {
|
||||
|
||||
return {
|
||||
_calls,
|
||||
transport: {
|
||||
isConnected: true
|
||||
},
|
||||
parity: {
|
||||
accountsInfo: () => {
|
||||
const stub = sinon.stub().resolves(info || TEST_INFO)();
|
||||
|
Loading…
Reference in New Issue
Block a user