OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @constructor | 6 * @constructor |
7 * @param {number} timeout | 7 * @param {number} timeout |
8 */ | 8 */ |
9 WebInspector.Throttler = function(timeout) | 9 WebInspector.Throttler = function(timeout) |
10 { | 10 { |
11 this._timeout = timeout; | 11 this._timeout = timeout; |
12 this._isRunningProcess = false; | 12 this._isRunningProcess = false; |
13 this._asSoonAsPossible = false; | 13 this._asSoonAsPossible = false; |
14 /** @type {?function():(!Promise.<?>)} */ | 14 /** @type {?function():(!Promise.<?>)} */ |
15 this._process = null; | 15 this._process = null; |
| 16 this._lastCompleteTime = 0; |
16 } | 17 } |
17 | 18 |
18 WebInspector.Throttler.prototype = { | 19 WebInspector.Throttler.prototype = { |
19 _processCompleted: function() | 20 _processCompleted: function() |
20 { | 21 { |
| 22 this._lastCompleteTime = window.performance.now(); |
21 this._isRunningProcess = false; | 23 this._isRunningProcess = false; |
22 if (this._process) | 24 if (this._process) |
23 this._innerSchedule(false); | 25 this._innerSchedule(false); |
24 this._processCompletedForTests(); | 26 this._processCompletedForTests(); |
25 }, | 27 }, |
26 | 28 |
27 _processCompletedForTests: function() | 29 _processCompletedForTests: function() |
28 { | 30 { |
29 // For sniffing in tests. | 31 // For sniffing in tests. |
30 }, | 32 }, |
(...skipping 15 matching lines...) Expand all Loading... |
46 * @param {function():(!Promise.<?>)} process | 48 * @param {function():(!Promise.<?>)} process |
47 * @param {boolean=} asSoonAsPossible | 49 * @param {boolean=} asSoonAsPossible |
48 */ | 50 */ |
49 schedule: function(process, asSoonAsPossible) | 51 schedule: function(process, asSoonAsPossible) |
50 { | 52 { |
51 // Deliberately skip previous process. | 53 // Deliberately skip previous process. |
52 this._process = process; | 54 this._process = process; |
53 | 55 |
54 // Run the first scheduled task instantly. | 56 // Run the first scheduled task instantly. |
55 var hasScheduledTasks = !!this._processTimeout || this._isRunningProcess
; | 57 var hasScheduledTasks = !!this._processTimeout || this._isRunningProcess
; |
56 asSoonAsPossible = !!asSoonAsPossible || !hasScheduledTasks; | 58 var okToFire = window.performance.now() - this._lastCompleteTime > this.
_timeout; |
| 59 asSoonAsPossible = !!asSoonAsPossible || (!hasScheduledTasks && okToFire
); |
57 | 60 |
58 var forceTimerUpdate = asSoonAsPossible && !this._asSoonAsPossible; | 61 var forceTimerUpdate = asSoonAsPossible && !this._asSoonAsPossible; |
59 this._asSoonAsPossible = this._asSoonAsPossible || asSoonAsPossible; | 62 this._asSoonAsPossible = this._asSoonAsPossible || asSoonAsPossible; |
60 | 63 |
61 this._innerSchedule(forceTimerUpdate); | 64 this._innerSchedule(forceTimerUpdate); |
62 }, | 65 }, |
63 | 66 |
| 67 flush: function() |
| 68 { |
| 69 if (this._process) |
| 70 this._onTimeout(); |
| 71 }, |
| 72 |
64 /** | 73 /** |
65 * @param {boolean} forceTimerUpdate | 74 * @param {boolean} forceTimerUpdate |
66 */ | 75 */ |
67 _innerSchedule: function(forceTimerUpdate) | 76 _innerSchedule: function(forceTimerUpdate) |
68 { | 77 { |
69 if (this._isRunningProcess) | 78 if (this._isRunningProcess) |
70 return; | 79 return; |
71 if (this._processTimeout && !forceTimerUpdate) | 80 if (this._processTimeout && !forceTimerUpdate) |
72 return; | 81 return; |
73 if (this._processTimeout) | 82 if (this._processTimeout) |
(...skipping 17 matching lines...) Expand all Loading... |
91 * @return {number} | 100 * @return {number} |
92 */ | 101 */ |
93 _setTimeout: function(operation, timeout) | 102 _setTimeout: function(operation, timeout) |
94 { | 103 { |
95 return setTimeout(operation, timeout); | 104 return setTimeout(operation, timeout); |
96 } | 105 } |
97 } | 106 } |
98 | 107 |
99 /** @typedef {function(!Error=)} */ | 108 /** @typedef {function(!Error=)} */ |
100 WebInspector.Throttler.FinishCallback; | 109 WebInspector.Throttler.FinishCallback; |
OLD | NEW |