UNPKG

tdesign-mobile-vue

Version:
101 lines (97 loc) 3.6 kB
/** * tdesign v1.7.0 * (c) 2024 TDesign Group * @license MIT */ import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator'; import _regeneratorRuntime from '@babel/runtime/regenerator'; import { ref, reactive, watch } from 'vue'; import { useRafFn } from '@vueuse/core'; import { getShowTimes, getRemainTimes, getScreenFps } from './utils.js'; import { isBrowser } from '../util.js'; import '@babel/runtime/helpers/slicedToArray'; import 'lodash/isNumber'; import '../../config.js'; function useCountDown(props, visibility) { var _ref = props || {}, _ref$time = _ref.time, time = _ref$time === void 0 ? 0 : _ref$time, autoStart = _ref.autoStart, _ref$millisecond = _ref.millisecond, millisecond = _ref$millisecond === void 0 ? false : _ref$millisecond, _ref$format = _ref.format, format = _ref$format === void 0 ? "HH:mm:ss" : _ref$format, _ref$splitWithUnit = _ref.splitWithUnit, splitWithUnit = _ref$splitWithUnit === void 0 ? false : _ref$splitWithUnit, onFinish = _ref.onFinish, onChange = _ref.onChange; var fps = ref(); var count = ref(Number(time)); var showTimes = reactive(getShowTimes(getRemainTimes(time), format, millisecond, splitWithUnit)); var hiddenTime = 0; visibility && watch(visibility, function (val) { if (val) { count.value -= Date.now() - hiddenTime; rafFn(); } else { hiddenTime = Date.now(); } }); var rafFn = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _getShowTimes, _getShowTimes$forEach; var res, times; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (isBrowser) { _context.next = 2; break; } return _context.abrupt("return"); case 2: if (fps.value) { _context.next = 7; break; } _context.next = 5; return getScreenFps === null || getScreenFps === void 0 ? void 0 : getScreenFps(); case 5: res = _context.sent; fps.value = res || 60; case 7: count.value = parseInt("".concat(Number(count.value) - 1e3 / fps.value), 10); if (count.value <= 0) { pause === null || pause === void 0 || pause(); count.value = 0; } times = getRemainTimes(count.value); onChange === null || onChange === void 0 || onChange(times); count.value === 0 && (onFinish === null || onFinish === void 0 ? void 0 : onFinish()); (_getShowTimes = getShowTimes(times, format, millisecond, splitWithUnit)) === null || _getShowTimes === void 0 || (_getShowTimes$forEach = _getShowTimes.forEach) === null || _getShowTimes$forEach === void 0 || _getShowTimes$forEach.call(_getShowTimes, function (i, idx) { return showTimes[idx].value = i === null || i === void 0 ? void 0 : i.value; }); case 13: case "end": return _context.stop(); } }, _callee); })); return function rafFn() { return _ref2.apply(this, arguments); }; }(); var _useRafFn = useRafFn(rafFn, { immediate: autoStart }), pause = _useRafFn.pause, resume = _useRafFn.resume; return { time: count, showTimes: showTimes, pause: pause, resume: resume }; } export { useCountDown }; //# sourceMappingURL=index.js.map