Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 1.69 KB

README.md

File metadata and controls

56 lines (41 loc) · 1.69 KB

cumulative-raf-schd

A throttle function based on raf-schd. With cumulative-raf-schd, it is possible to accumulate function arguments before execution. It is compatible with raf-schd.

NPM

Install

npm install --save cumulative-raf-schd

Usage

import cumulativeRafSchd from "cumulative-raf-schd";

const expensiveFn = (...args) => {
  //...
  console.log(args);
};

const schedule = cumulativeRafSchd(expensiveFn, [true, false, true]);

schedule(1, 10, 100);
schedule(2, 20, 200);
schedule(3, 30, 300);
// animation frame fires
// => [6, 30, 600]

The second argument tells which args must accumulate and which must not. Examples:

cumulativeRafSchd(func, true); //accumulate 1st argument
cumulativeRafSchd(func, [true]); //accumulate 1st argument
cumulativeRafSchd(func, [false, true]); //accumulate 2nd argument
cumulativeRafSchd(func, [0, 1]); //accumulate 2nd argument
cumulativeRafSchd(func, [, 1]); //accumulate 2nd argument
cumulativeRafSchd(func, [, , 1, , 1]); //accumulate 3rd and 5th arguments
cumulativeRafSchd(func, [0, (acc, val) => acc * val]); //accumulate 2nd argument with given function
cumulativeRafSchd(func, [(acc, val) => acc - val, (acc, val) => acc * val]); //accumulate 1st and 2nd arguments with given functions

Default accumulate function is addition

(acc, val) => acc + val;

Default accumulate function can be set with 3rd argument

// 3rd and 5th argument will be accumulated with given default accumulate function
cumulativeRafSchd(func, [, , 1, , 1], (acc, val) => acc * val);