react-periodical
Version:
React wrapper component for calling function periodically.
254 lines (221 loc) • 7.16 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['exports', 'react'], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require('react'));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.react);
global.index = mod.exports;
}
})(this, function (exports, _react) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
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;
};
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
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;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
function _possibleConstructorReturn(self, call) {
if (!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) {
if (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
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}
var Periodical = function (_Component) {
_inherits(Periodical, _Component);
function Periodical(props) {
_classCallCheck(this, Periodical);
var _this = _possibleConstructorReturn(this, (Periodical.__proto__ || Object.getPrototypeOf(Periodical)).call(this, props));
_this.runCount = 0;
return _this;
}
_createClass(Periodical, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.start();
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(_ref) {
var f = _ref.f;
var period = _ref.period;
var limit = _ref.limit;
var paused = _ref.paused;
return this.props.f !== f || this.props.period !== period || this.props.limit !== limit || this.props.paused !== paused;
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate(_ref2) {
var prevPaused = _ref2.paused;
if (prevPaused && !this.props.paused) {
this.resume();
}
if (!prevPaused && this.props.paused) {
this.pause();
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
clearInterval(this.runner);
this.runner = null;
}
}, {
key: 'run',
value: function run() {
if (this.runner || this.runCount === 0) {
this.isRunnable() ? this.props.f() : this.pause();
this.runCount = this.runCount + 1;
}
}
}, {
key: 'start',
value: function start() {
var _this2 = this;
this.run();
this.runner = setInterval(function () {
return _this2.run();
}, this.props.period);
}
}, {
key: 'pause',
value: function pause() {
clearInterval(this.runner);
this.runner = null;
var limit = this.props.limit;
if (limit && limit === this.runCount) {
this.forceUpdate();
}
}
}, {
key: 'resume',
value: function resume() {
var _this3 = this;
this.runner = setInterval(function () {
return _this3.run();
}, this.props.period);
}
}, {
key: 'reset',
value: function reset() {
this.pause();
this.runCount = 0;
}
}, {
key: 'isRunnable',
value: function isRunnable() {
var _props = this.props;
var limit = _props.limit;
var paused = _props.paused;
return !paused && (!limit || this.runCount < limit);
}
}, {
key: 'getChildren',
value: function getChildren(childProps) {
var _props2 = this.props;
var components = _props2.components;
var children = _props2.children;
if (components) {
if (Array.isArray(components) && components.length) {
return components.map(function (Child, i) {
return _react2.default.createElement(Child, _extends({ key: i }, childProps));
});
}
var Child = components;
return _react2.default.createElement(Child, childProps);
}
if (children) {
return _react2.default.Children.map(children, function (child) {
return _react2.default.cloneElement(child, childProps);
});
}
return null;
}
}, {
key: 'render',
value: function render() {
var _this4 = this;
var limit = this.props.limit;
var childProps = { runOnceHandler: function runOnceHandler() {
return _this4.props.f();
} };
if (limit && limit === this.runCount) {
childProps['restartHandler'] = function () {
_this4.reset();
_this4.start();
};
}
return _react2.default.createElement(
'div',
null,
this.getChildren(childProps)
);
}
}]);
return Periodical;
}(_react.Component);
Periodical.propTypes = {
f: _react.PropTypes.func.isRequired,
period: _react.PropTypes.number,
limit: _react.PropTypes.number,
paused: _react.PropTypes.bool,
components: _react.PropTypes.oneOfType([_react.PropTypes.arrayOf(_react.PropTypes.oneOfType([_react.PropTypes.func, _react.PropTypes.string])), _react.PropTypes.oneOfType([_react.PropTypes.func, _react.PropTypes.string])])
};
Periodical.defaultProps = {
period: 1000,
paused: false
};
exports.default = Periodical;
});