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..b5596633a81cc5b69c0fa856dd100b53cb4e1969 |
| --- /dev/null |
| +++ b/Source/devtools/front_end/common/Throttler.js |
| @@ -0,0 +1,61 @@ |
| +// 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; |
| +} |
| + |
| +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; |
| + this._asSoonAsPossible = this._asSoonAsPossible || !!asSoonAsPossible; |
|
dgozman
2014/06/09 12:03:33
I think, the general usage of throttler is: immedi
lushnikov
2014/06/09 13:07:26
Agreed. Will fix this in next CL
|
| + |
| + if (this._isRunningProcess) |
| + return; |
| + |
| + this._innerSchedule(); |
| + }, |
| + |
| + _innerSchedule: function() |
| + { |
| + if (this._processTimeout) |
|
dgozman
2014/06/09 07:01:51
How can this be non-null?
lushnikov
2014/06/09 09:45:53
It is set to null as the timeout fires. @see _onTi
|
| + return; |
| + var timeout = this._asSoonAsPossible ? 0 : this._timeout; |
| + this._asSoonAsPossible = false; |
| + this._processTimeout = setTimeout(this._onTimeout.bind(this), timeout); |
| + } |
| +} |
| + |
| +/** @typedef {function()} */ |
|
dgozman
2014/06/09 07:01:51
function(function) ?
lushnikov
2014/06/09 09:45:53
Nope, the typedef here is correct.
dgozman
2014/06/09 12:03:33
My bad.
|
| +WebInspector.Throttler.FinishCallback; |