UNPKG

hjs-message

Version:
105 lines (90 loc) 3.61 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.CountDownTimer = undefined; var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _handler = require('./handler'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var MSG = 1; /** @babel */ var CountDownTimer = exports.CountDownTimer = function () { function CountDownTimer() { var _this = this; var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref$millisInFuture = _ref.millisInFuture, millisInFuture = _ref$millisInFuture === undefined ? 0 : _ref$millisInFuture, _ref$countDownInterva = _ref.countDownInterval, countDownInterval = _ref$countDownInterva === undefined ? 0 : _ref$countDownInterva, _ref$onTick = _ref.onTick, onTick = _ref$onTick === undefined ? null : _ref$onTick, _ref$onFinish = _ref.onFinish, onFinish = _ref$onFinish === undefined ? null : _ref$onFinish; (0, _classCallCheck3.default)(this, CountDownTimer); this.mMillisInFuture = millisInFuture; this.mCountdownInterval = countDownInterval; this.mCancelled = false; if (onFinish) { this.onFinish = onFinish; } if (onTick) { this.onTick = onTick; } this.mHandler = _handler.MessageHandler.create({ handleMessage: function handleMessage(msg) { var handled = false; var h = _this.mHandler; var t = Date.now(); var millisLeft = _this.mStopTimeInFuture - t; if (millisLeft <= 0) { _this.onFinish(); } else if (millisLeft < _this.mCountdownInterval) { handled = h.sendMessageDelayed(h.obtainMessage({ what: MSG }), millisLeft); } else { var lastTickStart = Date.now(); _this.onTick(millisLeft); var delay = lastTickStart + _this.mCountdownInterval - t; while (delay < 0) { delay += _this.mCountdownInterval; } handled = h.sendMessageDelayed(h.obtainMessage({ what: MSG }), delay); } return handled; } }); } (0, _createClass3.default)(CountDownTimer, [{ key: 'cancel', value: function cancel() { this.mCancelled = true; this.mHandler.removeMessages(MSG); this.onFinish(); } }, { key: 'isCancelled', value: function isCancelled() { return this.mCancelled; } }, { key: 'onFinish', value: function onFinish() {} }, { key: 'onTick', value: function onTick(millisUntilFinished) {} }, { key: 'start', value: function start() { this.mCancelled = false; if (this.mMillisInFuture <= 0) { this.onFinish(); return this; } this.mStopTimeInFuture = Date.now() + this.mMillisInFuture; this.mHandler.sendMessage(this.mHandler.obtainMessage({ what: MSG })); return this; } }]); return CountDownTimer; }();