all files / mapbox-gl-draw/src/lib/ throttle.js

100% Statements 11/11
100% Branches 4/4
100% Functions 3/3
100% Lines 11/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30          120× 120× 108× 108×         907×   787×       120× 120× 120×       19×      
function throttle(fn, time, context) {
  let lock, args;
 
  function later () {
    // reset lock and call if queued
    lock = false;
    if (args) {
      wrapperFn.apply(context, args);
      args = false;
    }
  }
 
  function wrapperFn () {
    if (lock) {
      // called too soon, queue to call later
      args = arguments;
 
    } else {
      // lock until later then call
      lock = true;
      fn.apply(context, arguments);
      setTimeout(later, time);
    }
  }
 
  return wrapperFn;
}
 
module.exports = throttle;