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.1 to 0.0.4

31

lib/seq-queue.js

@@ -35,6 +35,7 @@ var EventEmitter = require('events').EventEmitter;

* @param fn new request
* @param ontimeout callback when task timeout
* @param timeout timeout for current request. take the global timeout if this is invalid
* @returns true or false
*/
pro.push = function(fn, timeout) {
pro.push = function(fn, ontimeout, timeout) {
if(this.status !== exp.STATUS_IDLE && this.status !== exp.STATUS_BUSY) {

@@ -48,3 +49,3 @@ //ignore invalid status

}
this.queue.push({fn: fn, timeout: timeout});
this.queue.push({fn: fn, ontimeout: ontimeout, timeout: timeout});

@@ -105,2 +106,3 @@ if(this.status === exp.STATUS_IDLE) {

this.status = exp.STATUS_IDLE;
this.curId++; //modify curId to invalidate timeout task
} else {

@@ -115,7 +117,4 @@ this.status = exp.STATUS_DRAINED;

task.id = ++this.curId;
task.fn({
done: function() {
self._next(task.id);
}
});
//start timer
var timeout = task.timeout > 0 ? task.timeout : this.timeout;

@@ -128,3 +127,21 @@ timeout = timeout > 0 ? timeout : DEFAULT_TIMEOUT;

self.emit('timeout', task);
if(!!task.ontimeout) {
task.ontimeout();
}
}, timeout);
try {
task.fn({
done: function() {
var res = task.id === self.curId
self._next(task.id);
return res;
}
});
} catch(err) {
console.log('[seq-queue] task exception:' + err.message);
this.emit('error', err, task);
this._next(this.curId);
}
};

@@ -131,0 +148,0 @@

{
"name": "seq-queue",
"author": "changchang <changchang005@gmail.com>",
"version": "0.0.1",
"version": "0.0.4",
"description": "A simple tool to keep requests to be executed in order.",

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

@@ -27,2 +27,4 @@ #seq-queue

task.done();
}, function() {
console.log('task timeout');
}, 1000);

@@ -41,9 +43,10 @@ });

###seqqueue.createQueue(timeout)
Create a new queue instance. A gloabal timeout value in ms for the new instance can be set by `timeout` parameter or use the default timeout (3ms) by no parameter.
Create a new queue instance. A global timeout value in ms for the new instance can be set by `timeout` parameter or use the default timeout (3s) by no parameter.
###queue.push(fn, timeout)
###queue.push(fn, ontimeout, timeout)
Add a task into the queue instance.
####Arguments
+ fn(task) - The function that describes the content of task and would be invoke by queue. `fn` takes a arguemnt task and we *must* call task.done() to tell queue current task has finished.
+ timeout - Timeout in ms for `fn`. If specified, it would overwrite the gloabal timeout that set by `createQueue` for `fn`.
+ fn(task) - The function that describes the content of task and would be invoke by queue. `fn` takes a arguemnt task and we *must* call task.done() to tell queue current task has finished.
+ ontimeout() - Callback for task timeout.
+ timeout - Timeout in ms for `fn`. If specified, it would overwrite the global timeout that set by `createQueue` for `fn`.

@@ -59,2 +62,4 @@ ###queue.close(force)

If current task not invoke task.done() within the timeout ms, a timeout event would be emit. totask.fn and totask.timeout is the `fn` and `timeout` arguments that passed by `queue.push(2)`.
###'error'(err, task)
If the task function (not callbacks) throws an uncaught error, queue would emit an error event and passes the err and task informations by event callback arguments.
###'closed'

@@ -61,0 +66,0 @@ Emit when the close(false) is invoked.

@@ -49,2 +49,19 @@ var should = require('should');

});
it('should ok if the task call done() directly', function(done) {
var queue = SeqQueue.createQueue();
var taskCount = 0;
queue.push(function(task) {
taskCount++;
task.done();
});
queue.push(function(task) {
taskCount++;
task.done();
});
setTimeout(function() {
taskCount.should.equal(2);
done();
}, 500);
});
});

@@ -115,2 +132,3 @@

var timeoutCount = 0;
var onTimeoutCount = 0;
//add timeout listener

@@ -126,2 +144,4 @@ queue.on('timeout', function(task) {

//no task.done() invoke to cause a timeout
}, function() {
onTimeoutCount++;
}).should.be.true;

@@ -138,2 +158,3 @@

timeoutCount.should.be.equal(1);
onTimeoutCount.should.be.equal(1);
done();

@@ -143,2 +164,36 @@ }, 4000); //default timeout is 3s

it('should return false when invoke task.done() if task has already timeout', function(done) {
var queue = SeqQueue.createQueue();
var executedTaskCount = 0;
var timeoutCount = 0;
var timeout = 1000;
//add timeout listener
queue.on('timeout', function(task) {
task.should.be.a('object');
task.fn.should.be.a('function');
timeoutCount++;
});
queue.push(function(task) {
executedTaskCount++;
task.done().should.be.true;
}).should.be.true;
queue.push(function(task) {
//sleep to make a timeout
setTimeout(function() {
executedTaskCount++;
task.done().should.be.false;
}, timeout + 1000);
}, null, timeout).should.be.true;
setTimeout(function() {
//wait all task finish
executedTaskCount.should.be.equal(2);
timeoutCount.should.be.equal(1);
done();
}, 4000);
});
it('should never timeout after close forcefully', function(done) {

@@ -194,6 +249,36 @@ var queue = SeqQueue.createQueue(timeout);

//no task.done() invoke to cause a timeout
}, localTimeout).should.be.true;
}, null, localTimeout).should.be.true;
var start = Date.now();
});
});
describe('#error', function() {
it('should emit an error event and invoke next task when a task throws an event', 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++;
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);
});
});
});
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