implement pagination using skip & limit

This commit is contained in:
Ivan Socolsky 2015-03-20 16:58:54 -03:00
parent f13fc5660c
commit 330c399c07
2 changed files with 19 additions and 11 deletions

View File

@ -913,12 +913,11 @@ WalletService.prototype._normalizeTxHistory = function(txs) {
}; };
/** /**
* Retrieves all transactions (incoming & outgoing) in the range (maxTs-minTs) * Retrieves all transactions (incoming & outgoing)
* Times are in UNIX EPOCH * Times are in UNIX EPOCH
* *
* @param {Object} opts * @param {Object} opts
* @param {Number} opts.minTs (defaults to 0) * @param {Number} opts.skip (defaults to 0)
* @param {Number} opts.maxTs (defaults to now)
* @param {Number} opts.limit * @param {Number} opts.limit
* @returns {TxProposal[]} Transaction proposals, first newer * @returns {TxProposal[]} Transaction proposals, first newer
*/ */
@ -1003,17 +1002,14 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
}; };
function paginate(txs) { function paginate(txs) {
var limited = opts.limit && opts.limit != -1; var skip = opts.skip || 0;
var minTs = opts.minTs || 0; var limited = _.isNumber(opts.limit) && opts.limit != -1;
var maxTs = opts.maxTs || Math.ceil(Date.now() / 1000);
var filtered = _.sortBy(_.filter(txs, function(tx) { var sliced = _.slice(_.sortBy(txs, function(tx) {
return tx.time >= minTs && tx.time <= maxTs;
}), function(tx) {
return -tx.time; return -tx.time;
}); }), skip);
return limited ? _.take(filtered, opts.limit) : filtered; return limited ? _.take(sliced, opts.limit) : sliced;
}; };
// Get addresses for this wallet // Get addresses for this wallet

View File

@ -2383,6 +2383,18 @@ describe('Copay server', function() {
limit: 0, limit: 0,
}, },
expected: [], expected: [],
}, {
opts: {
skip: 4,
limit: 20,
},
expected: [10],
}, {
opts: {
skip: 20,
limit: 1,
},
expected: [],
}]; }];
server._normalizeTxHistory = sinon.stub().returnsArg(0); server._normalizeTxHistory = sinon.stub().returnsArg(0);