throttle.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. var ORIGIN_METHOD = '\0__throttleOriginMethod';
  41. var RATE = '\0__throttleRate';
  42. var THROTTLE_TYPE = '\0__throttleType';
  43. ;
  44. /**
  45. * @public
  46. * @param {(Function)} fn
  47. * @param {number} [delay=0] Unit: ms.
  48. * @param {boolean} [debounce=false]
  49. * true: If call interval less than `delay`, only the last call works.
  50. * false: If call interval less than `delay, call works on fixed rate.
  51. * @return {(Function)} throttled fn.
  52. */
  53. export function throttle(fn, delay, debounce) {
  54. var currCall;
  55. var lastCall = 0;
  56. var lastExec = 0;
  57. var timer = null;
  58. var diff;
  59. var scope;
  60. var args;
  61. var debounceNextCall;
  62. delay = delay || 0;
  63. function exec() {
  64. lastExec = new Date().getTime();
  65. timer = null;
  66. fn.apply(scope, args || []);
  67. }
  68. var cb = function () {
  69. var cbArgs = [];
  70. for (var _i = 0; _i < arguments.length; _i++) {
  71. cbArgs[_i] = arguments[_i];
  72. }
  73. currCall = new Date().getTime();
  74. scope = this;
  75. args = cbArgs;
  76. var thisDelay = debounceNextCall || delay;
  77. var thisDebounce = debounceNextCall || debounce;
  78. debounceNextCall = null;
  79. diff = currCall - (thisDebounce ? lastCall : lastExec) - thisDelay;
  80. clearTimeout(timer); // Here we should make sure that: the `exec` SHOULD NOT be called later
  81. // than a new call of `cb`, that is, preserving the command order. Consider
  82. // calculating "scale rate" when roaming as an example. When a call of `cb`
  83. // happens, either the `exec` is called dierectly, or the call is delayed.
  84. // But the delayed call should never be later than next call of `cb`. Under
  85. // this assurance, we can simply update view state each time `dispatchAction`
  86. // triggered by user roaming, but not need to add extra code to avoid the
  87. // state being "rolled-back".
  88. if (thisDebounce) {
  89. timer = setTimeout(exec, thisDelay);
  90. } else {
  91. if (diff >= 0) {
  92. exec();
  93. } else {
  94. timer = setTimeout(exec, -diff);
  95. }
  96. }
  97. lastCall = currCall;
  98. };
  99. /**
  100. * Clear throttle.
  101. * @public
  102. */
  103. cb.clear = function () {
  104. if (timer) {
  105. clearTimeout(timer);
  106. timer = null;
  107. }
  108. };
  109. /**
  110. * Enable debounce once.
  111. */
  112. cb.debounceNextCall = function (debounceDelay) {
  113. debounceNextCall = debounceDelay;
  114. };
  115. return cb;
  116. }
  117. /**
  118. * Create throttle method or update throttle rate.
  119. *
  120. * @example
  121. * ComponentView.prototype.render = function () {
  122. * ...
  123. * throttle.createOrUpdate(
  124. * this,
  125. * '_dispatchAction',
  126. * this.model.get('throttle'),
  127. * 'fixRate'
  128. * );
  129. * };
  130. * ComponentView.prototype.remove = function () {
  131. * throttle.clear(this, '_dispatchAction');
  132. * };
  133. * ComponentView.prototype.dispose = function () {
  134. * throttle.clear(this, '_dispatchAction');
  135. * };
  136. *
  137. */
  138. export function createOrUpdate(obj, fnAttr, rate, throttleType) {
  139. var fn = obj[fnAttr];
  140. if (!fn) {
  141. return;
  142. }
  143. var originFn = fn[ORIGIN_METHOD] || fn;
  144. var lastThrottleType = fn[THROTTLE_TYPE];
  145. var lastRate = fn[RATE];
  146. if (lastRate !== rate || lastThrottleType !== throttleType) {
  147. if (rate == null || !throttleType) {
  148. return obj[fnAttr] = originFn;
  149. }
  150. fn = obj[fnAttr] = throttle(originFn, rate, throttleType === 'debounce');
  151. fn[ORIGIN_METHOD] = originFn;
  152. fn[THROTTLE_TYPE] = throttleType;
  153. fn[RATE] = rate;
  154. }
  155. return fn;
  156. }
  157. /**
  158. * Clear throttle. Example see throttle.createOrUpdate.
  159. */
  160. export function clear(obj, fnAttr) {
  161. var fn = obj[fnAttr];
  162. if (fn && fn[ORIGIN_METHOD]) {
  163. // Clear throttle
  164. fn.clear && fn.clear();
  165. obj[fnAttr] = fn[ORIGIN_METHOD];
  166. }
  167. }