It has 3 porpuses:
- Start using github and contribute to open source.
- Learn how to publish something to npm. (TODO)
- Become something usefull?. (TODO)
Essentially it's a queue where you can push functions to be processed at a certain interval. Actually the functions are not called periodically but you can enqueue many functions and they will be executed in order at a certain rate.
scheduler = new Scheduler(1000); //call functions every 1sec
scheduler.enqueue(function(){
console.log(new Date());
});
scheduler.enqueue(function(){
console.log(new Date());
});
//Now with arguments
scheduler.enqueue([
function(arg1,arg2){
console.log('First argument is:' + arg1);
console.log('Second argument is:' + arg2);
},
2,'hi!'])
scheduler.start()
Imagine you must use a certain API for many things, but the limit is imposed to the whole API, not just per endpoint.
- Rate limit is 1 call/sec
var scheduler = new Scheduler(1000); // 1 call each 1000ms
var api_manager = new RestrictedApiManager({user:'me',pass:'secret');
//This could be rate limited
api_manager.getData(function(data){
console.log("Data is": data);
});
api_manager.getMoreData(function(data2){
console.log("Data is": data2);
});
api_manager.getOtherData(function(data3){
console.log("Data is": data3);
});
//This won't be rate limited but it is awful
var rate_limit_delay = 1000;
api_manager.getData(function(data){
console.log("Data is": data);
setTimeout(function(){
api_manager.getMoreData(function(data2){
console.log("Data is": data2);
setTimeout( function(){
api_manager.getOtherData(function(data3){
console.log("Data is": data3);
}); //getOtherData
},rate_limit_delay);
}); //getMoreData
},rate_limit_delay);
}); //getData
//This looks better
var scheduler = new Scheduler(1000); // 1 call per 1000ms
scheduler.start();
printData = function(data){
console.log("Data is:");
console.log(data);
}
scheduler.enqueue(api_manager.getData,printData);
scheduler.enqueue(api_manager.getMoreData,printData);
scheduler.enqueue(api_manager.getOtherData,printData);
This is just the first structure.
- Tests
- Validations. What happens if you enqueue something that is not a function?
- Error handling. What happens if your function crashes?
- Functinality to get statistics:
- When will a function be executed?
- How many elements are in the queue?
- More funcionalities
- Priorities
- Remove enqueued function
- Change execution interval
- Change execution interval
- NPM publish