Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor paging.

  • Loading branch information...
commit c762f1d5a0b32497153ef99b7b7a47a5f514a6f0 1 parent 57caf94
@chjj authored
Showing with 72 additions and 67 deletions.
  1. +72 −67 lib/charged.js
View
139 lib/charged.js
@@ -947,73 +947,7 @@ Charged.prototype.get = function(path, query, callback, format) {
}
if (query.max) {
- var max = query.max
- , concurrency = 1
- , pending = 0
- , out = [];
-
- delete query.max;
-
- if (query.concurrency) {
- concurrency = query.concurrency;
- delete query.concurrency;
- }
-
- if (concurrency > Charged.concurrencyLimit) {
- concurrency = Charged.concurrencyLimit;
- }
-
- if (concurrency <= 0) {
- concurrency = 1;
- }
-
- query.page = 0;
- query.per_page = max;
-
- var cb = function(err) {
- // If `out` is maxed, execute callback even
- // if there are still requests pending.
- if (pending && out.length < max) {
- return;
- }
-
- if (cb.done) return;
- cb.done = true;
-
- return err
- ? callback(err)
- : callback(null, out);
- };
-
- var page = function() {
- query.page++;
- pending++;
- return self.get(path, query, function(err, results) {
- pending--;
-
- if (err) return cb(err);
-
- if (!results.length) {
- return cb();
- }
-
- out.push.apply(out, results);
-
- if (out.length >= max) {
- out = out.slice(0, max);
- return cb();
- }
-
- if (!pending) return next();
- }, format);
- };
-
- var next = function() {
- var i = concurrency;
- while (i--) page();
- };
-
- return next();
+ return this._page(path, query, callback, format);
}
if (query.concurrency) {
@@ -1041,6 +975,77 @@ Charged.prototype.get = function(path, query, callback, format) {
return this.request(options, callback);
};
+Charged.prototype._page = function(path, query, callback, format) {
+ var self = this
+ , max = query.max
+ , concurrency = 1
+ , pending = 0
+ , out = [];
+
+ delete query.max;
+
+ if (query.concurrency) {
+ concurrency = query.concurrency;
+ delete query.concurrency;
+ }
+
+ if (concurrency > Charged.concurrencyLimit) {
+ concurrency = Charged.concurrencyLimit;
+ }
+
+ if (concurrency <= 0) {
+ concurrency = 1;
+ }
+
+ query.page = 0;
+ query.per_page = max;
+
+ function cb(err) {
+ // If `out` is maxed, execute callback even
+ // if there are still requests pending.
+ if (pending && out.length < max) {
+ return;
+ }
+
+ if (cb.done) return;
+ cb.done = true;
+
+ return err
+ ? callback(err)
+ : callback(null, out);
+ }
+
+ function page() {
+ query.page++;
+ pending++;
+ return self.get(path, query, function(err, results) {
+ pending--;
+
+ if (err) return cb(err);
+
+ if (!results.length) {
+ return cb();
+ }
+
+ out.push.apply(out, results);
+
+ if (out.length >= max) {
+ out = out.slice(0, max);
+ return cb();
+ }
+
+ if (!pending) return next();
+ }, format);
+ }
+
+ function next() {
+ var i = concurrency;
+ while (i--) page();
+ }
+
+ return next();
+};
+
Charged.prototype.post = function(path, body, callback, format) {
if (typeof body === 'function') {
format = callback;
Please sign in to comment.
Something went wrong with that request. Please try again.