Index: Source/devtools/front_end/common/Throttler.js |
diff --git a/Source/devtools/front_end/common/Throttler.js b/Source/devtools/front_end/common/Throttler.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2a3a8969dba4125c51f172f7b34fa50008bac0c2 |
--- /dev/null |
+++ b/Source/devtools/front_end/common/Throttler.js |
@@ -0,0 +1,47 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+/** |
+ * @constructor |
+ * @param {number} timeout |
+ */ |
+WebInspector.Throttler = function(timeout) |
+{ |
+ this._timeout = timeout; |
+ this._isRunningProcess = false; |
+ this._somethingScheduled = false; |
+} |
+ |
+WebInspector.Throttler.prototype = { |
+ _processCompleted: function() |
+ { |
+ this._isRunningProcess = false; |
+ if (this._somethingScheduled) |
+ this.schedule(this._process); |
+ }, |
+ |
+ _onProcessTimeout: function() |
+ { |
+ this._processTimeout = null; |
+ this._somethingScheduled = false; |
+ this._isRunningProcess = true; |
+ this._process(this._processCompleted.bind(this)); |
+ }, |
+ |
+ /** |
+ * @param {function(function())} process |
+ */ |
+ schedule: function(process) |
+ { |
+ this._somethingScheduled = true; |
+ this._process = process; |
+ |
+ if (this._isRunningProcess) |
+ return; |
+ |
+ if (!this._processTimeout) |
+ this._processTimeout = setTimeout(this._onProcessTimeout.bind(this), this._timeout); |
+ } |
+} |
+ |