Skip to content

Commit

Permalink
first commit version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
martinj committed Apr 19, 2012
0 parents commit 767b6a8
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.4
- 0.6
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
MIT License

Copyright (c) 2012 Martin Jonsson

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# AsyncQueue

[![Build Status](https://secure.travis-ci.org/martinj/node-async-queue.png)](http://travis-ci.org/martinj/node-async-queue)

## Installation

npm install async-queue

## Example

var AsyncQueue = require('async-queue');

new AsyncQueue(function (err, job) {
if (err) job.fail(err);
setTimeout(function () {
console.log('First job executed')
job.success();
}, 500);
}, function (err, job) {
console.log('Second job executed');
job.success();
}).run();

Output

First job executed
Second job executed

## Run Tests

npm install
npm test
112 changes: 112 additions & 0 deletions async-queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"use strict";
/**
* AsyncQueue is a simple FIFO queue to execute async jobs linear.
*
* Copyright (c) 2012, Martin Jonsson All Rights Reserved.
* Available via the MIT or new BSD license.
*
* http://github.com/martinj/node-async-queue
*
* Example
*
* new AsyncQueue(function (err, job) {
* if (err) job.fail(err);
* setTimeout(function () {
* console.log('First job executed')
* job.success();
* }, 500);
* }, function (err, job) {
* console.log('Second job executed');
* job.success();
* }).run();
*
*
*/
function AsyncQueue() {
this.jobs = Array.prototype.slice.call(arguments);
this.running = false;
}

AsyncQueue.prototype = {

/**
* Add job to the queue
*
* @param {Function} job
*/
add: function (job) {
if (job) {
if (arguments.length > 1) {
this.jobs = this.jobs.concat(Array.prototype.slice.call(arguments));
} else {
this.jobs.push(job);
}
}
return this;
},

/**
* Start running the queue
*
* @param {Function} [job] optional add a job to the queue at the same time
*/
run: function (job) {
this.add(job);

if (!this.running) {
this.start();
}
return this;
},

/**
* start executing the job in the queue
*
* @private
*/
start: function () {
this.running = true;
this.next(false);
},

/**
* Mark the current job as finished and call the next in queue
*
* @public
*/
success: function () {
this.next(false);
},

/**
* This marks the current as finished with an error then calls the next job in queue
*
* @public
* @param {Object} err
*/
fail: function (err) {
err = err || 'Job failed';
this.next(err);
},

/**
* Run the next job in queue
*
* @private
* @param {Object} err
*/
next: function (err) {
if (this.jobs.length) {
var job = this.jobs.shift();
try {
job(err, this);
} catch (e) {
this.next(e);
}
} else {
this.running = false;
}
}
};

module.exports = AsyncQueue;
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "async-queue",
"description": "The power of Nau",
"version": "0.1.0",
"repository": "git://github.com/martinj/node-async-queue",
"homepage": "https://github.com/martinj/node-async-queue",
"licenses": [{
"type": "MIT",
"url": "https://github.com/martinj/node-async-queue/raw/master/LICENSE"
}],
"keywords": ["queue", "async", "flow"],
"author": "Martin Jonsson <martin.jonsson@gmail.com>",
"engines": {
"node": ">= 0.4"
},
"dependencies": {},
"devDependencies": {
"mocha": "1.0.x",
"should": "*"
},
"main": "./async-queue",
"scripts" : {
"test" : "node_modules/.bin/mocha test/*.test.js"
}
}
75 changes: 75 additions & 0 deletions test/async-queue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
var should = require('should'),
AsyncQueue = require('../async-queue');

describe('AsyncQueue', function () {
describe('#AsyncQueue()', function () {
it('should init with jobs as params', function () {
var q = new AsyncQueue(function () {}, function () {});
q.jobs.should.have.lengthOf(2);
});
});

describe('#add()', function () {
it('should add job to existing queue', function () {
var q = new AsyncQueue();
q.add(function () {});
q.jobs.should.have.lengthOf(1);
});

it('should add multiple jobs to existing queue at the end', function () {
var q = new AsyncQueue(function () {}),
f1 = function () {},
f2 = function () {};

q.add(f1, f2);
q.jobs.should.have.lengthOf(3);
q.jobs[1].should.equal(f1);
q.jobs[2].should.equal(f2);
});
});

describe('#run()', function () {
it('should run jobs in order', function (done) {
var executed = {};

var q = new AsyncQueue(function (err, job) {
executed['job1'] = true;
job.success();
});

q.add(function (err, job) {
executed['job1'].should.be.true;
executed['job2'] = true;
job.success();
},
function () {
executed['job1'].should.be.true;
executed['job2'].should.be.true;
done();
}).run();
});

it('should not trigger start if running', function () {
var q = new AsyncQueue(function (err, job) {}).run();
q.running.should.be.true;
q.start = function () {
true.should.be.false;
};

q.run();
});
});

describe('#fail()', function () {
it('should forward errors to next job in queue', function (done) {
new AsyncQueue(
function (err, job) {
job.fail('an error');
},
function (err, job) {
err.should.equal('an error');
done();
}).run();
});
});
});

0 comments on commit 767b6a8

Please sign in to comment.