Web Inspector: Fix the debounce function
authortimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 18 Apr 2016 17:28:36 +0000 (17:28 +0000)
committertimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 18 Apr 2016 17:28:36 +0000 (17:28 +0000)
https://bugs.webkit.org/show_bug.cgi?id=156696
rdar://problem/25778133

Reviewed by Brian Burg.

* UserInterface/Base/Utilities.js:
(Function.prototype.debounce): Store the timeout on the original function instead
of the bound function. Also simplify the implementation with arrow functions
and eliminate the bind altogether.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@199676 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Base/Utilities.js

index e139148..a58cbc8 100644 (file)
@@ -1,3 +1,17 @@
+2016-04-18  Timothy Hatcher  <timothy@apple.com>
+
+        Web Inspector: Fix the debounce function
+
+        https://bugs.webkit.org/show_bug.cgi?id=156696
+        rdar://problem/25778133
+
+        Reviewed by Brian Burg.
+
+        * UserInterface/Base/Utilities.js:
+        (Function.prototype.debounce): Store the timeout on the original function instead
+        of the bound function. Also simplify the implementation with arrow functions
+        and eliminate the bind altogether.
+
 2016-04-16  Matt Baker  <mattbaker@apple.com>
 
         Web Inspector: Adopt Number.prototype.toLocaleString For All Sizes and Times
index 622d3ff..dce4334 100644 (file)
@@ -1194,17 +1194,23 @@ Object.defineProperty(Array.prototype, "binaryIndexOf",
 });
 
 (function() {
+    // The `debounce` function lets you call a function with a delay and
+    // if the function keeps getting called, the delay gets reset.
+    // Note: The last call's arguments end being the ones that get used.
+    // Use: foo.bar.debounce(200, foo)("Argument 1", "Argument 2")
+
     const debounceSymbol = Symbol("function-debounce-timeout");
+
     Object.defineProperty(Function.prototype, "debounce",
     {
         value: function(delay, thisObject)
         {
-            let callback = this.bind(thisObject);
-            return function() {
-                clearTimeout(callback[debounceSymbol]);
+            return () => {
+                clearTimeout(this[debounceSymbol]);
+
                 let args = arguments;
-                callback[debounceSymbol] = setTimeout(() => {
-                    callback.apply(null, args);
+                this[debounceSymbol] = setTimeout(() => {
+                    this.apply(thisObject, args);
                 }, delay);
             };
         }