# Code coverage report for lib/runloop.js

## Statements: 87.88% (29 / 33) Branches: 81.25% (13 / 16) Functions: 66.67% (4 / 6) Lines: 86.67% (26 / 30) Ignored: none

All files » lib/ » runloop.js

    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
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    801

    1

    1

    1

    1
    1
    1

    1

    93
    58

    58

    58
    35
    35

    35
    35
    35

    81
    79
    79

    79
    58
    58
    58

    58

    1var protoclass = require("protoclass");

    var rAF = (global.requestAnimationFrame     ||
              global.webkitRequestAnimationFrame ||
              global.mozRequestAnimationFrame    ||
              process.nextTick).bind(global);

    Iif (process.browser) {
      var defaultTick = function(next) {
        rAF(next);
      };
    } else {
      var defaultTick = function(next) {
        next();
      };
    }

    /**
     */

    function RunLoop(options) {
      this._animationQueue = [];
      this.tick = options.tick || defaultTick;
    }

    protoclass(RunLoop, {

      /**
       * Runs animatable object on requestAnimationFrame. This gets
       * called whenever the UI state changes.
       *
       * @method animate
       * @param {Object} animatable object. Must have `update()`
       */

      deferOnce: function(context) {

        if (context.__running) return;
        context.__running = true;

        // push on the animatable object
        this._animationQueue.push(context);

        // if animating, don't continue
        if (this._requestingFrame) return;
        this._requestingFrame = true;
        var self = this;

        // run the animation frame, and callback all the animatable objects
        this.tick(function() {
          self.runNow();
          self._requestingFrame = false;
        });
      },

      /**
       */

      runNow: function() {
        if (!this._requestingFrame) return;
        var queue = this._animationQueue;
        this._animationQueue = [];

        // queue.length is important here, because animate() can be
        // called again immediately after an update
        for (var i = 0; i < queue.length; i++) {
          var item = queue[i];
          item.__running = false;
          item.update();

          // check for anymore animations - need to run
          // them in order
          Iif (this._animationQueue.length) {
            this.runNow();
          }
        }
      }
    });

    module.exports = RunLoop;