diff --git a/lib/contracts/contribution.js b/lib/contracts/contribution.js index e49591a..9033d0c 100644 --- a/lib/contracts/contribution.js +++ b/lib/contracts/contribution.js @@ -1,20 +1,13 @@ -const ethers = require('ethers'); - -const ContributionSerializer = require('../serializers/contribution'); const Base = require('./base'); +const ContributionSerializer = require('../serializers/contribution'); +const paged = require('../utils/pagination'); class Contribution extends Base { - all() { + all(options = {}) { return this.functions.contributionsCount() - .then(async (count) => { - let contributions = []; - - for (let id = 1; id <= count; id++) { - const contribution = await this.getById(id) - contributions.push(contribution); - } - - return contributions; + .then((count) => { + let records = paged(count, options).map((id) => this.getById(id)); + return Promise.all(records); }); } diff --git a/lib/contracts/contributor.js b/lib/contracts/contributor.js index d207773..75f2b2e 100644 --- a/lib/contracts/contributor.js +++ b/lib/contracts/contributor.js @@ -1,19 +1,13 @@ -const RSVP = require('rsvp'); - -const ContributorSerializer = require('../serializers/contributor'); const Base = require('./base'); +const ContributorSerializer = require('../serializers/contributor'); +const paged = require('../utils/pagination'); class Contributor extends Base { - all() { + all(options = {}) { return this.functions.contributorsCount() - .then(count => { - let contributors = []; - - for (let id = 1; id <= count; id++) { - contributors.push(this.getById(id)); - } - - return RSVP.all(contributors); + .then((count) => { + let records = paged(count, options).map((id) => this.getById(id)); + return Promise.all(records); }); } diff --git a/lib/contracts/proposal.js b/lib/contracts/proposal.js index 3caccba..6252d84 100644 --- a/lib/contracts/proposal.js +++ b/lib/contracts/proposal.js @@ -1,19 +1,13 @@ -const RSVP = require('rsvp'); - -const ContributionSerializer = require('../serializers/contribution'); const Base = require('./base'); +const ContributionSerializer = require('../serializers/contribution'); +const paged = require('../utils/pagination'); class Proposal extends Base { - all() { + all(options = {}) { return this.functions.proposalsCount() - .then(count => { - let proposals = []; - - for (let id = 1; id <= count; id++) { - proposals.push(this.getById(id)); - } - - return RSVP.all(proposals); + .then((count) => { + let records = paged(count, options).map((id) => this.getById(id)); + return Promise.all(records); }); } diff --git a/lib/kredits.js b/lib/kredits.js index ef47795..fc1c168 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -1,5 +1,4 @@ const ethers = require('ethers'); -const RSVP = require('rsvp'); const Preflight = require('./utils/preflight'); @@ -57,7 +56,8 @@ class Kredits { ); }); }); - return RSVP.all(addressPromises).then(() => { return this }); + + return Promise.all(addressPromises).then(() => { return this }); }); } diff --git a/lib/utils/pagination.js b/lib/utils/pagination.js new file mode 100644 index 0000000..02c2f97 --- /dev/null +++ b/lib/utils/pagination.js @@ -0,0 +1,46 @@ +function pageNumber(number, size, recordCount) { + let numberOfPages = Math.ceil(recordCount / size); + + number = parseInt(number) || 1; + + // Ensure page number is in range + number = number < 1 ? 1 : number; + number = number > numberOfPages ? numberOfPages : number; + + return number; +} + +function buildIds(order, number, size, recordCount) { + let offset = size * (number - 1); + + let start; + let mapFunction; + + if (order === 'asc') { + start = 1 + offset; + mapFunction = (_, i) => start + i; + } else { + start = recordCount - offset; + mapFunction = (_, i) => start - i; + } + + // Ensure size is in range + let end = offset + size; + if (end > recordCount) { + let diff = end - recordCount; + size = size - diff; + } + + return Array.from({ length: size }, mapFunction); +} + +module.exports = function paged(recordCount, options = {}) { + let { order, page } = options; + order = order || 'desc'; + page = page || {}; + + let size = parseInt(page.size) || 25; + let number = pageNumber(page.number, size, recordCount); + + return buildIds(order, number, size, recordCount); +}; diff --git a/package.json b/package.json index 50f086c..e0d9909 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,6 @@ "ethers": "^4.0.27", "ipfs-http-client": "^30.1.1", "kosmos-schemas": "github:67P/kosmos-schemas#feature/contribution_date_time", - "rsvp": "^4.8.2", "tv4": "^1.3.0" }, "keywords": [