src/modules/guiEngine/recloop.js
/*
   * Add a function to reoccur at opts.interval intervals.
   * NOTE: jitter will also be applied to delays to keep jobs from all
   * happening at once and spiking the cpu.
   */
Add a function to reoccur at opts.interval intervals.
NOTE: jitter will also be applied to delays to keep jobs from all
happening at once and spiking the cpu.
Add a function to reoccur at opts . interval intervals . NOTE : jitter will also be applied to delays to keep jobs from all happening at once and spiking the cpu .
function add(opts, fn) {
    if (!fn && typeof opts === 'function') {
      fn = opts;
      opts = undefined;
    }
    opts = opts || {};
    opts.interval = opts.interval || 15 * 1000;
    opts.jitter = opts.jitter || 0.2;
    var stopFlag = false;
    function rec() {
      return Promise.fromNode(function(cb) {
        var duration = applyJitter(opts.jitter, opts.interval);
        setTimeout(function() {
          return $q.try(fn)
          .catch(function(err) {
            return errorHandler(err);
          })
          .nodeify(cb);
        }, duration);
      })
      .then(function() {
        if (!stopFlag) {
          return rec();
        }
      });
    }
    var prm = rec();
    var api = {
      fn: fn,
      stop: function() {
        stopFlag = true;
        return prm;
      }
    };
    fns.push(api);
    return api;
  }
function add ( opts , fn ) { if ( ! fn && typeof opts === 'function' ) { fn = opts ; opts = undefined ; } opts = opts || { } ; opts . interval = opts . interval || 15 * 1000 ; opts . jitter = opts . jitter || 0.2 ; var stopFlag = false ; function rec ( ) { return Promise . fromNode ( function ( cb ) { var duration = applyJitter ( opts . jitter , opts . interval ) ; setTimeout ( function ( ) { return $q . try ( fn ) . catch ( function ( err ) { return errorHandler ( err ) ; } ) . nodeify ( cb ) ; } , duration ) ; } ) . then ( function ( ) { if ( ! stopFlag ) { return rec ( ) ; } } ) ; } var prm = rec ( ) ; var api = { fn : fn , stop : function ( ) { stopFlag = true ; return prm ; } } ; fns . push ( api ) ; return api ; }
Add a function to reoccur at opts.interval intervals.
Add a function to reoccur at opts . interval intervals .
// Argument shuffling. // Default options. // Flag for stopping repeating interval. // Recursive function. // Start a new promise. // Apply jitter to interval to get next timeout duration. // Init a timeout. // Run handler function. // Handle errors. // Resolve promise. // Recurse unless top flag has been set. // Hold onto promise returned by recursive function so caller can wait on it. // Return micro api so called can turn it off and wait for it to finish. // Tell recursive function to stop. // Return promise. // Add api to list of apis. // Return api.