0.0.3 • Published 11 years ago
fnrate v0.0.3
FnRate 
A bit of code that will limit the iteration of a function to a specific rate.
Install
npm intall fnrate
-or-
npm install ben-bradley/fnrate
Example
var fnrate = require('fnrate');
fnrate({
rate: '10/sec',
duration: '5 min', // will run for 5 minutes & stop
times: 100, // `callback` will iterate 100 times
max: 5, // [optional] there will only be 5 pending `callback`s at any time
// `callback` will iterate for whichever is less: times or duration
callback: function(next) {
// do stuff
next(); // next([error, [result]]);
},
done: function(err, results) {
if (err) console.log(err);
else console.log('all done!');
}
});Options
rate- The rate at which you wish to initiate yourcallback. Expects a string:n/periodwherenis a number andperiodis one of'sec''second''min''minutes''hr''hour'
duration- The length of time that you want the itaration to last. Expects a string:n periodwherenis the number ofperiods.times- The number of times that you want thecallbackto run.max- The maximum number of pendingcallbacks at any time.callback- The function to iterate. Provides anextfunction that must be called when complete to trigger the next iteration. If thenextfunction is called with arguments, it will pass them through to thedone;done- The function to call when thedurationortimesare complete.
Flow control
I found that I wanted to be able to pause and resume the callbacks from within the callback so as of version 0.0.3, you can:
options.max- Integer - Setting this option will cause fnrate to pause if there are ever more than this number of pending callbacks.pause()- Callingthis.pause()within thecallbackwill also pause the execution of other callbacks untilthis.resume()is called.resume()- Clears thepause()state and resumes execution of thecallback.
Test
npm test
Versions
- 0.0.3 - More finely-tuned version of flow-control
- 0.0.2 - First atempt at flow-control
- 0.0.1 - Initial drop & npm publish