react-user-idle
Version:
This project is inspired by and forked from https://github.com/ReactTraining/react-idle .
133 lines (106 loc) • 4.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
var _react = require('react');
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _throttle = require('lodash/throttle');
var _throttle2 = _interopRequireDefault(_throttle);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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 watchEvents = ['mousemove', 'mousedown', 'keydown', 'touchstart', 'scroll'];
/**
* two common situation for usage
* 1) preload resources when user idle
* 2) break the entire page when user has no action anymore
*
* in case 1, custom render is the preferred.
* in case 2, usually modal children is preferred
*/
var Idle = function (_Component) {
_inherits(Idle, _Component);
function Idle() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Idle);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Idle.__proto__ || Object.getPrototypeOf(Idle)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
idle: false
// a tiemout timer to set idle state
}, _this.timer = null, _this.handler = null, _this.startTimer = function () {
_this.timer = setTimeout(function () {
var breakOnIdle = _this.props.children;
if (breakOnIdle) {
_this.removeEvents();
}
_this.setState({ idle: true });
}, _this.props.timeout * 1000);
}, _this.resetTimer = function () {
if (_this.state.idle) {
_this.setState({ idle: false });
}
clearTimeout(_this.timer);
_this.startTimer();
}, _this.attachEvents = function () {
watchEvents.forEach(function (event) {
window.addEventListener(event, _this.handler, true);
});
}, _this.removeEvents = function () {
clearTimeout(_this.timer);
watchEvents.forEach(function (event) {
window.removeEventListener(event, _this.handler, true);
});
}, _temp), _possibleConstructorReturn(_this, _ret);
}
// a handler to reset the timer according to user action
_createClass(Idle, [{
key: 'componentDidMount',
value: function componentDidMount() {
var throttleTime = this.props.throttle * 1000;
this.handler = throttleTime ? (0, _throttle2.default)(this.resetTimer, throttleTime) : this.resetTimer;
this.attachEvents();
this.startTimer();
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.removeEvents();
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps, prevState) {
if (this.state.idle !== prevState.idle) {
this.props.onChange && this.props.onChange(this.state.idle);
}
}
}, {
key: 'render',
value: function render() {
if (typeof this.props.children === 'function') {
return this.props.children(this.state.idle);
} else {
return this.state.idle ? this.props.children : null;
}
}
}]);
return Idle;
}(_react.Component);
Idle.propTypes = {
timeout: _propTypes2.default.number,
throttle: _propTypes2.default.number,
onChange: _propTypes2.default.func,
children: _propTypes2.default.any
};
Idle.defaultProps = {
// seconds
timeout: 60 * 15,
// seconds, must less than timeout
throttle: 5
};
exports.default = Idle;