burst-queue
Advanced tools
Comparing version 0.1.1 to 0.1.2
// Generated by CoffeeScript 1.3.3 | ||
(function() { | ||
var BurstQueue, exports; | ||
var BurstQueue; | ||
BurstQueue = (function() { | ||
var _availableCalls, _counter, _maxItems, _queue; | ||
var _maxItems, _queue; | ||
_queue = new Array(); | ||
_counter = 0; | ||
_maxItems = 0; | ||
_availableCalls = 0; | ||
function BurstQueue(period, itemsPerPeriod) { | ||
if (itemsPerPeriod != null) { | ||
_availableCalls = _maxItems = itemsPerPeriod; | ||
} else { | ||
_availableCalls = _maxItems; | ||
} | ||
this.counter = 0; | ||
this.availableCalls = _maxItems = itemsPerPeriod != null ? itemsPerPeriod : _maxItems; | ||
setInterval(this.process, period); | ||
@@ -31,41 +24,32 @@ } | ||
fn = functions[_i]; | ||
if (_availableCalls > 0) { | ||
_availableCalls--; | ||
if (this.availableCalls > 0) { | ||
this.availableCalls--; | ||
fn(); | ||
} else { | ||
_queue.push({ | ||
id: _counter, | ||
id: this.counter, | ||
fn: fn | ||
}); | ||
} | ||
ids.push(_counter++); | ||
ids.push(this.counter++); | ||
} | ||
return ids; | ||
} else { | ||
if (_availableCalls > 0) { | ||
_availableCalls--; | ||
if (this.availableCalls > 0) { | ||
this.availableCalls--; | ||
functions(); | ||
} else { | ||
_queue.push({ | ||
id: _counter, | ||
id: this.counter, | ||
fn: functions | ||
}); | ||
} | ||
return _counter++; | ||
return this.counter++; | ||
} | ||
}; | ||
BurstQueue.prototype.availableCalls = function() { | ||
return _availableCalls; | ||
}; | ||
BurstQueue.prototype.clear = function() { | ||
_queue.length = 0; | ||
_availableCalls = _maxItems; | ||
}; | ||
BurstQueue.prototype.counter = function() { | ||
return _counter; | ||
}; | ||
BurstQueue.prototype.enqueued = function() { | ||
@@ -78,3 +62,3 @@ return _queue.length; | ||
if (_queue.length < 1) { | ||
_availableCalls = _maxItems; | ||
this.availableCalls = _maxItems; | ||
return; | ||
@@ -90,3 +74,3 @@ } | ||
if (index < _maxItems) { | ||
return _availableCalls = _maxItems - (index + 1); | ||
return this.availableCalls = _maxItems - (index + 1); | ||
} | ||
@@ -117,8 +101,4 @@ }; | ||
exports = module.exports; | ||
module.exports = BurstQueue; | ||
exports.createQueue = function(period, functionsPerPeriod) { | ||
return new BurstQueue(period, functionsPerPeriod); | ||
}; | ||
}).call(this); |
{ | ||
"author": "Mike Maelzer <mmaelzer@gmail.com>", | ||
"name": "burst-queue", | ||
"description": "a queue that executes callbacks in predefined bursts", | ||
"version": "0.1.1", | ||
"description": "a queue that executes functions in predefined bursts", | ||
"version": "0.1.2", | ||
"repository": { | ||
@@ -10,3 +10,3 @@ "type": "git", | ||
}, | ||
"main": "index", | ||
"main": "./index", | ||
"engines": { | ||
@@ -13,0 +13,0 @@ "node": "*" |
@@ -0,1 +1,2 @@ | ||
[![build status](https://secure.travis-ci.org/mmaelzer/burst-queue.png)](http://travis-ci.org/mmaelzer/burst-queue) | ||
burst-queue | ||
@@ -5,33 +6,74 @@ =========== | ||
Methods | ||
------- | ||
### createQueue(period, [functionsPerPeriod]) ### | ||
__Returns__: a burst-queue that will execute passed in functions every `period` (milliseconds). The optional `functionsPerPeriod` value denotes a maximum number of functions to be executed at each interval. | ||
### Objects ### | ||
**Queue(period, [callsPerPeriod])** | ||
Returns a burst-queue that will execute passed-in functions every `period` (milliseconds). The optional `callsPerPeriod` value denotes a maximum number of functions to be executed at each interval. | ||
### add(fn) / add([fn1, fn2, ...]) ### | ||
Adds functions to the queue. The `add` method takes both single functions or arrays of functions. | ||
var Queue = require('burst-queue'); | ||
// Call the oldest 20 functions every minute | ||
var queue = new Queue(60*1000, 20); | ||
__Returns__: the queue's internal id(s) of the passed in functions. The internal ids can be later used to `remove` items from the queue. If an array of functions is passed to `add`, an array of ids is returned in the same order as the functions that were passed in. | ||
--------------- | ||
### availableCalls ### | ||
Only valid when `functionsPerPeriod` is defined. | ||
### Methods ### | ||
__Returns__: the number of calls available in the current period. In other words, any function passed in when `availableCalls()` returns a value greater than 0 will be executed immediately. | ||
**add(fn) / add([fn1, fn2, ...])** | ||
Adds functions to the queue. The `add` method takes either a single function or an array of functions. Returns the queue's internal id(s) of the passed in functions. The internal ids can be later used to `remove` items from the queue. If an array of functions is passed to `add`, an array of ids is returned in the same order as the functions that were passed in. | ||
### clear ### | ||
Clears the queue of all functions. Sets the `availableCalls` back to the original value of `functionsPerPeriod` that was specified on creation of the queue. If no value was given for `functionsPerPeriod`, `availableCalls` remains at 0. | ||
var hello = function() { console.log("hello") }; | ||
var comma = function() { console.log(",") }; | ||
var space = function() { console.log(" ") }; | ||
var is_it_me = function() { console.log("is it me") }; | ||
var youre_looking_for = function() { console.log("you're looking for") }; | ||
__Returns__: nothing | ||
queue.add(hello); | ||
queue.add([ comma, space, is_it_me, space, youre_looking_for ]); | ||
### counter ### | ||
__Returns__: the current value of the counter used for generating the queue's internal ids for functions. This value increments by one each time a function is added to the queue. | ||
//>hello, is it me you're looking for | ||
### enqueued ### | ||
__Returns__: the current count of items in the queue. | ||
**clear()** | ||
Clears the queue of all functions. | ||
### remove(id) ### | ||
Takes an id returned from an `add` call and removes that function from the queue, if it is still in the queue. | ||
var queue = new Queue(60*1000, 2); | ||
queue.add([ hello, comma, comma, space ); | ||
// the first 2 functions are called instantly since we must first | ||
// reach the calls per period limit before enqueueing functions | ||
console.log(queue.enqueued()); | ||
//> 2 | ||
queue.clear(); | ||
console.log(queue.enqueued()); | ||
//> 0 | ||
__Returns__: a bool that designates whether the `remove` call successfully found and removed the function from the queue. If a function no longer exists in the queue when `remove` is called on its id, `remove` returns `false`. | ||
**enqueued()** | ||
Returns the current count of items in the queue. | ||
**remove(id)** | ||
Takes an id returned from an `add` call and removes that function from the queue, if it is still in the queue. Returns a bool that designates whether the `remove` call successfully found and removed the function from the queue. If a function no longer exists in the queue when `remove` is called on its id, `remove` returns `false`. | ||
var tryRemove = function(id) { | ||
if (queue.remove(id)) { | ||
console.log("success!"); | ||
} else { | ||
console.log("fail!"); | ||
} | ||
} | ||
var helloId = queue.add(hello); | ||
tryRemove(helloId); | ||
//> success! | ||
tryRemove(helloId); | ||
//> fail! | ||
---------------- | ||
### Properties ### | ||
**counter** | ||
Returns the current value of the counter used for generating the queue's internal ids for functions. This value increments by one each time a function is added to the queue. | ||
**availableCalls** | ||
Only valid when `callsPerPeriod` is defined. Returns the number of calls available in the current period. Any function added to the queue in when `availableCalls` returns a value greater than 0 will be executed immediately. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
11245
10
78
87