Permalink
Browse files

added a request queue that serializes API calls

  • Loading branch information...
1 parent 5c08242 commit 1a94ee369420b2f343e145635a4c2250302badcb @pdfcrowd committed Apr 28, 2011
Showing with 37 additions and 5 deletions.
  1. +34 −3 lib/pdfcrowd.js
  2. +3 −2 test/client_test.js
View
@@ -17,20 +17,20 @@ var Pdfcrowd = function(username, apikey) {
Pdfcrowd.prototype.convertHtml = function(html, callbacks, options) {
- convertInternal(this, html, '/api/pdf/convert/html/', callbacks, options);
+ requestQueue.addRequest([this, html, '/api/pdf/convert/html/', callbacks, options]);
}
Pdfcrowd.prototype.convertURI = function(uri, callbacks, options) {
- convertInternal(this, uri, '/api/pdf/convert/uri/', callbacks, options);
+ requestQueue.addRequest([this, uri, '/api/pdf/convert/uri/', callbacks, options]);
}
Pdfcrowd.prototype.convertFile = function(fname, callbacks, options) {
var that = this;
fs.readFile(fname, function(err, data) {
if (err) throw err;
- convertInternal(that, data.toString(), '/api/pdf/convert/html/', callbacks, options);
+ requestQueue.addRequest([that, data.toString(), '/api/pdf/convert/html/', callbacks, options]);
});
}
@@ -66,10 +66,12 @@ var convertInternal = function(that, src, endpoint, callbacks, options) {
if (res.statusCode < 300) {
res.on('end', function() {
callbacks.end();
+ requestQueue.requestDone();
});
res.on('error', function(exc) {
callbacks.error(exc.toString());
+ requestQueue.requestDone();
});
callbacks.pdf(res);
@@ -81,19 +83,48 @@ var convertInternal = function(that, src, endpoint, callbacks, options) {
});
res.on('end', function() {
callbacks.error(err.join(''), res.statusCode);
+ requestQueue.requestDone();
});
}
});
req.on('error', function(res) {
callbacks.error(res.toString(), res.statusCode);
+ requestQueue.requestDone();
});
req.write(postData);
req.end();
}
+var requestQueue = {
+ init: function() {
+ this.queue = [];
+ this.working = 0;
+ },
+
+ addRequest: function(req) {
+ this.queue.push(req);
+ this.processRequest();
+ },
+
+ requestDone: function() {
+ this.working = 0;
+ this.processRequest();
+ },
+
+ processRequest: function() {
+ if (this.queue.length > 0 && !this.working) {
+ this.working = 1;
+ var args = this.queue.shift();
+ convertInternal.apply(null, args);
+ }
+ }
+};
+requestQueue.init();
+
+
var clone_object = function(obj) {
var prop, cloned = {};
View
@@ -6,6 +6,7 @@ credentials = require('./config').Credentials;
function saveToFile(fname) {
return {
pdf: function(rstream) {
+ console.log("start");
wstream = fs.createWriteStream(fname);
rstream.pipe(wstream);
},
@@ -17,6 +18,6 @@ function saveToFile(fname) {
myPdfcrowd = new Pdfcrowd(credentials.username,
credentials.apikey);
-//myPdfcrowd.convertHtml("raw code", saveToFile("html.pdf"))
-//myPdfcrowd.convertURI("http://example.com", saveToFile("url.pdf"))
+myPdfcrowd.convertHtml("raw code", saveToFile("html.pdf"))
+myPdfcrowd.convertURI("http://example.com", saveToFile("url.pdf"))
myPdfcrowd.convertFile("sample.html", saveToFile("file.pdf"))

0 comments on commit 1a94ee3

Please sign in to comment.