UNPKG

@fe6/water-pro

Version:

An enterprise-class UI design language and Vue-based implementation

95 lines (73 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.throttle = throttle; exports.useThrottle = useThrottle; var _warning = _interopRequireDefault(require("../warning")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function throttle(handle, wait) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; if (!_typeof(handle)) { (0, _warning.default)('handle is not Function!'); } var _options$immediate = options.immediate, immediate = _options$immediate === void 0 ? false : _options$immediate; var _options$once = options.once, once = _options$once === void 0 ? false : _options$once, _options$debounce = options.debounce, debounce = _options$debounce === void 0 ? false : _options$debounce; var timeoutId; // Has it been cancelled var cancelled = false; /** * @description: clear timer */ function clearTimer() { if (timeoutId) { window.clearTimeout(timeoutId); timeoutId = null; } } /** cancel exec */ function cancel() { clearTimer(); cancelled = true; } // If once is true, only execute once function cancelExec() { once && cancel(); } function fn() { var _this = this; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } // If it has been cancelled, it will not be executed if (cancelled) { return; } var exec = function exec() { !debounce && clearTimer(); handle.apply(_this, args); cancelExec(); }; if (immediate) { immediate = false; var callNow = !timeoutId; if (callNow) { exec(); timeoutId = null; } } else { debounce && clearTimer(); if (!timeoutId || debounce) { timeoutId = setTimeout(exec, wait); } } } return [fn, cancel]; } function useThrottle(handle, wait) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; return throttle(handle, wait, options); }