UNPKG

tdesign-mobile-vue

Version:
110 lines (102 loc) 4.24 kB
/** * tdesign v1.7.0 * (c) 2024 TDesign Group * @license MIT */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator'); var _regeneratorRuntime = require('@babel/runtime/regenerator'); var vue = require('vue'); var core = require('@vueuse/core'); var shared_useCountDown_utils = require('./utils.js'); var shared_util = require('../util.js'); require('@babel/runtime/helpers/slicedToArray'); require('lodash/isNumber'); require('../../config.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator); var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime); 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 = vue.ref(); var count = vue.ref(Number(time)); var showTimes = vue.reactive(shared_useCountDown_utils.getShowTimes(shared_useCountDown_utils.getRemainTimes(time), format, millisecond, splitWithUnit)); var hiddenTime = 0; visibility && vue.watch(visibility, function (val) { if (val) { count.value -= Date.now() - hiddenTime; rafFn(); } else { hiddenTime = Date.now(); } }); var rafFn = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator__default["default"](/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() { var _getShowTimes, _getShowTimes$forEach; var res, times; return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (shared_util.isBrowser) { _context.next = 2; break; } return _context.abrupt("return"); case 2: if (fps.value) { _context.next = 7; break; } _context.next = 5; return shared_useCountDown_utils.getScreenFps === null || shared_useCountDown_utils.getScreenFps === void 0 ? void 0 : shared_useCountDown_utils.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 = shared_useCountDown_utils.getRemainTimes(count.value); onChange === null || onChange === void 0 || onChange(times); count.value === 0 && (onFinish === null || onFinish === void 0 ? void 0 : onFinish()); (_getShowTimes = shared_useCountDown_utils.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 = core.useRafFn(rafFn, { immediate: autoStart }), pause = _useRafFn.pause, resume = _useRafFn.resume; return { time: count, showTimes: showTimes, pause: pause, resume: resume }; } exports.useCountDown = useCountDown; //# sourceMappingURL=index.js.map