stanza-extend
Version:
Modern XMPP in the browser, with a JSON API
45 lines (44 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPager = exports.ResultSetPager = void 0;
class ResultSetPager {
constructor(opts) {
var _a, _b, _c;
this.cursor = { first: opts.before, last: opts.after };
this.query = opts.query;
this.direction = (_a = opts.direction) !== null && _a !== void 0 ? _a : 'forward';
this.reverse = (_b = opts.reverse) !== null && _b !== void 0 ? _b : this.direction === 'backward';
this.pageSize = (_c = opts.pageSize) !== null && _c !== void 0 ? _c : 20;
}
async *[Symbol.asyncIterator]() {
let currentResults = [];
do {
currentResults = await this.fetchPage();
for (const item of currentResults) {
yield item;
}
} while (currentResults.length > 0);
}
async size() {
const { paging } = await this.query({ max: 0 });
return paging.count;
}
async fetchPage() {
var _a;
const { results, paging } = await this.query({
before: this.direction === 'backward' ? (_a = this.cursor.first) !== null && _a !== void 0 ? _a : '' : undefined,
after: this.direction === 'forward' ? this.cursor.last : undefined,
max: this.pageSize
});
this.cursor = paging;
if (this.reverse) {
results.reverse();
}
return results;
}
}
exports.ResultSetPager = ResultSetPager;
function createPager(opts) {
return new ResultSetPager(opts);
}
exports.createPager = createPager;