jenkins-bot has submitted this change and it was merged.

Change subject: Add throttle to complement debounce
......................................................................


Add throttle to complement debounce

Change-Id: Ic4bb40d0d6f57420f2582e33636100c2342707b2
---
M src/core.js
1 file changed, 52 insertions(+), 0 deletions(-)

Approvals:
  Divec: Looks good to me, approved
  Jforrester: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/core.js b/src/core.js
index 59c84f3..311e61e 100644
--- a/src/core.js
+++ b/src/core.js
@@ -247,6 +247,58 @@
 };
 
 /**
+ * Returns a function, that, when invoked, will only be triggered at most once
+ * during a given window of time. If called again during that window, it will
+ * wait until the window ends and then trigger itself again.
+ *
+ * As it's not knowable to the caller whether the function will actually run
+ * when the wrapper is called, return values from the function are entirely
+ * discarded.
+ *
+ * @param {Function} func
+ * @param {number} wait
+ * @return {Function}
+ */
+OO.ui.throttle = function ( func, wait ) {
+       var context, args, timeout,
+               previous = 0,
+               run = function () {
+                       timeout = null;
+                       previous = OO.ui.now();
+                       func.apply( context, args );
+               };
+       return function () {
+               // Check how long it's been since the last time the function was
+               // called, and whether it's more or less than the requested 
throttle
+               // period. If it's less, run the function immediately. If it's 
more,
+               // set a timeout for the remaining time -- but don't replace an
+               // existing timeout, since that'd indefinitely prolong the wait.
+               var remaining = wait - ( OO.ui.now() - previous );
+               context = this;
+               args = arguments;
+               if ( remaining <= 0 ) {
+                       // Note: unless wait was ridiculously large, this means 
we'll
+                       // automatically run the first time the function was 
called in a
+                       // given period. (If you provide a wait period larger 
than the
+                       // current Unix timestamp, you *deserve* unexpected 
behavior.)
+                       clearTimeout( timeout );
+                       run();
+               } else if ( !timeout ) {
+                       timeout = setTimeout( run, remaining );
+               }
+       };
+};
+
+/**
+ * A (possibly faster) way to get the current timestamp as an integer
+ *
+ * @return {number} Current timestamp
+ */
+OO.ui.now = Date.now || function () {
+       return new Date().getTime();
+};
+
+/**
  * Proxy for `node.addEventListener( eventName, handler, true )`.
  *
  * @param {HTMLElement} node

-- 
To view, visit https://gerrit.wikimedia.org/r/275913
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic4bb40d0d6f57420f2582e33636100c2342707b2
Gerrit-PatchSet: 3
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: DLynch <[email protected]>
Gerrit-Reviewer: Bartosz DziewoƄski <[email protected]>
Gerrit-Reviewer: DLynch <[email protected]>
Gerrit-Reviewer: Divec <[email protected]>
Gerrit-Reviewer: Esanders <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to