All files / core polyfills.js

23.08% Statements 6/26
31.25% Branches 5/16
0% Functions 0/6
23.08% Lines 6/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                      4x   4x   4x             4x                           4x                   4x                                                  
/* eslint-disable no-extend-native, prefer-rest-params */
 
/**
 * http://paulirish.com/2011/requestanimationframe-for-smart-animating/
 * http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
 * requestAnimationFrame polyfill by Erik Möller.
 * fixes from Paul Irish and Tino Zijdel
 * MIT license
 */
let x;
 
let lastTime = 0;
 
const vendors = ["ms", "moz", "webkit", "o"];
 
for (x = 0; x < vendors.length && !global.requestAnimationFrame; x++) {
  global.requestAnimationFrame = global[`${vendors[x]}RequestAnimationFrame`];
  global.cancelAnimationFrame =
    global[`${vendors[x]}CancelAnimationFrame`] ||
    global[`${vendors[x]}CancelRequestAnimationFrame`];
}
 
Iif (!global.requestAnimationFrame)
  global.requestAnimationFrame = function requestAnimationFrame(callback) {
    const currTime = new Date().getTime();
 
    const timeToCall = Math.max(0, 16 - (currTime - lastTime));
 
    const id = global.setTimeout(function cb() {
      callback(currTime + timeToCall);
    }, timeToCall);
 
    lastTime = currTime + timeToCall;
    return id;
  };
 
Iif (!global.cancelAnimationFrame)
  global.cancelAnimationFrame = function cancelAnimationFrame(id) {
    clearTimeout(id);
  };
 
/**
 * Function.prototype.bind polyfill found on MDN.
 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Compatibility
 * Public domain
 */
Iif (!Function.prototype.bind)
  Function.prototype.bind = function bind(oThis) {
    if (typeof this !== "function")
      // Closest thing possible to the ECMAScript 5 internal IsCallable
      // function:
      throw new TypeError(
        "Function.prototype.bind - what is trying to be bound is not callable"
      );
 
    const aArgs = Array.prototype.slice.call(arguments, 1);
    const fToBind = this;
 
    function fNOP() {}
    function fBound() {
      return fToBind.apply(
        this instanceof fNOP && oThis ? this : oThis,
        aArgs.concat(Array.prototype.slice.call(arguments))
      );
    }
 
    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();
 
    return fBound;
  };