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) | |
|
aandrey
2014/06/10 15:08:38
IMO, the most common use case in our code is: send
| |
| 10 { | |
| 11 this._timeout = timeout; | |
| 12 this._isRunningProcess = false; | |
| 13 this._processTimeout = null; | |
|
aandrey
2014/06/10 14:59:53
remove
| |
| 14 this._asSoonAsPossible = false; | |
| 15 this._process = null; | |
|
aandrey
2014/06/10 14:59:53
jsdoc
lushnikov
2014/06/10 16:05:09
Done.
| |
| 16 } | |
| 17 | |
| 18 WebInspector.Throttler.prototype = { | |
| 19 _processCompleted: function() | |
| 20 { | |
| 21 this._isRunningProcess = false; | |
| 22 if (this._process) | |
| 23 this._innerSchedule(); | |
| 24 }, | |
| 25 | |
| 26 _onTimeout: function() | |
| 27 { | |
| 28 this._processTimeout = null; | |
|
aandrey
2014/06/10 14:59:53
delete this._processTimeout;
lushnikov
2014/06/10 16:05:09
Done.
| |
| 29 this._asSoonAsPossible = false; | |
| 30 this._isRunningProcess = true; | |
| 31 | |
| 32 // Process might issue synchronous calls to this throttler. | |
| 33 var process = this._process; | |
| 34 this._process = null; | |
| 35 process(this._processCompleted.bind(this)); | |
| 36 }, | |
| 37 | |
| 38 /** | |
| 39 * @param {function(!WebInspector.Throttler.FinishCallback)} process | |
| 40 * @param {boolean=} asSoonAsPossible | |
| 41 */ | |
| 42 schedule: function(process, asSoonAsPossible) | |
| 43 { | |
| 44 // Deliberately skip previous process. | |
| 45 this._process = process; | |
|
vsevik
2014/06/10 11:39:00
var force = asSoonAsPossible && !this._asSoonAsPos
lushnikov
2014/06/10 16:05:09
Done.
| |
| 46 this._asSoonAsPossible = this._asSoonAsPossible || !!asSoonAsPossible; | |
| 47 | |
| 48 if (this._isRunningProcess) | |
| 49 return; | |
| 50 | |
| 51 this._innerSchedule(); | |
| 52 }, | |
| 53 | |
| 54 _innerSchedule: function() | |
| 55 { | |
| 56 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.
| |
| 57 return; | |
| 58 if (this._processTimeout && this._asSoonAsPossible) | |
| 59 clearTimeout(this._processTimeout); | |
| 60 | |
| 61 // Pretend the process is running to avoid timer rescheduling. | |
| 62 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.
| |
| 63 | |
| 64 var timeout = this._asSoonAsPossible ? 0 : this._timeout; | |
| 65 this._processTimeout = setTimeout(this._onTimeout.bind(this), timeout); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 /** @typedef {function()} */ | |
| 70 WebInspector.Throttler.FinishCallback; | |
| OLD | NEW |