debounce-2c8b61fb.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. 'use strict';
  7. /** Detect free variable `global` from Node.js. */
  8. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  9. /** Detect free variable `self`. */
  10. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  11. /** Used as a reference to the global object. */
  12. var root = freeGlobal || freeSelf || Function('return this')();
  13. /** Built-in value references. */
  14. var Symbol = root.Symbol;
  15. /** Used for built-in method references. */
  16. var objectProto$1 = Object.prototype;
  17. /** Used to check objects for own properties. */
  18. var hasOwnProperty = objectProto$1.hasOwnProperty;
  19. /**
  20. * Used to resolve the
  21. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  22. * of values.
  23. */
  24. var nativeObjectToString$1 = objectProto$1.toString;
  25. /** Built-in value references. */
  26. var symToStringTag$1 = Symbol ? Symbol.toStringTag : undefined;
  27. /**
  28. * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values.
  29. *
  30. * @private
  31. * @param {*} value The value to query.
  32. * @returns {string} Returns the raw `toStringTag`.
  33. */
  34. function getRawTag(value) {
  35. var isOwn = hasOwnProperty.call(value, symToStringTag$1),
  36. tag = value[symToStringTag$1];
  37. try {
  38. value[symToStringTag$1] = undefined;
  39. var unmasked = true;
  40. } catch (e) {}
  41. var result = nativeObjectToString$1.call(value);
  42. if (unmasked) {
  43. if (isOwn) {
  44. value[symToStringTag$1] = tag;
  45. } else {
  46. delete value[symToStringTag$1];
  47. }
  48. }
  49. return result;
  50. }
  51. /** Used for built-in method references. */
  52. var objectProto = Object.prototype;
  53. /**
  54. * Used to resolve the
  55. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  56. * of values.
  57. */
  58. var nativeObjectToString = objectProto.toString;
  59. /**
  60. * Converts `value` to a string using `Object.prototype.toString`.
  61. *
  62. * @private
  63. * @param {*} value The value to convert.
  64. * @returns {string} Returns the converted string.
  65. */
  66. function objectToString(value) {
  67. return nativeObjectToString.call(value);
  68. }
  69. /** `Object#toString` result references. */
  70. var nullTag = '[object Null]',
  71. undefinedTag = '[object Undefined]';
  72. /** Built-in value references. */
  73. var symToStringTag = Symbol ? Symbol.toStringTag : undefined;
  74. /**
  75. * The base implementation of `getTag` without fallbacks for buggy environments.
  76. *
  77. * @private
  78. * @param {*} value The value to query.
  79. * @returns {string} Returns the `toStringTag`.
  80. */
  81. function baseGetTag(value) {
  82. if (value == null) {
  83. return value === undefined ? undefinedTag : nullTag;
  84. }
  85. return (symToStringTag && symToStringTag in Object(value))
  86. ? getRawTag(value)
  87. : objectToString(value);
  88. }
  89. /**
  90. * Checks if `value` is object-like. A value is object-like if it's not `null`
  91. * and has a `typeof` result of "object".
  92. *
  93. * @static
  94. * @memberOf _
  95. * @since 4.0.0
  96. * @category Lang
  97. * @param {*} value The value to check.
  98. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  99. * @example
  100. *
  101. * _.isObjectLike({});
  102. * // => true
  103. *
  104. * _.isObjectLike([1, 2, 3]);
  105. * // => true
  106. *
  107. * _.isObjectLike(_.noop);
  108. * // => false
  109. *
  110. * _.isObjectLike(null);
  111. * // => false
  112. */
  113. function isObjectLike(value) {
  114. return value != null && typeof value == 'object';
  115. }
  116. /** `Object#toString` result references. */
  117. var symbolTag = '[object Symbol]';
  118. /**
  119. * Checks if `value` is classified as a `Symbol` primitive or object.
  120. *
  121. * @static
  122. * @memberOf _
  123. * @since 4.0.0
  124. * @category Lang
  125. * @param {*} value The value to check.
  126. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  127. * @example
  128. *
  129. * _.isSymbol(Symbol.iterator);
  130. * // => true
  131. *
  132. * _.isSymbol('abc');
  133. * // => false
  134. */
  135. function isSymbol(value) {
  136. return typeof value == 'symbol' ||
  137. (isObjectLike(value) && baseGetTag(value) == symbolTag);
  138. }
  139. /** Used to match a single whitespace character. */
  140. var reWhitespace = /\s/;
  141. /**
  142. * Used by `_.trim` and `_.trimEnd` to get the index of the last non-whitespace
  143. * character of `string`.
  144. *
  145. * @private
  146. * @param {string} string The string to inspect.
  147. * @returns {number} Returns the index of the last non-whitespace character.
  148. */
  149. function trimmedEndIndex(string) {
  150. var index = string.length;
  151. while (index-- && reWhitespace.test(string.charAt(index))) {}
  152. return index;
  153. }
  154. /** Used to match leading whitespace. */
  155. var reTrimStart = /^\s+/;
  156. /**
  157. * The base implementation of `_.trim`.
  158. *
  159. * @private
  160. * @param {string} string The string to trim.
  161. * @returns {string} Returns the trimmed string.
  162. */
  163. function baseTrim(string) {
  164. return string
  165. ? string.slice(0, trimmedEndIndex(string) + 1).replace(reTrimStart, '')
  166. : string;
  167. }
  168. /**
  169. * Checks if `value` is the
  170. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  171. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  172. *
  173. * @static
  174. * @memberOf _
  175. * @since 0.1.0
  176. * @category Lang
  177. * @param {*} value The value to check.
  178. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  179. * @example
  180. *
  181. * _.isObject({});
  182. * // => true
  183. *
  184. * _.isObject([1, 2, 3]);
  185. * // => true
  186. *
  187. * _.isObject(_.noop);
  188. * // => true
  189. *
  190. * _.isObject(null);
  191. * // => false
  192. */
  193. function isObject(value) {
  194. var type = typeof value;
  195. return value != null && (type == 'object' || type == 'function');
  196. }
  197. /** Used as references for various `Number` constants. */
  198. var NAN = 0 / 0;
  199. /** Used to detect bad signed hexadecimal string values. */
  200. var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
  201. /** Used to detect binary string values. */
  202. var reIsBinary = /^0b[01]+$/i;
  203. /** Used to detect octal string values. */
  204. var reIsOctal = /^0o[0-7]+$/i;
  205. /** Built-in method references without a dependency on `root`. */
  206. var freeParseInt = parseInt;
  207. /**
  208. * Converts `value` to a number.
  209. *
  210. * @static
  211. * @memberOf _
  212. * @since 4.0.0
  213. * @category Lang
  214. * @param {*} value The value to process.
  215. * @returns {number} Returns the number.
  216. * @example
  217. *
  218. * _.toNumber(3.2);
  219. * // => 3.2
  220. *
  221. * _.toNumber(Number.MIN_VALUE);
  222. * // => 5e-324
  223. *
  224. * _.toNumber(Infinity);
  225. * // => Infinity
  226. *
  227. * _.toNumber('3.2');
  228. * // => 3.2
  229. */
  230. function toNumber(value) {
  231. if (typeof value == 'number') {
  232. return value;
  233. }
  234. if (isSymbol(value)) {
  235. return NAN;
  236. }
  237. if (isObject(value)) {
  238. var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
  239. value = isObject(other) ? (other + '') : other;
  240. }
  241. if (typeof value != 'string') {
  242. return value === 0 ? value : +value;
  243. }
  244. value = baseTrim(value);
  245. var isBinary = reIsBinary.test(value);
  246. return (isBinary || reIsOctal.test(value))
  247. ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  248. : (reIsBadHex.test(value) ? NAN : +value);
  249. }
  250. /**
  251. * Gets the timestamp of the number of milliseconds that have elapsed since
  252. * the Unix epoch (1 January 1970 00:00:00 UTC).
  253. *
  254. * @static
  255. * @memberOf _
  256. * @since 2.4.0
  257. * @category Date
  258. * @returns {number} Returns the timestamp.
  259. * @example
  260. *
  261. * _.defer(function(stamp) {
  262. * console.log(_.now() - stamp);
  263. * }, _.now());
  264. * // => Logs the number of milliseconds it took for the deferred invocation.
  265. */
  266. var now = function() {
  267. return root.Date.now();
  268. };
  269. /** Error message constants. */
  270. var FUNC_ERROR_TEXT = 'Expected a function';
  271. /* Built-in method references for those with the same name as other `lodash` methods. */
  272. var nativeMax = Math.max,
  273. nativeMin = Math.min;
  274. /**
  275. * Creates a debounced function that delays invoking `func` until after `wait`
  276. * milliseconds have elapsed since the last time the debounced function was
  277. * invoked. The debounced function comes with a `cancel` method to cancel
  278. * delayed `func` invocations and a `flush` method to immediately invoke them.
  279. * Provide `options` to indicate whether `func` should be invoked on the
  280. * leading and/or trailing edge of the `wait` timeout. The `func` is invoked
  281. * with the last arguments provided to the debounced function. Subsequent
  282. * calls to the debounced function return the result of the last `func`
  283. * invocation.
  284. *
  285. * **Note:** If `leading` and `trailing` options are `true`, `func` is
  286. * invoked on the trailing edge of the timeout only if the debounced function
  287. * is invoked more than once during the `wait` timeout.
  288. *
  289. * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
  290. * until to the next tick, similar to `setTimeout` with a timeout of `0`.
  291. *
  292. * See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
  293. * for details over the differences between `_.debounce` and `_.throttle`.
  294. *
  295. * @static
  296. * @memberOf _
  297. * @since 0.1.0
  298. * @category Function
  299. * @param {Function} func The function to debounce.
  300. * @param {number} [wait=0] The number of milliseconds to delay.
  301. * @param {Object} [options={}] The options object.
  302. * @param {boolean} [options.leading=false]
  303. * Specify invoking on the leading edge of the timeout.
  304. * @param {number} [options.maxWait]
  305. * The maximum time `func` is allowed to be delayed before it's invoked.
  306. * @param {boolean} [options.trailing=true]
  307. * Specify invoking on the trailing edge of the timeout.
  308. * @returns {Function} Returns the new debounced function.
  309. * @example
  310. *
  311. * // Avoid costly calculations while the window size is in flux.
  312. * jQuery(window).on('resize', _.debounce(calculateLayout, 150));
  313. *
  314. * // Invoke `sendMail` when clicked, debouncing subsequent calls.
  315. * jQuery(element).on('click', _.debounce(sendMail, 300, {
  316. * 'leading': true,
  317. * 'trailing': false
  318. * }));
  319. *
  320. * // Ensure `batchLog` is invoked once after 1 second of debounced calls.
  321. * var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 });
  322. * var source = new EventSource('/stream');
  323. * jQuery(source).on('message', debounced);
  324. *
  325. * // Cancel the trailing debounced invocation.
  326. * jQuery(window).on('popstate', debounced.cancel);
  327. */
  328. function debounce(func, wait, options) {
  329. var lastArgs,
  330. lastThis,
  331. maxWait,
  332. result,
  333. timerId,
  334. lastCallTime,
  335. lastInvokeTime = 0,
  336. leading = false,
  337. maxing = false,
  338. trailing = true;
  339. if (typeof func != 'function') {
  340. throw new TypeError(FUNC_ERROR_TEXT);
  341. }
  342. wait = toNumber(wait) || 0;
  343. if (isObject(options)) {
  344. leading = !!options.leading;
  345. maxing = 'maxWait' in options;
  346. maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait;
  347. trailing = 'trailing' in options ? !!options.trailing : trailing;
  348. }
  349. function invokeFunc(time) {
  350. var args = lastArgs,
  351. thisArg = lastThis;
  352. lastArgs = lastThis = undefined;
  353. lastInvokeTime = time;
  354. result = func.apply(thisArg, args);
  355. return result;
  356. }
  357. function leadingEdge(time) {
  358. // Reset any `maxWait` timer.
  359. lastInvokeTime = time;
  360. // Start the timer for the trailing edge.
  361. timerId = setTimeout(timerExpired, wait);
  362. // Invoke the leading edge.
  363. return leading ? invokeFunc(time) : result;
  364. }
  365. function remainingWait(time) {
  366. var timeSinceLastCall = time - lastCallTime,
  367. timeSinceLastInvoke = time - lastInvokeTime,
  368. timeWaiting = wait - timeSinceLastCall;
  369. return maxing
  370. ? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke)
  371. : timeWaiting;
  372. }
  373. function shouldInvoke(time) {
  374. var timeSinceLastCall = time - lastCallTime,
  375. timeSinceLastInvoke = time - lastInvokeTime;
  376. // Either this is the first call, activity has stopped and we're at the
  377. // trailing edge, the system time has gone backwards and we're treating
  378. // it as the trailing edge, or we've hit the `maxWait` limit.
  379. return (lastCallTime === undefined || (timeSinceLastCall >= wait) ||
  380. (timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait));
  381. }
  382. function timerExpired() {
  383. var time = now();
  384. if (shouldInvoke(time)) {
  385. return trailingEdge(time);
  386. }
  387. // Restart the timer.
  388. timerId = setTimeout(timerExpired, remainingWait(time));
  389. }
  390. function trailingEdge(time) {
  391. timerId = undefined;
  392. // Only invoke if we have `lastArgs` which means `func` has been
  393. // debounced at least once.
  394. if (trailing && lastArgs) {
  395. return invokeFunc(time);
  396. }
  397. lastArgs = lastThis = undefined;
  398. return result;
  399. }
  400. function cancel() {
  401. if (timerId !== undefined) {
  402. clearTimeout(timerId);
  403. }
  404. lastInvokeTime = 0;
  405. lastArgs = lastCallTime = lastThis = timerId = undefined;
  406. }
  407. function flush() {
  408. return timerId === undefined ? result : trailingEdge(now());
  409. }
  410. function debounced() {
  411. var time = now(),
  412. isInvoking = shouldInvoke(time);
  413. lastArgs = arguments;
  414. lastThis = this;
  415. lastCallTime = time;
  416. if (isInvoking) {
  417. if (timerId === undefined) {
  418. return leadingEdge(lastCallTime);
  419. }
  420. if (maxing) {
  421. // Handle invocations in a tight loop.
  422. clearTimeout(timerId);
  423. timerId = setTimeout(timerExpired, wait);
  424. return invokeFunc(lastCallTime);
  425. }
  426. }
  427. if (timerId === undefined) {
  428. timerId = setTimeout(timerExpired, wait);
  429. }
  430. return result;
  431. }
  432. debounced.cancel = cancel;
  433. debounced.flush = flush;
  434. return debounced;
  435. }
  436. exports.Symbol = Symbol;
  437. exports.baseGetTag = baseGetTag;
  438. exports.debounce = debounce;
  439. exports.freeGlobal = freeGlobal;
  440. exports.isObject = isObject;
  441. exports.isObjectLike = isObjectLike;
  442. exports.isSymbol = isSymbol;
  443. exports.root = root;