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; | |
|
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
| |
| 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 // Issue first call immediately if no process is running. | |
| 43 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
| |
| 44 | |
| 45 if (this._isRunningProcess) | |
| 46 return; | |
| 47 | |
| 48 this._innerSchedule(); | |
| 49 }, | |
| 50 | |
| 51 _innerSchedule: function() | |
| 52 { | |
| 53 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
| |
| 54 return; | |
| 55 var timeout = this._asSoonAsPossible ? 0 : this._timeout; | |
| 56 this._asSoonAsPossible = false; | |
| 57 this._processTimeout = setTimeout(this._onTimeout.bind(this), timeout); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 /** @typedef {function()} */ | |
| 62 WebInspector.Throttler.FinishCallback; | |
| OLD | NEW |