Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 0f5c0ccde32060f4d7fe4f65dbb5b6de07c008ee @rauchg rauchg committed Jul 30, 2012
Showing with 263 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +13 −0 Makefile
  3. +19 −0 package.json
  4. +107 −0 superagent-queue.js
  5. +123 −0 test/superagent-queue.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,13 @@
+
+TESTS = test/*.js
+REPORTER = dot
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --require should \
+ --reporter $(REPORTER) \
+ --timeout 10000 \
+ --growl \
+ $(TESTS)
+
+.PHONY: test
@@ -0,0 +1,19 @@
+{
+ "name": "superagent-queue",
+ "dependencies": "adds a `queue` fn to define request queues",
+ "version": "0.0.1",
+ "main": "./superagent-queue",
+ "dependencies": {
+ "superagent": "*"
+ },
+ "devDependencies": {
+ "mocha": "*",
+ "should": "*",
+ "express": "3.0.0rc1"
+ },
+ "component": {
+ "scripts": {
+ "superagent-queue": "./superagent-queue.js"
+ }
+ }
+}
@@ -0,0 +1,107 @@
+
+/**
+ * Module dependencies.
+ */
+
+var superagent = require('superagent')
+
+/**
+ * Module exports.
+ */
+
+module.exports = extend;
+
+/**
+ * Installs the `queue` extension to superagent.
+ *
+ * @param {Object} superagent module
+ * @api public
+ */
+
+function extend(sa){
+ var Request = sa.Request
+
+ /**
+ * Queues.
+ */
+
+ var queues = {}
+ , running = {}
+
+ /**
+ * `queue` method.
+ *
+ * @param {String} name of the queue
+ * @return {Request} for chaining
+ * @api public
+ */
+
+ Request.prototype.queue = function(name){
+ this.queueName = name;
+ return this;
+ };
+
+ /**
+ * Reference to original `end`.
+ */
+
+ var oldEnd = Request.prototype.end;
+
+ /**
+ * Checks for queued requests.
+ *
+ * @api private
+ */
+
+ function unqueue(name){
+ var item = queues[name].shift();
+
+ if (!item) {
+ delete queues[name];
+ return;
+ }
+
+ var obj = item[0]
+ , fn = item[1]
+
+ // immutable .length hack :\
+ if (fn.length == 1) {
+ oldEnd.call(obj, function(res){
+ fn(res);
+ unqueue(name);
+ });
+ } else {
+ oldEnd.call(obj, function(err, res){
+ fn(err, res);
+ unqueue(name);
+ });
+ }
+ }
+
+ /**
+ * Overrides `end` method to defer calls.
+ *
+ * @api private
+ */
+
+ Request.prototype.end = function(fn){
+ var queue = this.queueName;
+
+ if (queue) {
+ if (!queues[queue]) {
+ queues[queue] = [[this, fn]];
+ unqueue(queue);
+ } else {
+ queues[queue].push([this, fn]);
+ }
+ } else {
+ oldEnd.call(this, fn);
+ }
+ };
+};
+
+/**
+ * Extends the built-in dependency.
+ */
+
+extend(superagent);
@@ -0,0 +1,123 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express')
+ , superagent = require('superagent')
+
+/**
+ * Create express app.
+ */
+
+var app = express();
+app.listen(5005);
+
+/**
+ * Testing route.
+ */
+
+var concurrent = 0
+ , total = 0
+ , interval
+
+app.get('/', function(req, res){
+ concurrent++;
+ total++;
+
+ setTimeout(function(){
+ concurrent--;
+ res.send(200);
+ }, 500);
+});
+
+/**
+ * Queue requests
+ */
+
+describe('superagent-queue', function(){
+
+ it('adds the queue method', function(){
+ (undefined == superagent.Request.prototype.queue).should.be.true;
+ require('../superagent-queue');
+ (undefined == superagent.Request.prototype.queue).should.be.false;
+ });
+
+ it('queues', function(done){
+ var interval = setInterval(function(){
+ concurrent.should.equal(1);
+ }, 200);
+
+ superagent
+ .get('http://localhost:5005/')
+ .queue('woot')
+ .end(function(){
+ total.should.equal(1);
+ superagent.get('http://localhost:5005/')
+ .queue('woot')
+ .end(function(){
+ total.should.equal(2);
+ superagent.get('http://localhost:5005/')
+ .queue('woot')
+ .end(function(){
+ total.should.equal(3);
+ superagent.get('http://localhost:5005/')
+ .queue('woot')
+ .end(function(){
+ total.should.equal(4);
+ superagent.get('http://localhost:5005/')
+ .queue('woot')
+ .end(function(){
+ total.should.equal(5);
+ clearInterval(interval);
+ done();
+ })
+ })
+ })
+ })
+ });
+ });
+
+ it('works with multiple queues', function(done){
+ var total = 4;
+ setTimeout(function(){
+ concurrent.should.equal(2);
+ }, 200);
+ superagent.get('http://localhost:5005/')
+ .queue('woot')
+ .end(function(){
+ --total || done();
+ })
+ superagent.get('http://localhost:5005/')
+ .queue('woot')
+ .end(function(){
+ --total || done();
+ });
+ superagent.get('http://localhost:5005/')
+ .queue('woot 2')
+ .end(function(){
+ --total || done();
+ });
+ superagent.get('http://localhost:5005/')
+ .queue('woot 2')
+ .end(function(){
+ --total || done();
+ });
+ });
+
+ it('works with no queues', function(done){
+ var total = 2;
+ setTimeout(function(){
+ concurrent.should.equal(2);
+ }, 200);
+ superagent.get('http://localhost:5005/')
+ .end(function(){
+ --total || done();
+ })
+ superagent.get('http://localhost:5005/')
+ .end(function(){
+ --total || done();
+ })
+ });
+
+});

0 comments on commit 0f5c0cc

Please sign in to comment.