Merge pull request #611 from darkdarkdragon/develop-RLJS-535

add timeout logic to Remote.getLedgerSequence
This commit is contained in:
Chris Clark
2015-10-23 14:28:12 -07:00
2 changed files with 23 additions and 11 deletions

View File

@@ -64,18 +64,20 @@ function getBalanceSheetAsync(address: string, options: BalanceSheetOptions,
const requestCallback = composeAsync(
formatBalanceSheet, convertErrors(callback));
this.remote.getLedgerSequence((err, ledgerVersion) => {
if (err) {
callback(err);
return;
}
if (_.isUndefined(request.ledger_index)) {
this.remote.getLedgerSequence((err, ledgerVersion) => {
if (err) {
convertErrors(callback)(err);
return;
}
if (_.isUndefined(request.ledger_index)) {
request.ledger_index = ledgerVersion;
}
this.remote.rawRequest(request, requestCallback);
});
} else {
this.remote.rawRequest(request, requestCallback);
});
}
}
function getBalanceSheet(address: string, options: BalanceSheetOptions = {}

View File

@@ -535,13 +535,23 @@ Remote.prototype.getLedgerSequence = function(callback = function() {}) {
return;
}
let timeout = null;
function onLedgerClosed() {
clearTimeout(timeout);
callback(null, this._ledger_current_index - 1);
}
if (_.isFinite(this._ledger_current_index)) {
// the "current" ledger is the one after the most recently closed ledger
callback(null, this._ledger_current_index - 1);
} else {
this.once('ledger_closed', () => {
callback(null, this._ledger_current_index - 1);
});
this.once('ledger_closed', onLedgerClosed);
timeout = setTimeout(() => {
this.removeListener('ledger_closed', onLedgerClosed);
callback(new RippleError('timeout',
'Timed out waiting for ledger to close.'));
}, this.pathfind_timeout);
}
};