Runs an interval with variable timeout. Useful for loops adapting to heavy load or other kind of situations.
npm install elastic-loopRuns function in loop, mutating timeout depending on stress. Returns instance containing end function.
function: Interval function. (Function)stress: Function returning bool. (Function)options: optional, but must be an Object if specified, containing zero or more of the following properties:timeout: Default interval timeout in milliseconds. (Number; default:1000)modifier: Timeout multiplier on stress. (Float; default:1.20)min: Min stress multiplier. (Number|Float; default:1)max: Max stress multiplier. (Number|Float; default:0:infinite)
constloop=require('elastic-loop')constbusy=require('node-busy')constmonitor=busy()// @cycle: current cycle fingerprintfunctionrun(cycle){// → @cycle: { timeout: 1000, stress: 1 }}functionstress(){// returns true if overloaded, false otherwisereturnmonitor.blocked}constcycle=loop(run,stress)// cycle.end()See the License file.