Giter VIP home page Giter VIP logo

fnrate's Introduction

FnRate Build Status

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

Examples

Basic 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!');
  }
});

Variable rate example

var fnrate = require('fnrate');
var i = 0;

fnrate({
  rate: function () {
    if (i < 25) return '10/sec';
    else if (i < 50) return '20/sec';
    else if (i < 75) return '5/sec';
    else return '15/sec';
  },
  times: 100,
  max: 10,
  callback: function (next) {
    i += 1;
    console.log(i);
    next(null, i);
  },
  done: function (err, results) {
    console.log('all done!');
    done();
  }
});

Options

  • rate - The rate at which you wish to initiate your callback. Expects a string: n/period where n is a number and period is one of
    • 'sec'
    • 'second'
    • 'min'
    • 'minutes'
    • 'hr'
    • 'hour'
  • duration - The length of time that you want the itaration to last. Expects a string: n period where n is the number of periods.
  • times - The number of times that you want the callback to run.
  • max - The maximum number of pending callbacks at any time.
  • callback - The function to iterate. Provides a next function that must be called when complete to trigger the next iteration. If the next function is called with arguments, it will pass them through to the done;
  • done - The function to call when the duration or times are 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() - Calling this.pause() within the callback will also pause the execution of other callbacks until this.resume() is called.
  • resume() - Clears the pause() state and resumes execution of the callback.

Test

npm test

-or-

mocha -R spec

Versions

  • 0.0.5 - Made .rate able to accept a function to modify rates on the fly!
  • 0.0.4 - Added code to catch race conditions
  • 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

fnrate's People

Watchers

 avatar  avatar

fnrate's Issues

make .rate a function that returns a variable rate

Make it possible to change the rate based on a function.

var fnrate = require('fnrate');

fnrate({
  rate: function() {
    var now = new Date();
    if (now.getHours() > 10)
      return '10/sec';
    else
      return '5/sec';
  },
  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!');
  }
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.