react-compound-timer
Advanced tools
Comparing version 1.0.8 to 1.0.10
@@ -1,358 +0,196 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
import React from 'react'; | ||
import TimerModel from '../../lib/models/TimerModel'; | ||
var TimerContext = React.createContext({ | ||
ms: 0, | ||
s: 0, | ||
m: 0, | ||
h: 0, | ||
d: 0, | ||
}); | ||
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; 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 _react2 = _interopRequireDefault(_react); | ||
var _propTypes = require('prop-types'); | ||
var _propTypes2 = _interopRequireDefault(_propTypes); | ||
var _TimerModel = require('../../lib/models/TimerModel'); | ||
var _TimerModel2 = _interopRequireDefault(_TimerModel); | ||
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 TimerContext = _react2.default.createContext(); | ||
var TimerValue = function (_React$Component) { | ||
_inherits(TimerValue, _React$Component); | ||
function TimerValue() { | ||
_classCallCheck(this, TimerValue); | ||
return _possibleConstructorReturn(this, (TimerValue.__proto__ || Object.getPrototypeOf(TimerValue)).apply(this, arguments)); | ||
} | ||
_createClass(TimerValue, [{ | ||
key: 'shouldComponentUpdate', | ||
value: function shouldComponentUpdate(nextProps) { | ||
var value = this.props.value; | ||
if (value !== nextProps.value) { | ||
return true; | ||
} | ||
return false; | ||
var TimerValue = /** @class */ (function (_super) { | ||
__extends(TimerValue, _super); | ||
function TimerValue() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
var value = this.props.value; | ||
return String(value) || null; | ||
TimerValue.prototype.shouldComponentUpdate = function (nextProps) { | ||
var value = this.props.value; | ||
if (value !== nextProps.value) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
TimerValue.prototype.render = function () { | ||
var value = this.props.value; | ||
return String(value) || null; | ||
}; | ||
return TimerValue; | ||
}(React.Component)); | ||
var Milliseconds = function () { return (React.createElement(Timer.Consumer, null, function (_a) { | ||
var ms = _a.ms; | ||
return React.createElement(TimerValue, { value: ms }); | ||
})); }; | ||
var Seconds = function () { return (React.createElement(Timer.Consumer, null, function (_a) { | ||
var s = _a.s; | ||
return React.createElement(TimerValue, { value: s }); | ||
})); }; | ||
var Minutes = function () { return (React.createElement(Timer.Consumer, null, function (_a) { | ||
var m = _a.m; | ||
return React.createElement(TimerValue, { value: m }); | ||
})); }; | ||
var Hours = function () { return (React.createElement(Timer.Consumer, null, function (_a) { | ||
var h = _a.h; | ||
return React.createElement(TimerValue, { value: h }); | ||
})); }; | ||
var Days = function () { return (React.createElement(Timer.Consumer, null, function (_a) { | ||
var d = _a.d; | ||
return React.createElement(TimerValue, { value: d }); | ||
})); }; | ||
var Timer = /** @class */ (function (_super) { | ||
__extends(Timer, _super); | ||
function Timer(props) { | ||
var _this = _super.call(this, props) || this; | ||
var _a = _this.props, initialTime = _a.initialTime, direction = _a.direction, timeToUpdate = _a.timeToUpdate, lastUnit = _a.lastUnit, checkpoints = _a.checkpoints; | ||
_this.timer = new TimerModel({ | ||
initialTime: initialTime, | ||
direction: direction, | ||
timeToUpdate: timeToUpdate, | ||
lastUnit: lastUnit, | ||
checkpoints: checkpoints, | ||
onChange: _this.setState.bind(_this), | ||
}); | ||
_this.state = __assign({}, _this.timer.timeParts, { timerState: 'INITED' }); | ||
_this.start = _this.start.bind(_this); | ||
_this.pause = _this.pause.bind(_this); | ||
_this.resume = _this.resume.bind(_this); | ||
_this.stop = _this.stop.bind(_this); | ||
_this.reset = _this.reset.bind(_this); | ||
_this.setTime = _this.setTime.bind(_this); | ||
_this.getTime = _this.getTime.bind(_this); | ||
_this.getTimerState = _this.getTimerState.bind(_this); | ||
_this.setDirection = _this.setDirection.bind(_this); | ||
_this.setCheckpoints = _this.setCheckpoints.bind(_this); | ||
return _this; | ||
} | ||
}]); | ||
return TimerValue; | ||
}(_react2.default.Component); | ||
var Milliseconds = function Milliseconds() { | ||
return _react2.default.createElement( | ||
Timer.Consumer, | ||
null, | ||
function (_ref) { | ||
var ms = _ref.ms; | ||
return _react2.default.createElement(TimerValue, { value: ms }); | ||
} | ||
); | ||
}; | ||
var Seconds = function Seconds() { | ||
return _react2.default.createElement( | ||
Timer.Consumer, | ||
null, | ||
function (_ref2) { | ||
var s = _ref2.s; | ||
return _react2.default.createElement(TimerValue, { value: s }); | ||
} | ||
); | ||
}; | ||
var Minutes = function Minutes() { | ||
return _react2.default.createElement( | ||
Timer.Consumer, | ||
null, | ||
function (_ref3) { | ||
var m = _ref3.m; | ||
return _react2.default.createElement(TimerValue, { value: m }); | ||
} | ||
); | ||
}; | ||
var Hours = function Hours() { | ||
return _react2.default.createElement( | ||
Timer.Consumer, | ||
null, | ||
function (_ref4) { | ||
var h = _ref4.h; | ||
return _react2.default.createElement(TimerValue, { value: h }); | ||
} | ||
); | ||
}; | ||
var Days = function Days() { | ||
return _react2.default.createElement( | ||
Timer.Consumer, | ||
null, | ||
function (_ref5) { | ||
var d = _ref5.d; | ||
return _react2.default.createElement(TimerValue, { value: d }); | ||
} | ||
); | ||
}; | ||
var Timer = function (_React$PureComponent) { | ||
_inherits(Timer, _React$PureComponent); | ||
function Timer(props) { | ||
_classCallCheck(this, Timer); | ||
var _this2 = _possibleConstructorReturn(this, (Timer.__proto__ || Object.getPrototypeOf(Timer)).call(this, props)); | ||
var _this2$props = _this2.props, | ||
initialTime = _this2$props.initialTime, | ||
direction = _this2$props.direction, | ||
timeToUpdate = _this2$props.timeToUpdate, | ||
lastUnit = _this2$props.lastUnit, | ||
checkpoints = _this2$props.checkpoints; | ||
_this2.timer = new _TimerModel2.default({ | ||
initialTime: initialTime, | ||
direction: direction, | ||
timeToUpdate: timeToUpdate, | ||
lastUnit: lastUnit, | ||
checkpoints: checkpoints, | ||
onChange: _this2.setState.bind(_this2) | ||
}); | ||
_this2.state = _extends({}, _this2.timer.timeParts); | ||
_this2.start = _this2.start.bind(_this2); | ||
_this2.pause = _this2.pause.bind(_this2); | ||
_this2.resume = _this2.resume.bind(_this2); | ||
_this2.stop = _this2.stop.bind(_this2); | ||
_this2.reset = _this2.reset.bind(_this2); | ||
_this2.setTime = _this2.setTime.bind(_this2); | ||
_this2.getTime = _this2.getTime.bind(_this2); | ||
_this2.getTimerState = _this2.getTimerState.bind(_this2); | ||
_this2.setDirection = _this2.setDirection.bind(_this2); | ||
_this2.setCheckpoints = _this2.setCheckpoints.bind(_this2); | ||
return _this2; | ||
} | ||
_createClass(Timer, [{ | ||
key: 'componentDidMount', | ||
value: function componentDidMount() { | ||
var startImmediately = this.props.startImmediately; | ||
startImmediately && this.timer.start(); | ||
} | ||
}, { | ||
key: 'componentWillUnmount', | ||
value: function componentWillUnmount() { | ||
this.timer.stop(); | ||
} | ||
}, { | ||
key: 'setTime', | ||
value: function setTime(time) { | ||
this.timer.setTime(time); | ||
} | ||
}, { | ||
key: 'getTime', | ||
value: function getTime() { | ||
return this.timer.getTime(); | ||
} | ||
}, { | ||
key: 'getTimerState', | ||
value: function getTimerState() { | ||
return this.timer.state; | ||
} | ||
}, { | ||
key: 'setDirection', | ||
value: function setDirection(direction) { | ||
this.timer.setDirection(direction); | ||
} | ||
}, { | ||
key: 'setCheckpoints', | ||
value: function setCheckpoints(checkpoints) { | ||
this.timer.setCheckpoints(checkpoints); | ||
} | ||
}, { | ||
key: 'start', | ||
value: function start() { | ||
this.timer.start(); | ||
this.props.onStart(); | ||
} | ||
}, { | ||
key: 'stop', | ||
value: function stop() { | ||
this.timer.stop(); | ||
this.props.onStop(); | ||
} | ||
}, { | ||
key: 'pause', | ||
value: function pause() { | ||
this.timer.pause(); | ||
this.props.onPause(); | ||
} | ||
}, { | ||
key: 'reset', | ||
value: function reset() { | ||
this.timer.reset(); | ||
this.props.onReset(); | ||
} | ||
}, { | ||
key: 'resume', | ||
value: function resume() { | ||
this.timer.resume(); | ||
this.props.onResume(); | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
var start = this.start, | ||
pause = this.pause, | ||
resume = this.resume, | ||
stop = this.stop, | ||
reset = this.reset, | ||
getTime = this.getTime, | ||
getTimerState = this.getTimerState, | ||
setTime = this.setTime, | ||
setDirection = this.setDirection, | ||
setCheckpoints = this.setCheckpoints; | ||
var _state = this.state, | ||
ms = _state.ms, | ||
s = _state.s, | ||
m = _state.m, | ||
h = _state.h, | ||
d = _state.d, | ||
timerState = _state.timerState; | ||
var children = this.props.children; | ||
return _react2.default.createElement( | ||
TimerContext.Provider, | ||
{ | ||
value: { | ||
ms: ms, s: s, m: m, h: h, d: d | ||
} | ||
}, | ||
Timer.getUI(children, { | ||
start: start, | ||
resume: resume, | ||
pause: pause, | ||
stop: stop, | ||
reset: reset, | ||
getTime: getTime, | ||
getTimerState: getTimerState, | ||
setTime: setTime, | ||
setDirection: setDirection, | ||
setCheckpoints: setCheckpoints, | ||
timerState: timerState | ||
}) | ||
); | ||
} | ||
}], [{ | ||
key: 'getUI', | ||
value: function getUI(children, renderProps) { | ||
if (children === null) { | ||
return null; | ||
} | ||
if (Array.isArray(children) || _react2.default.isValidElement(children)) { | ||
return children; | ||
} | ||
if (children.prototype && children.prototype.isReactComponent) { | ||
return _react2.default.createElement(children, renderProps); | ||
} | ||
if (typeof children === 'function') { | ||
return children(renderProps); | ||
} | ||
throw new Error('Please use one of the supported APIs for children'); | ||
} | ||
}]); | ||
return Timer; | ||
}(_react2.default.PureComponent); | ||
Timer.Consumer = TimerContext.Consumer; | ||
Timer.Milliseconds = Milliseconds; | ||
Timer.Seconds = Seconds; | ||
Timer.Minutes = Minutes; | ||
Timer.Hours = Hours; | ||
Timer.Days = Days; | ||
Timer.defaultProps = { | ||
timeToUpdate: 1000, | ||
direction: 'forward', | ||
initialTime: 0, | ||
startImmediately: true, | ||
lastUnit: 'd', | ||
checkpoints: [], | ||
children: null, | ||
onStart: function onStart() {}, | ||
onResume: function onResume() {}, | ||
onPause: function onPause() {}, | ||
onStop: function onStop() {}, | ||
onReset: function onReset() {} | ||
}; | ||
Timer.propTypes = { | ||
/** Timer count direction */ | ||
direction: _propTypes2.default.oneOf(['forward', 'backward']), | ||
/** Inittial time on timer */ | ||
initialTime: _propTypes2.default.number, | ||
/** Time to rerender */ | ||
timeToUpdate: _propTypes2.default.number, | ||
/** Start timer immediately after render */ | ||
startImmediately: _propTypes2.default.bool, | ||
/** Function that will be called on timer start */ | ||
onStart: _propTypes2.default.func, | ||
/** Function that will be called on timer resume */ | ||
onResume: _propTypes2.default.func, | ||
/** Function that will be called on timer pause */ | ||
onPause: _propTypes2.default.func, | ||
/** Function that will be called on timer stop */ | ||
onStop: _propTypes2.default.func, | ||
/** Function that will be called on timer reset */ | ||
onReset: _propTypes2.default.func, | ||
/** Last unit will accumulate time, for example, 26 hours or 90 seconds */ | ||
lastUnit: _propTypes2.default.oneOf(['ms', 's', 'm', 'h', 'd']), | ||
/** Time checkpoints with callback functions */ | ||
checkpoints: _propTypes2.default.arrayOf(_propTypes2.default.shape({ | ||
time: _propTypes2.default.number, | ||
callback: _propTypes2.default.func | ||
})), | ||
children: _propTypes2.default.any | ||
}; | ||
exports.default = Timer; | ||
Timer.getUI = function (children, renderProps) { | ||
if (children === null) { | ||
return null; | ||
} | ||
if (Array.isArray(children) || React.isValidElement(children)) { | ||
return children; | ||
} | ||
if (children.prototype && children.prototype.isReactComponent) { | ||
return React.createElement(children, renderProps); | ||
} | ||
if (typeof children === 'function') { | ||
return children(renderProps); | ||
} | ||
throw new Error('Please use one of the supported APIs for children'); | ||
}; | ||
Timer.prototype.componentDidMount = function () { | ||
var startImmediately = this.props.startImmediately; | ||
startImmediately && this.timer.start(); | ||
}; | ||
Timer.prototype.componentWillUnmount = function () { | ||
this.timer.stop(); | ||
}; | ||
Timer.prototype.render = function () { | ||
var _a = this, start = _a.start, pause = _a.pause, resume = _a.resume, stop = _a.stop, reset = _a.reset, getTime = _a.getTime, getTimerState = _a.getTimerState, setTime = _a.setTime, setDirection = _a.setDirection, setCheckpoints = _a.setCheckpoints; | ||
var _b = this.state, ms = _b.ms, s = _b.s, m = _b.m, h = _b.h, d = _b.d, timerState = _b.timerState; | ||
var children = this.props.children; | ||
return (React.createElement(TimerContext.Provider, { value: { ms: ms, s: s, m: m, h: h, d: d } }, Timer.getUI(children, { | ||
start: start, | ||
resume: resume, | ||
pause: pause, | ||
stop: stop, | ||
reset: reset, | ||
getTime: getTime, | ||
getTimerState: getTimerState, | ||
setTime: setTime, | ||
setDirection: setDirection, | ||
setCheckpoints: setCheckpoints, | ||
timerState: timerState, | ||
}))); | ||
}; | ||
Timer.prototype.setTime = function (time) { | ||
this.timer.setTime(time); | ||
}; | ||
Timer.prototype.getTime = function () { | ||
return this.timer.getTime(); | ||
}; | ||
Timer.prototype.getTimerState = function () { | ||
return this.timer.state; | ||
}; | ||
Timer.prototype.setDirection = function (direction) { | ||
this.timer.setDirection(direction); | ||
}; | ||
Timer.prototype.setCheckpoints = function (checkpoints) { | ||
this.timer.setCheckpoints(checkpoints); | ||
}; | ||
Timer.prototype.start = function () { | ||
this.timer.start(); | ||
this.props.onStart(); | ||
}; | ||
Timer.prototype.stop = function () { | ||
this.timer.stop(); | ||
this.props.onStop(); | ||
}; | ||
Timer.prototype.pause = function () { | ||
this.timer.pause(); | ||
this.props.onPause(); | ||
}; | ||
Timer.prototype.reset = function () { | ||
this.timer.reset(); | ||
this.props.onReset(); | ||
}; | ||
Timer.prototype.resume = function () { | ||
this.timer.resume(); | ||
this.props.onResume(); | ||
}; | ||
Timer.Consumer = TimerContext.Consumer; | ||
Timer.Milliseconds = Milliseconds; | ||
Timer.Seconds = Seconds; | ||
Timer.Minutes = Minutes; | ||
Timer.Hours = Hours; | ||
Timer.Days = Days; | ||
Timer.defaultProps = { | ||
timeToUpdate: 1000, | ||
direction: 'forward', | ||
initialTime: 0, | ||
startImmediately: true, | ||
lastUnit: 'd', | ||
checkpoints: [], | ||
children: null, | ||
onStart: function () { }, | ||
onResume: function () { }, | ||
onPause: function () { }, | ||
onStop: function () { }, | ||
onReset: function () { }, | ||
}; | ||
return Timer; | ||
}(React.PureComponent)); | ||
export default Timer; | ||
//# sourceMappingURL=Timer.js.map |
@@ -1,28 +0,27 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = getTimeParts; | ||
function getTimeParts(time, lastUnit) { | ||
var units = ['ms', 's', 'm', 'h', 'd']; | ||
var lastUnitIndex = units.findIndex(function (unit) { | ||
return unit === lastUnit; | ||
}); | ||
var dividers = [1000, 60, 60, 24, 1]; | ||
var dividersAcc = [1, 1000, 60000, 3600000, 86400000]; | ||
var output = units.reduce(function (obj, unit, index) { | ||
if (index > lastUnitIndex) { | ||
obj[unit] = 0; | ||
} else if (index === lastUnitIndex) { | ||
obj[unit] = Math.floor(time / dividersAcc[index]); | ||
} else { | ||
obj[unit] = Math.floor(time / dividersAcc[index]) % dividers[index]; | ||
} | ||
return obj; | ||
}, {}); | ||
return output; | ||
} | ||
export default function getTimeParts(time, lastUnit) { | ||
var units = ['ms', 's', 'm', 'h', 'd']; | ||
var lastUnitIndex = units.findIndex(function (unit) { return unit === lastUnit; }); | ||
var dividers = [1000, 60, 60, 24, 1]; | ||
var dividersAcc = [1, 1000, 60000, 3600000, 86400000]; | ||
var startValue = { | ||
ms: 0, | ||
s: 0, | ||
m: 0, | ||
h: 0, | ||
d: 0, | ||
}; | ||
var output = units.reduce(function (obj, unit, index) { | ||
if (index > lastUnitIndex) { | ||
obj[unit] = 0; | ||
} | ||
else if (index === lastUnitIndex) { | ||
obj[unit] = Math.floor(time / dividersAcc[index]); | ||
} | ||
else { | ||
obj[unit] = Math.floor(time / dividersAcc[index]) % dividers[index]; | ||
} | ||
return obj; | ||
}, startValue); | ||
return output; | ||
} | ||
//# sourceMappingURL=getTimeParts.js.map |
@@ -1,198 +0,114 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
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; 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 _getTimeParts2 = require('../helpers/getTimeParts'); | ||
var _getTimeParts3 = _interopRequireDefault(_getTimeParts2); | ||
var _TimerState = require('./TimerState'); | ||
var _TimerState2 = _interopRequireDefault(_TimerState); | ||
var _Units = require('./Units'); | ||
var _Units2 = _interopRequireDefault(_Units); | ||
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"); } } | ||
var Timer = function () { | ||
function Timer(_ref) { | ||
var _ref$initialTime = _ref.initialTime, | ||
initialTime = _ref$initialTime === undefined ? 0 : _ref$initialTime, | ||
_ref$direction = _ref.direction, | ||
direction = _ref$direction === undefined ? 'forward' : _ref$direction, | ||
_ref$timeToUpdate = _ref.timeToUpdate, | ||
timeToUpdate = _ref$timeToUpdate === undefined ? 1000 : _ref$timeToUpdate, | ||
_ref$lastUnit = _ref.lastUnit, | ||
lastUnit = _ref$lastUnit === undefined ? _Units2.default.d : _ref$lastUnit, | ||
_ref$checkpoints = _ref.checkpoints, | ||
checkpoints = _ref$checkpoints === undefined ? [] : _ref$checkpoints, | ||
_ref$onChange = _ref.onChange, | ||
onChange = _ref$onChange === undefined ? function () {} : _ref$onChange; | ||
_classCallCheck(this, Timer); | ||
this.initialTime = initialTime; | ||
this.time = initialTime; | ||
this.direction = direction; | ||
this.timeToUpdate = timeToUpdate; | ||
this.lastUnit = lastUnit; | ||
this.checkpoints = checkpoints; | ||
this._state = new _TimerState2.default(onChange); | ||
this._onChange = onChange; | ||
this.timerId = null; | ||
} | ||
_createClass(Timer, [{ | ||
key: '_getTimeParts', | ||
value: function _getTimeParts(time) { | ||
return (0, _getTimeParts3.default)(time, this.lastUnit); | ||
import getTimeParts from '../helpers/getTimeParts'; | ||
import TimerState from './TimerState'; | ||
var Timer = /** @class */ (function () { | ||
function Timer(_a) { | ||
var _b = _a.initialTime, initialTime = _b === void 0 ? 0 : _b, _c = _a.direction, direction = _c === void 0 ? 'forward' : _c, _d = _a.timeToUpdate, timeToUpdate = _d === void 0 ? 1000 : _d, _e = _a.lastUnit, lastUnit = _e === void 0 ? 'd' : _e, _f = _a.checkpoints, checkpoints = _f === void 0 ? [] : _f, _g = _a.onChange, onChange = _g === void 0 ? function () { } : _g; | ||
this.initialTime = initialTime; | ||
this.time = initialTime; | ||
this.direction = direction; | ||
this.timeToUpdate = timeToUpdate; | ||
this.lastUnit = lastUnit; | ||
this.checkpoints = checkpoints; | ||
this.innerState = new TimerState(onChange); | ||
this.onChange = onChange; | ||
this.timerId = null; | ||
} | ||
}, { | ||
key: '_setTimerInterval', | ||
value: function _setTimerInterval() { | ||
var _this = this; | ||
var callImmediately = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; | ||
var timeToUpdate = this.timeToUpdate; | ||
var repeatedFunc = function repeatedFunc() { | ||
var oldTime = _this.time; | ||
var updatedTime = _this._computeTime(); | ||
_this._onChange(_extends({}, _this._getTimeParts(updatedTime))); | ||
_this.checkpoints.map(function (_ref2) { | ||
var time = _ref2.time, | ||
callback = _ref2.callback; | ||
var checkForForward = time > oldTime && time <= updatedTime; | ||
var checkForBackward = time < oldTime && time >= updatedTime; | ||
var checkIntersection = _this.direction === 'backward' ? checkForBackward : checkForForward; | ||
if (checkIntersection) { | ||
callback(); | ||
} | ||
}); | ||
}; | ||
callImmediately && this._onChange(_extends({}, this._getTimeParts(this.time))); | ||
this.timerId = setInterval(repeatedFunc, timeToUpdate); | ||
} | ||
}, { | ||
key: 'setTime', | ||
value: function setTime(time) { | ||
this.time = time; | ||
} | ||
}, { | ||
key: 'getTime', | ||
value: function getTime() { | ||
return this.time; | ||
} | ||
}, { | ||
key: 'setDirection', | ||
value: function setDirection(direction) { | ||
this.direction = direction; | ||
} | ||
}, { | ||
key: 'setCheckpoints', | ||
value: function setCheckpoints(checkpoints) { | ||
this.checkpoints = checkpoints; | ||
} | ||
}, { | ||
key: 'start', | ||
value: function start() { | ||
if (this._state.setPlaying()) { | ||
this._setTimerInterval(true); | ||
} | ||
} | ||
}, { | ||
key: 'resume', | ||
value: function resume() { | ||
if (!this._state.isStopped() && this._state.setPlaying()) { | ||
this._setTimerInterval(); | ||
} | ||
} | ||
}, { | ||
key: 'pause', | ||
value: function pause() { | ||
if (this._state.setPaused()) { | ||
clearInterval(this.timerId); | ||
} | ||
} | ||
}, { | ||
key: 'stop', | ||
value: function stop() { | ||
if (this._state.setStopped()) { | ||
clearInterval(this.timerId); | ||
} | ||
} | ||
}, { | ||
key: 'reset', | ||
value: function reset() { | ||
this.time = this.initialTime; | ||
this._onChange(_extends({}, this._getTimeParts(this.time))); | ||
} | ||
}, { | ||
key: '_computeTime', | ||
value: function _computeTime() { | ||
var _state = this._state, | ||
time = this.time, | ||
direction = this.direction, | ||
timeToUpdate = this.timeToUpdate; | ||
if (_state.isPlaying()) { | ||
switch (direction) { | ||
case 'forward': | ||
this.time = time + timeToUpdate; | ||
return this.time; | ||
case 'backward': | ||
{ | ||
this.time = time - timeToUpdate; | ||
if (this.time < 0) { | ||
this.stop(); | ||
return 0; | ||
} | ||
return this.time; | ||
Object.defineProperty(Timer.prototype, "state", { | ||
get: function () { | ||
return this.innerState.getState(); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Timer.prototype, "timeParts", { | ||
get: function () { | ||
return this.getTimeParts(this.computeTime()); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Timer.prototype.getTimeParts = function (time) { | ||
return getTimeParts(time, this.lastUnit); | ||
}; | ||
Timer.prototype.setTime = function (time) { | ||
this.time = time; | ||
}; | ||
Timer.prototype.getTime = function () { | ||
return this.time; | ||
}; | ||
Timer.prototype.setDirection = function (direction) { | ||
this.direction = direction; | ||
}; | ||
Timer.prototype.setCheckpoints = function (checkpoints) { | ||
this.checkpoints = checkpoints; | ||
}; | ||
Timer.prototype.start = function () { | ||
if (this.innerState.setPlaying()) { | ||
this.setTimerInterval(true); | ||
} | ||
}; | ||
Timer.prototype.resume = function () { | ||
if (!this.innerState.isStopped() && this.innerState.setPlaying()) { | ||
this.setTimerInterval(); | ||
} | ||
}; | ||
Timer.prototype.pause = function () { | ||
if (this.innerState.setPaused()) { | ||
clearInterval(this.timerId); | ||
} | ||
}; | ||
Timer.prototype.stop = function () { | ||
if (this.innerState.setStopped()) { | ||
clearInterval(this.timerId); | ||
} | ||
}; | ||
Timer.prototype.reset = function () { | ||
this.time = this.initialTime; | ||
this.onChange(this.getTimeParts(this.time)); | ||
}; | ||
Timer.prototype.setTimerInterval = function (callImmediately) { | ||
var _this = this; | ||
if (callImmediately === void 0) { callImmediately = false; } | ||
var repeatedFunc = function () { | ||
var oldTime = _this.time; | ||
var updatedTime = _this.computeTime(); | ||
_this.onChange(_this.getTimeParts(updatedTime)); | ||
_this.checkpoints.map(function (_a) { | ||
var time = _a.time, callback = _a.callback; | ||
var checkForForward = time > oldTime && time <= updatedTime; | ||
var checkForBackward = time < oldTime && time >= updatedTime; | ||
var checkIntersection = _this.direction === 'backward' ? | ||
checkForBackward : | ||
checkForForward; | ||
if (checkIntersection) { | ||
callback(); | ||
} | ||
}); | ||
}; | ||
callImmediately && this.onChange(this.getTimeParts(this.time)); | ||
this.timerId = setInterval(repeatedFunc, this.timeToUpdate); | ||
}; | ||
Timer.prototype.computeTime = function () { | ||
if (this.innerState.isPlaying()) { | ||
switch (this.direction) { | ||
case 'forward': | ||
this.time = this.time + this.timeToUpdate; | ||
return this.time; | ||
case 'backward': { | ||
this.time = this.time - this.timeToUpdate; | ||
if (this.time < 0) { | ||
this.stop(); | ||
return 0; | ||
} | ||
return this.time; | ||
} | ||
default: | ||
return this.time; | ||
} | ||
default: | ||
return time; | ||
} | ||
} | ||
return time; | ||
} | ||
}, { | ||
key: 'state', | ||
get: function get() { | ||
return this._state.getState(); | ||
} | ||
}, { | ||
key: 'timeParts', | ||
get: function get() { | ||
return this._getTimeParts(this._computeTime()); | ||
} | ||
}]); | ||
return Timer; | ||
}(); | ||
exports.default = Timer; | ||
return this.time; | ||
}; | ||
return Timer; | ||
}()); | ||
export default Timer; | ||
//# sourceMappingURL=TimerModel.js.map |
@@ -1,104 +0,63 @@ | ||
'use strict'; | ||
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; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var INITED = exports.INITED = 'INITED'; | ||
var PLAYING = exports.PLAYING = 'PLAYING'; | ||
var PAUSED = exports.PAUSED = 'PAUSED'; | ||
var STOPPED = exports.STOPPED = 'STOPPED'; | ||
var TimerState = function () { | ||
function TimerState() { | ||
var _this = this; | ||
var onChangeStatus = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {}; | ||
_classCallCheck(this, TimerState); | ||
this._onChange = function () { | ||
return onChangeStatus({ timerState: _this._state }); | ||
export var INITED = 'INITED'; | ||
export var PLAYING = 'PLAYING'; | ||
export var PAUSED = 'PAUSED'; | ||
export var STOPPED = 'STOPPED'; | ||
var TimerState = /** @class */ (function () { | ||
function TimerState(onChangeStatus) { | ||
if (onChangeStatus === void 0) { onChangeStatus = function (obj) { }; } | ||
var _this = this; | ||
this.state = INITED; | ||
this.onChange = function () { return onChangeStatus({ timerState: _this.state }); }; | ||
this.state = INITED; | ||
} | ||
TimerState.prototype.getState = function () { | ||
return this.state; | ||
}; | ||
this._state = INITED; | ||
} | ||
_createClass(TimerState, [{ | ||
key: 'getState', | ||
value: function getState() { | ||
return this._state; | ||
} | ||
}, { | ||
key: 'setInited', | ||
value: function setInited() { | ||
if (this._state === INITED) return false; | ||
this._state = INITED; | ||
this._onChange(); | ||
return true; | ||
} | ||
}, { | ||
key: 'isInited', | ||
value: function isInited() { | ||
return this._state === INITED; | ||
} | ||
}, { | ||
key: 'setPlaying', | ||
value: function setPlaying() { | ||
if (this._state === PLAYING) return false; | ||
this._state = PLAYING; | ||
this._onChange(); | ||
return true; | ||
} | ||
}, { | ||
key: 'isPlaying', | ||
value: function isPlaying() { | ||
return this._state === PLAYING; | ||
} | ||
}, { | ||
key: 'setPaused', | ||
value: function setPaused() { | ||
if (this._state !== PLAYING) return false; | ||
this._state = PAUSED; | ||
this._onChange(); | ||
return true; | ||
} | ||
}, { | ||
key: 'isPaused', | ||
value: function isPaused() { | ||
return this._state === PAUSED; | ||
} | ||
}, { | ||
key: 'setStopped', | ||
value: function setStopped() { | ||
if (this._state === INITED) return false; | ||
this._state = STOPPED; | ||
this._onChange(); | ||
return true; | ||
} | ||
}, { | ||
key: 'isStopped', | ||
value: function isStopped() { | ||
return this._state === STOPPED; | ||
} | ||
}]); | ||
return TimerState; | ||
}(); | ||
exports.default = TimerState; | ||
TimerState.prototype.setInited = function () { | ||
if (this.state === INITED) { | ||
return false; | ||
} | ||
this.state = INITED; | ||
this.onChange(); | ||
return true; | ||
}; | ||
TimerState.prototype.isInited = function () { | ||
return this.state === INITED; | ||
}; | ||
TimerState.prototype.setPlaying = function () { | ||
if (this.state === PLAYING) { | ||
return false; | ||
} | ||
this.state = PLAYING; | ||
this.onChange(); | ||
return true; | ||
}; | ||
TimerState.prototype.isPlaying = function () { | ||
return this.state === PLAYING; | ||
}; | ||
TimerState.prototype.setPaused = function () { | ||
if (this.state !== PLAYING) { | ||
return false; | ||
} | ||
this.state = PAUSED; | ||
this.onChange(); | ||
return true; | ||
}; | ||
TimerState.prototype.isPaused = function () { | ||
return this.state === PAUSED; | ||
}; | ||
TimerState.prototype.setStopped = function () { | ||
if (this.state === INITED) { | ||
return false; | ||
} | ||
this.state = STOPPED; | ||
this.onChange(); | ||
return true; | ||
}; | ||
TimerState.prototype.isStopped = function () { | ||
return this.state === STOPPED; | ||
}; | ||
return TimerState; | ||
}()); | ||
export default TimerState; | ||
//# sourceMappingURL=TimerState.js.map |
@@ -1,12 +0,8 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = { | ||
ms: 'ms', | ||
s: 's', | ||
m: 'm', | ||
h: 'h', | ||
d: 'd' | ||
}; | ||
export default { | ||
ms: 'ms', | ||
s: 's', | ||
m: 'm', | ||
h: 'h', | ||
d: 'd', | ||
}; | ||
//# sourceMappingURL=Units.js.map |
{ | ||
"name": "react-compound-timer", | ||
"version": "1.0.8", | ||
"version": "1.0.10", | ||
"description": "Timer compound react component", | ||
"main": "build/components/Timer/Timer", | ||
"types": "build/components/Timer/Timer.d.ts", | ||
"keywords": [ | ||
@@ -17,4 +18,5 @@ "react", | ||
"build": "npx styleguidist build", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"transpile": "babel src -d build --copy-files" | ||
"test": "jest", | ||
"transpile": "tsc", | ||
"prepublishOnly": "npm run transpile" | ||
}, | ||
@@ -24,15 +26,14 @@ "author": "German Volkov <Volkov.german.1997@gmail.com> (https://volkov97.com)", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-loader": "^7.1.5", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-plugin-transform-react-jsx": "^6.24.1", | ||
"babel-preset-env": "^1.7.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-airbnb": "^17.0.0", | ||
"eslint-plugin-import": "^2.13.0", | ||
"eslint-plugin-jsx-a11y": "^6.1.1", | ||
"eslint-plugin-react": "^7.10.0", | ||
"@types/jest": "^24.0.4", | ||
"@types/react": "^16.8.3", | ||
"jest": "^24.1.0", | ||
"react-docgen-typescript": "^1.12.3", | ||
"react-dom": "^16.7.0", | ||
"react-styleguidist": "^8.0.6", | ||
"ts-jest": "^23.10.5", | ||
"ts-loader": "^5.3.3", | ||
"tslint": "^5.12.1", | ||
"tslint-config-airbnb": "^5.11.1", | ||
"tslint-react": "^3.6.0", | ||
"typescript": "^3.3.3", | ||
"webpack": "^4.16.1", | ||
@@ -42,5 +43,4 @@ "webpack-cli": "^3.1.0" | ||
"dependencies": { | ||
"prop-types": "^15.6.2", | ||
"react": "^16.7.0" | ||
"react": "^16.8.2" | ||
} | ||
} |
@@ -0,3 +1,5 @@ | ||
const docGen = require('react-docgen-typescript'); | ||
module.exports = { | ||
components: 'src/components/**/*.jsx', | ||
components: 'src/components/**/*.tsx', | ||
@@ -9,2 +11,4 @@ showSidebar: false, | ||
styleguideDir: 'docs', | ||
propsParser: docGen.withCustomConfig('./tsconfig.json').parse, | ||
}; |
const path = require('path'); | ||
module.exports = { | ||
entry: './src/components/Timer/Timer.jsx', | ||
entry: './src/components/Timer/Timer', | ||
mode: 'production', | ||
@@ -11,14 +11,10 @@ output: { | ||
}, | ||
resolve: { | ||
extensions: ['.ts', '.tsx'], | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js(x)$/, | ||
include: path.resolve(__dirname, 'src'), | ||
exclude: /(node_modules|bower_components|build)/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
presets: ['env'], | ||
}, | ||
}, | ||
test: /\.tsx?$/, | ||
loader: 'ts-loader', | ||
}, | ||
@@ -25,0 +21,0 @@ ], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
53001
1
14
28
808
0
1
- Removedprop-types@^15.6.2
Updatedreact@^16.8.2