throttle.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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);
  81. // Here we should make sure that: the `exec` SHOULD NOT be called later
  82. // than a new call of `cb`, that is, preserving the command order. Consider
  83. // calculating "scale rate" when roaming as an example. When a call of `cb`
  84. // happens, either the `exec` is called dierectly, or the call is delayed.
  85. // But the delayed call should never be later than next call of `cb`. Under
  86. // this assurance, we can simply update view state each time `dispatchAction`
  87. // triggered by user roaming, but not need to add extra code to avoid the
  88. // state being "rolled-back".
  89. if (thisDebounce) {
  90. timer = setTimeout(exec, thisDelay);
  91. } else {
  92. if (diff >= 0) {
  93. exec();
  94. } else {
  95. timer = setTimeout(exec, -diff);
  96. }
  97. }
  98. lastCall = currCall;
  99. };
  100. /**
  101. * Clear throttle.
  102. * @public
  103. */
  104. cb.clear = function () {
  105. if (timer) {
  106. clearTimeout(timer);
  107. timer = null;
  108. }
  109. };
  110. /**
  111. * Enable debounce once.
  112. */
  113. cb.debounceNextCall = function (debounceDelay) {
  114. debounceNextCall = debounceDelay;
  115. };
  116. return cb;
  117. }
  118. /**
  119. * Create throttle method or update throttle rate.
  120. *
  121. * @example
  122. * ComponentView.prototype.render = function () {
  123. * ...
  124. * throttle.createOrUpdate(
  125. * this,
  126. * '_dispatchAction',
  127. * this.model.get('throttle'),
  128. * 'fixRate'
  129. * );
  130. * };
  131. * ComponentView.prototype.remove = function () {
  132. * throttle.clear(this, '_dispatchAction');
  133. * };
  134. * ComponentView.prototype.dispose = function () {
  135. * throttle.clear(this, '_dispatchAction');
  136. * };
  137. *
  138. */
  139. export function createOrUpdate(obj, fnAttr, rate, throttleType) {
  140. var fn = obj[fnAttr];
  141. if (!fn) {
  142. return;
  143. }
  144. var originFn = fn[ORIGIN_METHOD] || fn;
  145. var lastThrottleType = fn[THROTTLE_TYPE];
  146. var lastRate = fn[RATE];
  147. if (lastRate !== rate || lastThrottleType !== throttleType) {
  148. if (rate == null || !throttleType) {
  149. return obj[fnAttr] = originFn;
  150. }
  151. fn = obj[fnAttr] = throttle(originFn, rate, throttleType === 'debounce');
  152. fn[ORIGIN_METHOD] = originFn;
  153. fn[THROTTLE_TYPE] = throttleType;
  154. fn[RATE] = rate;
  155. }
  156. return fn;
  157. }
  158. /**
  159. * Clear throttle. Example see throttle.createOrUpdate.
  160. */
  161. export function clear(obj, fnAttr) {
  162. var fn = obj[fnAttr];
  163. if (fn && fn[ORIGIN_METHOD]) {
  164. // Clear throttle
  165. fn.clear && fn.clear();
  166. obj[fnAttr] = fn[ORIGIN_METHOD];
  167. }
  168. }