UNPKG

dash-core-components

Version:

Core component suite for Dash

114 lines (110 loc) 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireWildcard(require("react")); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } // eslint-disable-line no-unused-vars /** * A component that repeatedly increments a counter `n_intervals` * with a fixed time delay between each increment. * Interval is good for triggering a component on a recurring basis. * The time delay is set with the property "interval" in milliseconds. */ class Interval extends _react.Component { constructor(props) { super(props); this.intervalId = null; this.reportInterval = this.reportInterval.bind(this); this.handleTimer = this.handleTimer.bind(this); } handleTimer(props) { // Check if timer should stop or shouldn't even start if (props.max_intervals === 0 || props.disabled || props.n_intervals >= props.max_intervals && props.max_intervals !== -1) { // stop existing timer if (this.intervalId) { this.clearTimer(); } // and don't start a timer return; } // keep the existing timer running if (this.intervalId) { return; } // it hasn't started yet (& it should start) this.intervalId = window.setInterval(this.reportInterval, props.interval); } resetTimer(props) { this.clearTimer(); this.handleTimer(props); } clearTimer() { window.clearInterval(this.intervalId); this.intervalId = null; } reportInterval() { var _this$props = this.props, setProps = _this$props.setProps, n_intervals = _this$props.n_intervals; setProps({ n_intervals: n_intervals + 1 }); } componentDidMount() { this.handleTimer(this.props); } UNSAFE_componentWillReceiveProps(nextProps) { if (nextProps.interval !== this.props.interval) { this.resetTimer(nextProps); } else { this.handleTimer(nextProps); } } componentWillUnmount() { this.clearTimer(); } render() { return null; } } exports.default = Interval; Interval.propTypes = { /** * The ID of this component, used to identify dash components * in callbacks. The ID needs to be unique across all of the * components in an app. */ id: _propTypes.default.string, /** * This component will increment the counter `n_intervals` every * `interval` milliseconds */ interval: _propTypes.default.number, /** * If True, the counter will no longer update */ disabled: _propTypes.default.bool, /** * Number of times the interval has passed */ n_intervals: _propTypes.default.number, /** * Number of times the interval will be fired. * If -1, then the interval has no limit (the default) * and if 0 then the interval stops running. */ max_intervals: _propTypes.default.number, /** * Dash assigned callback */ setProps: _propTypes.default.func }; Interval.defaultProps = { interval: 1000, n_intervals: 0, max_intervals: -1 };