Comparing version 0.3.0 to 0.4.0
35
index.js
@@ -1,5 +0,10 @@ | ||
var add, create, q, setInterval, setLimit, t, tryNext; | ||
var add, create, defaults, q, setDefaultInterval, setDefaultLimit, setInterval, setLimit, t, tryNext; | ||
q = {}; | ||
defaults = { | ||
interval: 1000, | ||
limit: 1 | ||
}; | ||
t = function() { | ||
@@ -10,8 +15,2 @@ return new Date().getTime(); | ||
create = function(id, interval, limit) { | ||
if (interval == null) { | ||
interval = 1000; | ||
} | ||
if (limit == null) { | ||
limit = 1; | ||
} | ||
if (q[id] != null) { | ||
@@ -21,6 +20,6 @@ return false; | ||
if (!isFinite(interval)) { | ||
interval = 1000; | ||
interval = defaults.interval; | ||
} | ||
if (!isFinite(limit)) { | ||
limit = 1; | ||
limit = defaults.interval; | ||
} | ||
@@ -62,2 +61,16 @@ if (interval < 1) { | ||
setDefaultInterval = function(interval) { | ||
if (!isFinite(interval || interval < 1)) { | ||
return; | ||
} | ||
return defaults.interval = interval; | ||
}; | ||
setDefaultLimit = function(limit) { | ||
if (!isFinite(limit || limit < 1)) { | ||
return; | ||
} | ||
return defaults.limit = limit; | ||
}; | ||
add = function(fn, cb, id) { | ||
@@ -138,3 +151,5 @@ if (id == null) { | ||
setInterval: setInterval, | ||
setLimit: setLimit | ||
setLimit: setLimit, | ||
setDefaultInterval: setDefaultInterval, | ||
setDefaultLimit: setDefaultLimit | ||
}; | ||
@@ -141,0 +156,0 @@ |
{ | ||
"name": "multiqueue", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A simple Node.JS function queuing module with multiple queues", | ||
"keywords": [ | ||
"queue", | ||
"queues", | ||
"multiple", | ||
"async", | ||
"timer" | ||
], | ||
"main": "index.js", | ||
@@ -6,0 +13,0 @@ "scripts": { |
# Multiqueue | ||
A simple Node.JS function queuing module with multiple queues | ||
[](https://www.npmjs.com/package/multiqueue) | ||
[](https://david-dm.org/woubuc/multiqueue) | ||
[](https://github.com/woubuc/multiqueue/issues) | ||
## What it does | ||
@@ -8,12 +12,18 @@ This module allows you to have multiple queues with functions that will be processed one at a time at a set interval. Each queue operates independently from the others. | ||
## Basic Usage | ||
Install the module through npm | ||
``` | ||
npm install multiqueue --save | ||
``` | ||
Require the module in your code | ||
``` | ||
```Javascript | ||
var multiq = require('multiqueue'); | ||
``` | ||
Add a function to the queue | ||
``` | ||
```Javascript | ||
multiq.add(function(callback){ | ||
@@ -34,2 +44,20 @@ var a = 1 + 1; // Or you could, you know, do something useful instead | ||
You can add a custom queue easily by using the `create` method. | ||
```Javascript | ||
multiq.create('myQueue'); | ||
``` | ||
This will create a new queue called `myQueue`. You can then use this queue by adding the queue ID as an additional parameter to the multiqueue functions. | ||
```Javascript | ||
multiq.add(function(callback){ | ||
var a = 1 + 1; // Or you could, you know, do something useful instead | ||
callback(false, a); | ||
}, function(err, data){ | ||
console.log(data); // 2 | ||
}, 'myQueue'); | ||
``` | ||
## Error handling & retrying | ||
@@ -46,3 +74,3 @@ If something goes wrong, Multiqueue can try to run your function again. This may be useful if you are doing stuff that can fail sometimes, but succeed others (e.g. HTTP requests). The default queue does not retry, but when you create a custom queue you can set the retry limit to pretty much whatever you want. | ||
### Create | ||
``` | ||
```Javascript | ||
multiqueue.create(queueID [, interval][, limit]); | ||
@@ -61,3 +89,3 @@ ``` | ||
### Add | ||
``` | ||
```Javascript | ||
multiqueue.add(function, callback [, queueID]); | ||
@@ -75,4 +103,6 @@ ``` | ||
### Set Interval | ||
``` | ||
### Queue Properties | ||
#### Set Interval | ||
```Javascript | ||
multiqueue.setInterval(interval [, queueID]); | ||
@@ -89,4 +119,4 @@ ``` | ||
### Set Limit | ||
``` | ||
#### Set Limit | ||
```Javascript | ||
multiqueue.setInterval(limit [, queueID]); | ||
@@ -103,4 +133,30 @@ ``` | ||
### Default Values | ||
#### Set Default Interval | ||
```Javascript | ||
multiqueue.setDefaultInterval(interval); | ||
``` | ||
This sets the default interval for new queues | ||
Argument | Type | Description | ||
---------|--------|------------- | ||
interval | Number | Interval in milliseconds between each function in the queue | ||
#### Set Default Limit | ||
```Javascript | ||
multiqueue.setDefaultLimit(limit); | ||
``` | ||
This sets the default limit for new queues | ||
Argument | Type | Description | ||
---------|--------|------------- | ||
limit | Number | How many times to retry a failing function before returning an error | ||
### Queues | ||
``` | ||
```Javascript | ||
multiqueue.queues | ||
@@ -113,3 +169,3 @@ ``` | ||
``` | ||
```Javascript | ||
{ | ||
@@ -116,0 +172,0 @@ default: { |
@@ -1,3 +0,8 @@ | ||
var fs, handler, http, multiq, req; | ||
/* | ||
This is no automated test script if you were expecting that | ||
It is basically just a script that I use for testing new featues in my module | ||
*/ | ||
var fs, http, multiq; | ||
fs = require('fs'); | ||
@@ -9,41 +14,18 @@ | ||
multiq.create('myQ', 100, 1); | ||
multiq.setDefaultInterval(250); | ||
req = function(cb) { | ||
var r; | ||
console.log(this); | ||
r = http.get('http://jsonplaceholder.typicode.com/posts/1', function(res) { | ||
var data; | ||
data = ''; | ||
res.on('data', function(d) { | ||
return data += d; | ||
}); | ||
return res.on('end', function() { | ||
return cb(false, data); | ||
}); | ||
}); | ||
return r.on('error', cb); | ||
}; | ||
multiq.create('intervalTest'); | ||
handler = function(err, result) { | ||
if (err) { | ||
return console.error(err[err.length - 1]); | ||
} | ||
return console.log(result); | ||
}; | ||
console.log(multiq.queues); | ||
multiq.add(req, handler, 'myQ'); | ||
multiq.queues.intervalTest.add(function() { | ||
return console.log('yo1'); | ||
}); | ||
multiq.setInterval(500, 'myQ'); | ||
multiq.queues.intervalTest.add(function() { | ||
return console.log('yo2'); | ||
}); | ||
multiq.queues["default"].setLimit(20); | ||
console.log(multiq.queues); | ||
multiq.queues["default"].add(function(callback) { | ||
var a; | ||
a = 1 + 1; | ||
return callback(false, a); | ||
}, function(err, result) { | ||
return console.log(result); | ||
multiq.queues.intervalTest.add(function() { | ||
return console.log('yo3'); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
180
1
10523
5
182