Browse Source

Moved repo to Github, changed license to MIT, updated Readme, bumped version to 0.1.2

pull/1/merge
Brett Langdon 14 years ago
commit
ae95b72756
8 changed files with 261 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +8
    -0
      LICENSE
  3. +68
    -0
      README.md
  4. +29
    -0
      example/interval.js
  5. +25
    -0
      example/limit.js
  6. +31
    -0
      example/random.js
  7. +65
    -0
      index.js
  8. +32
    -0
      package.json

+ 3
- 0
.gitignore View File

@ -0,0 +1,3 @@
node_modules
*#*#
*~

+ 8
- 0
LICENSE View File

@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2012 Brett Langdon
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.

+ 68
- 0
README.md View File

@ -0,0 +1,68 @@
#Continuous
##About
Continuous is an event based utility used for setTimeout and setInterval.
It is useful when trying to have code that runs at random or continuous intervals.
##How to Install:
```
npm install continuous
```
##How to Use:
```
var continuous = require('continuous');
//tell it to run 5 times
//every 1 to 3 seconds
var options = {
limit: 5,
minTime: 1000,
maxTime: 3000,
callback: function(){
console.log('I have run');
return Math.round(new Date().getTime()/1000.0);
},
random: true
};
var run = new continuous( options );
run.on('started', function(){
console.log('It has begun to run');
});
run.on('stopped', function(){
console.log('All Done');
});
run.on('complete', function(count, result){
console.log('I have run ' + count + ' times');
console.log('The return of callback is:');
console.dir(result);
});
//start it
run.start();
//force it to stop after 5 seconds
setTimeout( function(){
run.stop();
}, 5000 );
```
##Options:
* `limit`: Number - optional, default: -1(forever)
* `time`: Number - milliseconds between runs (non-random only), default: 1000
* `minTime`: Number - min allowed milliseconds between runs (random only), default: 0
* `maxTime`: Number - max allowed milliseconds between runs (random only), default: 1000
* `random`: Boolean - whether or not it should run randomly between minTime and maxTime, default: false
* `callback`: Function - function to run continuously
##Methods:
* `start()` - start running
* `stop()` - stop running

+ 29
- 0
example/interval.js View File

@ -0,0 +1,29 @@
var continuous = require('../');
var run = new continuous({
limit: 20,
time: 500,
callback: function(){
return Math.round(new Date().getTime()/1000.0);
}
});
run.on('started', function(){
console.log('running');
});
run.on('stopped', function(){
console.log('stopped');
});
run.on('complete', function(count,result){
console.log('The count is: ' + count);
console.log('The time is: ' + result);
});
run.start();
setTimeout( function(){
run.stop();
}, 5000 );

+ 25
- 0
example/limit.js View File

@ -0,0 +1,25 @@
var continuous = require('../');
var run = new continuous({
limit: 5,
time: 500,
callback: function(){
return Math.round(new Date().getTime()/1000.0);
}
});
run.on('started', function(){
console.log('running');
});
run.on('stopped', function(){
console.log('stopped');
});
run.on('complete', function(count,result){
console.log('The count is: ' + count);
console.log('The time is: ' + result);
});
run.start();

+ 31
- 0
example/random.js View File

@ -0,0 +1,31 @@
var continuous = require('../');
var run = new continuous({
limit: 20,
minTime: 200,
maxTime: 1500,
random: true,
callback: function(){
return Math.round(new Date().getTime()/1000.0);
}
});
run.on('started', function(){
console.log('running');
});
run.on('stopped', function(){
console.log('stopped');
});
run.on('complete', function(count,result){
console.log('The count is: ' + count);
console.log('The time is: ' + result);
});
run.start();
setTimeout( function(){
run.stop();
}, 5000 );

+ 65
- 0
index.js View File

@ -0,0 +1,65 @@
var event = require('eventemitter2').EventEmitter2;
var util = require('util');
var continuous = function( options ){
options = (options)?options:{};
this.limit = (options.limit)?parseInt(options.limit):-1;
this.time = (options.time)?parseInt(options.time):1000;
this.minTime = (options.minTime)?parseInt(options.minTime):0;
this.maxTime = (options.maxTime)?parseInt(options.maxTime):1000;
this.random = (options.random)?options.random==true:false;
this.callback = (typeof(options.callback)=='function')?options.callback:function(){};
this._interval = null;
this._timeout = null;
this._count = 0;
this._running = false;
};
util.inherits(continuous, event);
continuous.prototype.stop = function(){
this._running = false;
clearInterval(this._interval);
clearTimeout(this._timeout);
this.emit('stopped');
};
continuous.prototype.start = function(){
this._count = 0;
this._running = true;
this._run();
this.emit('started');
};
continuous.prototype._run = function(){
if( !this._running ) return;
var self = this;
if( this.random ){
this._timeout = setTimeout( function(){
var result = self.callback();
++self._count;
self.emit('complete', self._count, result);
if( self._count == self.limit ) self.stop();
self._run();
}, self.randomNumber(self.minTime,self.maxTime) );
} else{
this._interval = setInterval( function(){
var result = self.callback();
++self._count;
self.emit('complete', self._count, result);
if( self._count == self.limit ) self.stop();
}, self.time );
}
};
continuous.prototype.randomNumber = function(min,max){
return Math.floor( Math.random()*(max-min)+1 ) + min;
};
module.exports = continuous;

+ 32
- 0
package.json View File

@ -0,0 +1,32 @@
{
"name": "continuous"
, "version": "0.1.2"
, "description": "Event based utility for setTimeout and setInterval"
, "homepage": "http://www.blangdon.com/"
, "keywords": [
"continuous"
, "timeout"
, "interval"
, "continuously"
, "event"
]
, "author": "Brett Langdon <brett@blangdon.com>"
, "contributors": [
{ "name": "Brett Langdon", "email": "brett@blangdon.com" }
]
, "licenses": [
{
"type": "MIT"
, "url": "http://www.opensource.org/licenses/MIT"
}
]
, "repository":{
"type": "git"
, "url": "git://github.com/brettlangdon/Continuous.git"
}
, "dependencies": {
"eventemitter2": ">=0.4.9"
}
, "main": "index.js"
, "engines": { "node": ">= 0.6.0" }
}

Loading…
Cancel
Save