all files / dist/ AnimationFrameComponent.js

86.11% Statements 62/72
63.83% Branches 30/47
94.12% Functions 16/17
100% Lines 41/41
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99      54×                           201×     201×     201×   197×   197× 11× 11×     197×                                                   10×                  
'use strict';
 
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { Iif (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
var React = require('react');
 
module.exports = function AnimationFrameComponent(InnerComponent, throttleMs) {
    return function (_React$Component) {
        _inherits(AnimatedComponent, _React$Component);
 
        function AnimatedComponent() {
            _classCallCheck(this, AnimatedComponent);
 
            var _this = _possibleConstructorReturn(this, (AnimatedComponent.__proto__ || Object.getPrototypeOf(AnimatedComponent)).call(this));
 
            _this.loop = _this.loop.bind(_this);
            _this.endAnimation = _this.endAnimation.bind(_this);
            _this.startAnimation = _this.startAnimation.bind(_this);
 
            _this.isActive = true;
            _this.rafId = 0;
            _this.lastInvocationMs = 0;
            return _this;
        }
 
        _createClass(AnimatedComponent, [{
            key: 'loop',
            value: function loop(time) {
                var lastInvocationMs = this.lastInvocationMs,
                    isActive = this.isActive;
 
                var isAnimatable = !!(this.innerComponent && this.innerComponent.onAnimationFrame);
 
                // Latter const is defensive check for React Native unmount (issues/#3)
                if (!isActive || !isAnimatable) return;
 
                var hasTimeElapsed = !throttleMs || time - lastInvocationMs >= throttleMs;
 
                if (hasTimeElapsed) {
                    this.lastInvocationMs = time;
                    this.innerComponent.onAnimationFrame(time, lastInvocationMs);
                }
 
                this.rafId = requestAnimationFrame(this.loop);
            }
        }, {
            key: 'endAnimation',
            value: function endAnimation() {
                cancelAnimationFrame(this.rafId);
 
                this.isActive = false;
            }
        }, {
            key: 'startAnimation',
            value: function startAnimation() {
                Eif (!this.isActive) {
                    this.isActive = true;
                    this.rafId = requestAnimationFrame(this.loop);
                }
            }
        }, {
            key: 'componentDidMount',
            value: function componentDidMount() {
                if (!this.innerComponent.onAnimationFrame) {
                    throw new Error('The component passed to AnimationFrameComponent does not implement onAnimationFrame');
                }
 
                this.rafId = requestAnimationFrame(this.loop);
            }
        }, {
            key: 'componentWillUnmount',
            value: function componentWillUnmount() {
                this.endAnimation();
            }
        }, {
            key: 'render',
            value: function render() {
                var _this2 = this;
 
                return React.createElement(InnerComponent, _extends({ ref: function ref(node) {
                        return _this2.innerComponent = node;
                    },
                    endAnimation: this.endAnimation,
                    startAnimation: this.startAnimation
                }, this.props));
            }
        }]);
 
        return AnimatedComponent;
    }(React.Component);
};