throttle.js 5.3 KB

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