Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Unified Diff: Source/devtools/front_end/common/Throttler.js

Issue 319143002: DevTools: introduce WebInspector.Throttler (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: fix testcase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/devtools/devtools.gypi ('k') | Source/devtools/front_end/inspector.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..fc6803a9538a5a16874bdf1bbb1968c9d9c85ac8
--- /dev/null
+++ b/Source/devtools/front_end/common/Throttler.js
@@ -0,0 +1,70 @@
+// 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)
aandrey 2014/06/10 15:08:38 IMO, the most common use case in our code is: send
+{
+ this._timeout = timeout;
+ this._isRunningProcess = false;
+ this._processTimeout = null;
aandrey 2014/06/10 14:59:53 remove
+ this._asSoonAsPossible = false;
+ this._process = null;
aandrey 2014/06/10 14:59:53 jsdoc
lushnikov 2014/06/10 16:05:09 Done.
+}
+
+WebInspector.Throttler.prototype = {
+ _processCompleted: function()
+ {
+ this._isRunningProcess = false;
+ if (this._process)
+ this._innerSchedule();
+ },
+
+ _onTimeout: function()
+ {
+ this._processTimeout = null;
aandrey 2014/06/10 14:59:53 delete this._processTimeout;
lushnikov 2014/06/10 16:05:09 Done.
+ this._asSoonAsPossible = false;
+ this._isRunningProcess = true;
+
+ // Process might issue synchronous calls to this throttler.
+ var process = this._process;
+ this._process = null;
+ process(this._processCompleted.bind(this));
+ },
+
+ /**
+ * @param {function(!WebInspector.Throttler.FinishCallback)} process
+ * @param {boolean=} asSoonAsPossible
+ */
+ schedule: function(process, asSoonAsPossible)
+ {
+ // Deliberately skip previous process.
+ this._process = process;
vsevik 2014/06/10 11:39:00 var force = asSoonAsPossible && !this._asSoonAsPos
lushnikov 2014/06/10 16:05:09 Done.
+ this._asSoonAsPossible = this._asSoonAsPossible || !!asSoonAsPossible;
+
+ if (this._isRunningProcess)
+ return;
+
+ this._innerSchedule();
+ },
+
+ _innerSchedule: function()
+ {
+ if (this._processTimeout && !this._asSoonAsPossible)
vsevik 2014/06/10 11:39:00 if (this._processTimeout && !force) return;
lushnikov 2014/06/10 16:05:09 Done.
+ return;
+ if (this._processTimeout && this._asSoonAsPossible)
+ clearTimeout(this._processTimeout);
+
+ // Pretend the process is running to avoid timer rescheduling.
+ this._isRunningProcess = this._asSoonAsPossible;
dgozman 2014/06/09 19:43:11 If you clear |this._asSoonAsPossible| here: - no n
lushnikov 2014/06/10 09:05:22 1. Consider having for-loop, which does throttler.
+
+ var timeout = this._asSoonAsPossible ? 0 : this._timeout;
+ this._processTimeout = setTimeout(this._onTimeout.bind(this), timeout);
+ }
+}
+
+/** @typedef {function()} */
+WebInspector.Throttler.FinishCallback;
« no previous file with comments | « Source/devtools/devtools.gypi ('k') | Source/devtools/front_end/inspector.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698