Chromium Code Reviews| 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..7e910ee0cb29c8ceaf062bc3875b7ed105ff5910 |
| --- /dev/null |
| +++ b/Source/devtools/front_end/common/Throttler.js |
| @@ -0,0 +1,62 @@ |
| +// 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; |
| + this._asSoonAsPossible = false; |
|
dgozman
2014/06/09 13:17:37
Consider renaming to something like |skipTimeout|.
lushnikov
2014/06/09 15:53:42
That's not really "skipTimeout" - we don't want to
|
| +} |
| + |
| +WebInspector.Throttler.prototype = { |
| + _processCompleted: function() |
| + { |
| + this._isRunningProcess = false; |
| + if (this._somethingScheduled) |
| + this._innerSchedule(); |
| + }, |
| + |
| + _onTimeout: function() |
| + { |
| + this._processTimeout = null; |
| + this._somethingScheduled = false; |
| + this._isRunningProcess = true; |
| + this._process(this._processCompleted.bind(this)); |
| + }, |
| + |
| + /** |
| + * @param {function(!WebInspector.Throttler.FinishCallback)} process |
| + * @param {boolean=} asSoonAsPossible |
| + */ |
| + schedule: function(process, asSoonAsPossible) |
| + { |
| + this._somethingScheduled = true; |
| + // Deliberately skip previous process. |
| + this._process = process; |
| + // Issue first call immediately if no process is running. |
| + this._asSoonAsPossible = this._asSoonAsPossible || !!asSoonAsPossible || !this._isRunningProcess; |
|
dgozman
2014/06/09 13:17:37
Should be !(this._isRunningProcess || this._proces
pfeldman
2014/06/09 13:26:45
This logic seems confusing. So I was scheduling so
lushnikov
2014/06/09 15:53:42
The code here has changed. Is your question still
|
| + |
| + if (this._isRunningProcess) |
| + return; |
| + |
| + this._innerSchedule(); |
| + }, |
| + |
| + _innerSchedule: function() |
| + { |
| + if (this._processTimeout) |
|
dgozman
2014/06/09 13:17:37
I'd rather move this check to the |schedule| funct
lushnikov
2014/06/09 15:53:42
I'd like to leave it as-is, because the innerSched
|
| + return; |
| + var timeout = this._asSoonAsPossible ? 0 : this._timeout; |
| + this._asSoonAsPossible = false; |
| + this._processTimeout = setTimeout(this._onTimeout.bind(this), timeout); |
| + } |
| +} |
| + |
| +/** @typedef {function()} */ |
| +WebInspector.Throttler.FinishCallback; |