Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @constructor | |
| 7 * @param {number} timeout | |
| 8 */ | |
| 9 WebInspector.Throttler = function(timeout) | |
| 10 { | |
| 11 this._timeout = timeout; | |
| 12 this._isRunningProcess = false; | |
| 13 this._somethingScheduled = false; | |
| 14 this._asSoonAsPossible = false; | |
| 15 } | |
| 16 | |
| 17 WebInspector.Throttler.prototype = { | |
| 18 _processCompleted: function() | |
| 19 { | |
| 20 this._isRunningProcess = false; | |
| 21 if (this._somethingScheduled) | |
| 22 this._innerSchedule(); | |
| 23 }, | |
| 24 | |
| 25 _onTimeout: function() | |
| 26 { | |
| 27 this._processTimeout = null; | |
| 28 this._somethingScheduled = false; | |
| 29 this._isRunningProcess = true; | |
| 30 this._process(this._processCompleted.bind(this)); | |
| 31 }, | |
| 32 | |
| 33 /** | |
| 34 * @param {function(!WebInspector.Throttler.FinishCallback)} process | |
| 35 * @param {boolean=} asSoonAsPossible | |
| 36 */ | |
| 37 schedule: function(process, asSoonAsPossible) | |
| 38 { | |
| 39 this._somethingScheduled = true; | |
| 40 // Deliberately skip previous process. | |
| 41 this._process = process; | |
| 42 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
| |
| 43 | |
| 44 if (this._isRunningProcess) | |
| 45 return; | |
| 46 | |
| 47 this._innerSchedule(); | |
| 48 }, | |
| 49 | |
| 50 _innerSchedule: function() | |
| 51 { | |
| 52 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
| |
| 53 return; | |
| 54 var timeout = this._asSoonAsPossible ? 0 : this._timeout; | |
| 55 this._asSoonAsPossible = false; | |
| 56 this._processTimeout = setTimeout(this._onTimeout.bind(this), timeout); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 /** @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.
| |
| 61 WebInspector.Throttler.FinishCallback; | |
| OLD | NEW |