Last active
August 29, 2015 14:00
-
-
Save jb55/b0ad2711d025490caae3 to your computer and use it in GitHub Desktop.
quick hack for fast pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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