Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

seq-queue

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

seq-queue - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

.jshintrc

135

lib/seq-queue.js

@@ -10,5 +10,7 @@ var EventEmitter = require('events').EventEmitter;

/**
* instance a new queue
*
* @param timeout a global timeout for new queue
* Instance a new queue
*
* @param {Number} timeout a global timeout for new queue
* @class
* @constructor
*/

@@ -18,3 +20,3 @@ var SeqQueue = function(timeout) {

if(!!timeout && timeout > 0) {
if(timeout && timeout > 0) {
this.timeout = timeout;

@@ -25,3 +27,3 @@ } else {

this.status = exp.STATUS_IDLE;
this.status = SeqQueueManager.STATUS_IDLE;
this.curId = INIT_ID;

@@ -32,6 +34,4 @@ this.queue = [];

var pro = SeqQueue.prototype;
/**
* add a task into queue.
* Add a task into queue.
*

@@ -43,4 +43,4 @@ * @param fn new request

*/
pro.push = function(fn, ontimeout, timeout) {
if(this.status !== exp.STATUS_IDLE && this.status !== exp.STATUS_BUSY) {
SeqQueue.prototype.push = function(fn, ontimeout, timeout) {
if(this.status !== SeqQueueManager.STATUS_IDLE && this.status !== SeqQueueManager.STATUS_BUSY) {
//ignore invalid status

@@ -55,4 +55,4 @@ return false;

if(this.status === exp.STATUS_IDLE) {
this.status = exp.STATUS_BUSY;
if(this.status === SeqQueueManager.STATUS_IDLE) {
this.status = SeqQueueManager.STATUS_BUSY;
var self = this;

@@ -67,8 +67,8 @@ process.nextTick(function() {

/**
* close queue
* Close queue
*
* @param force if true will close the queue immediately else will execute the rest task in queue
* @param {Boolean} force if true will close the queue immediately else will execute the rest task in queue
*/
pro.close = function(force) {
if(this.status !== exp.STATUS_IDLE && this.status !== exp.STATUS_BUSY) {
SeqQueue.prototype.close = function(force) {
if(this.status !== SeqQueueManager.STATUS_IDLE && this.status !== SeqQueueManager.STATUS_BUSY) {
//ignore invalid status

@@ -78,5 +78,5 @@ return;

if(!!force) {
this.status = exp.STATUS_DRAINED;
if(!!this.timerId) {
if(force) {
this.status = SeqQueueManager.STATUS_DRAINED;
if(this.timerId) {
clearTimeout(this.timerId);

@@ -87,3 +87,3 @@ this.timerId = undefined;

} else {
this.status = exp.STATUS_CLOSED;
this.status = SeqQueueManager.STATUS_CLOSED;
this.emit(EVENT_CLOSED);

@@ -94,8 +94,9 @@ }

/**
* invoke next task
* Invoke next task
*
* @param tid last executed task id
* @param {String|Number} tid last executed task id
* @api private
*/
pro._next = function(tid) {
if(tid !== this.curId || this.status !== exp.STATUS_BUSY && this.status !== exp.STATUS_CLOSED) {
SeqQueue.prototype._next = function(tid) {
if(tid !== this.curId || this.status !== SeqQueueManager.STATUS_BUSY && this.status !== SeqQueueManager.STATUS_CLOSED) {
//ignore invalid next call

@@ -105,3 +106,3 @@ return;

if(!!this.timerId) {
if(this.timerId) {
clearTimeout(this.timerId);

@@ -113,7 +114,7 @@ this.timerId = undefined;

if(!task) {
if(this.status === exp.STATUS_BUSY) {
this.status = exp.STATUS_IDLE;
if(this.status === SeqQueueManager.STATUS_BUSY) {
this.status = SeqQueueManager.STATUS_IDLE;
this.curId++; //modify curId to invalidate timeout task
} else {
this.status = exp.STATUS_DRAINED;
this.status = SeqQueueManager.STATUS_DRAINED;
this.emit(EVENT_DRAINED);

@@ -126,4 +127,3 @@ }

task.id = ++this.curId;
//start timer
var timeout = task.timeout > 0 ? task.timeout : this.timeout;

@@ -136,12 +136,14 @@ timeout = timeout > 0 ? timeout : DEFAULT_TIMEOUT;

self.emit('timeout', task);
if(!!task.ontimeout) {
if(task.ontimeout) {
task.ontimeout();
}
}, timeout);
try {
task.fn({
done: function() {
var res = task.id === self.curId
self._next(task.id);
var res = task.id === self.curId;
process.nextTick(function() {
self._next(task.id);
});
return res;

@@ -151,17 +153,62 @@ }

} catch(err) {
console.log('[seq-queue] task exception:' + err.message);
this.emit('error', err, task);
this._next(this.curId);
self.emit('error', err, task);
process.nextTick(function() {
self._next(task.id);
});
}
};
var exp = module.exports;
exp.STATUS_IDLE = 0; //status: idle
exp.STATUS_BUSY = 1; //status: busy
exp.STATUS_CLOSED = 2; //status: closed, no new request but will process the rest task in queue
exp.STATUS_DRAINED = 3; //status: drained, no new request and no task to execute
/**
* Queue manager.
*
* @module
*/
var SeqQueueManager = module.exports;
exp.createQueue = function(timeout) {
/**
* Queue status: idle, welcome new tasks
*
* @const
* @type {Number}
* @memberOf SeqQueueManager
*/
SeqQueueManager.STATUS_IDLE = 0;
/**
* Queue status: busy, queue is working for some tasks now
*
* @const
* @type {Number}
* @memberOf SeqQueueManager
*/
SeqQueueManager.STATUS_BUSY = 1;
/**
* Queue status: closed, queue has closed and would not receive task any more
* and is processing the remaining tasks now.
*
* @const
* @type {Number}
* @memberOf SeqQueueManager
*/
SeqQueueManager.STATUS_CLOSED = 2;
/**
* Queue status: drained, queue is ready to be destroy
*
* @const
* @type {Number}
* @memberOf SeqQueueManager
*/
SeqQueueManager.STATUS_DRAINED = 3;
/**
* Create Sequence queue
*
* @param {Number} timeout a global timeout for the new queue instance
* @return {Object} new queue instance
* @memberOf SeqQueueManager
*/
SeqQueueManager.createQueue = function(timeout) {
return new SeqQueue(timeout);
};
{
"name": "seq-queue",
"author": "changchang <changchang005@gmail.com>",
"version": "0.0.4",
"version": "0.0.5",
"description": "A simple tool to keep requests to be executed in order.",

@@ -6,0 +6,0 @@ "homepage": "https://github.com/changchang/seq-queue",

@@ -1,2 +0,4 @@

#seq-queue
seq-queue - queue to keep request process in sequence
=====================================================
Seq-queue is simple tool to keep requests to be executed in order.

@@ -12,2 +14,4 @@

* Tags: node.js
##Installation

@@ -24,17 +28,23 @@ ```

queue.push(function(task) {
setTimeout(function() {
console.log('hello ');
task.done();
}, function() {
console.log('task timeout');
}, 1000);
});
queue.push(
function(task) {
setTimeout(function() {
console.log('hello ');
task.done();
}, 500);
},
function() {
console.log('task timeout');
},
1000
);
queue.push(function(task) {
setTimeout(function() {
console.log('world~');
task.done();
}, 500);
});
queue.push(
function(task) {
setTimeout(function() {
console.log('world~');
task.done();
}, 500);
}
);
```

@@ -67,2 +77,2 @@

###'drained'
Emit when close(true) is invoked or all tasks left have finished in closed status.
Emit when close(true) is invoked or all tasks left have finished in closed status.

@@ -277,3 +277,32 @@ var should = require('should');

});
it('should be ok when task throw a error after done was invoked', function(done) {
var queue = SeqQueue.createQueue();
var errorCount = 0;
var taskCount = 0;
//add timeout listener
queue.on('error', function(err, task) {
errorCount++;
should.exist(err);
should.exist(task);
});
queue.push(function(task) {
taskCount++;
task.done();
throw new Error('some error');
}).should.be.true;
queue.push(function(task) {
taskCount++;
task.done();
});
setTimeout(function() {
taskCount.should.equal(2);
errorCount.should.equal(1);
done();
}, 500);
});
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc