Skip to content

Instantly share code, notes, and snippets.

@jb55
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jb55/b0ad2711d025490caae3 to your computer and use it in GitHub Desktop.
Save jb55/b0ad2711d025490caae3 to your computer and use it in GitHub Desktop.
quick hack for fast pagination
SoundCloudy.prototype.batch = function(offset, pageSize, n) {
n = n == null? 2 : n;
var self = this;
function* next() {
for (let i of range(n)) {
yield self.run({ offset: offset })
offset += pageSize;
}
}
return function*(){ return yield array(next()); }
}
SoundCloudy.prototype.runAll = function*() {
var self = this;
var results = [];
self.limit(self.limit() == null? 50 : self.limit())
var pageSize = self.limit();
var offset = self.offset() || 0;
var concurrency = this.concurrency() || 3;
var next = this.batch(offset, pageSize, concurrency)
var batch;
while(!donePaging(batch, pageSize)) {
batch = yield next();
for (let responses of arr(batch)) {
for (let response of arr(responses)) {
results.push(response);
}
}
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment