| 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 | |
| 5 /** | 4 /** |
| 6 * @constructor | 5 * @unrestricted |
| 7 * @param {number} timeout | |
| 8 */ | 6 */ |
| 9 WebInspector.Throttler = function(timeout) | 7 WebInspector.Throttler = class { |
| 10 { | 8 /** |
| 9 * @param {number} timeout |
| 10 */ |
| 11 constructor(timeout) { |
| 11 this._timeout = timeout; | 12 this._timeout = timeout; |
| 12 this._isRunningProcess = false; | 13 this._isRunningProcess = false; |
| 13 this._asSoonAsPossible = false; | 14 this._asSoonAsPossible = false; |
| 14 /** @type {?function():(!Promise.<?>)} */ | 15 /** @type {?function():(!Promise.<?>)} */ |
| 15 this._process = null; | 16 this._process = null; |
| 16 this._lastCompleteTime = 0; | 17 this._lastCompleteTime = 0; |
| 17 }; | 18 } |
| 18 | 19 |
| 19 WebInspector.Throttler.prototype = { | 20 _processCompleted() { |
| 20 _processCompleted: function() | 21 this._lastCompleteTime = window.performance.now(); |
| 21 { | 22 this._isRunningProcess = false; |
| 22 this._lastCompleteTime = window.performance.now(); | 23 if (this._process) |
| 23 this._isRunningProcess = false; | 24 this._innerSchedule(false); |
| 24 if (this._process) | 25 this._processCompletedForTests(); |
| 25 this._innerSchedule(false); | 26 } |
| 26 this._processCompletedForTests(); | |
| 27 }, | |
| 28 | 27 |
| 29 _processCompletedForTests: function() | 28 _processCompletedForTests() { |
| 30 { | 29 // For sniffing in tests. |
| 31 // For sniffing in tests. | 30 } |
| 32 }, | |
| 33 | 31 |
| 34 _onTimeout: function() | 32 _onTimeout() { |
| 35 { | 33 delete this._processTimeout; |
| 36 delete this._processTimeout; | 34 this._asSoonAsPossible = false; |
| 37 this._asSoonAsPossible = false; | 35 this._isRunningProcess = true; |
| 38 this._isRunningProcess = true; | |
| 39 | 36 |
| 40 Promise.resolve() | 37 Promise.resolve().then(this._process).catch(console.error.bind(console)).the
n(this._processCompleted.bind(this)); |
| 41 .then(this._process) | 38 this._process = null; |
| 42 .catch(console.error.bind(console)) | 39 } |
| 43 .then(this._processCompleted.bind(this)); | |
| 44 this._process = null; | |
| 45 }, | |
| 46 | 40 |
| 47 /** | 41 /** |
| 48 * @param {function():(!Promise.<?>)} process | 42 * @param {function():(!Promise.<?>)} process |
| 49 * @param {boolean=} asSoonAsPossible | 43 * @param {boolean=} asSoonAsPossible |
| 50 */ | 44 */ |
| 51 schedule: function(process, asSoonAsPossible) | 45 schedule(process, asSoonAsPossible) { |
| 52 { | 46 // Deliberately skip previous process. |
| 53 // Deliberately skip previous process. | 47 this._process = process; |
| 54 this._process = process; | |
| 55 | 48 |
| 56 // Run the first scheduled task instantly. | 49 // Run the first scheduled task instantly. |
| 57 var hasScheduledTasks = !!this._processTimeout || this._isRunningProcess
; | 50 var hasScheduledTasks = !!this._processTimeout || this._isRunningProcess; |
| 58 var okToFire = window.performance.now() - this._lastCompleteTime > this.
_timeout; | 51 var okToFire = window.performance.now() - this._lastCompleteTime > this._tim
eout; |
| 59 asSoonAsPossible = !!asSoonAsPossible || (!hasScheduledTasks && okToFire
); | 52 asSoonAsPossible = !!asSoonAsPossible || (!hasScheduledTasks && okToFire); |
| 60 | 53 |
| 61 var forceTimerUpdate = asSoonAsPossible && !this._asSoonAsPossible; | 54 var forceTimerUpdate = asSoonAsPossible && !this._asSoonAsPossible; |
| 62 this._asSoonAsPossible = this._asSoonAsPossible || asSoonAsPossible; | 55 this._asSoonAsPossible = this._asSoonAsPossible || asSoonAsPossible; |
| 63 | 56 |
| 64 this._innerSchedule(forceTimerUpdate); | 57 this._innerSchedule(forceTimerUpdate); |
| 65 }, | 58 } |
| 66 | 59 |
| 67 flush: function() | 60 flush() { |
| 68 { | 61 if (this._process) |
| 69 if (this._process) | 62 this._onTimeout(); |
| 70 this._onTimeout(); | 63 } |
| 71 }, | |
| 72 | 64 |
| 73 /** | 65 /** |
| 74 * @param {boolean} forceTimerUpdate | 66 * @param {boolean} forceTimerUpdate |
| 75 */ | 67 */ |
| 76 _innerSchedule: function(forceTimerUpdate) | 68 _innerSchedule(forceTimerUpdate) { |
| 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) | 74 this._clearTimeout(this._processTimeout); |
| 83 this._clearTimeout(this._processTimeout); | |
| 84 | 75 |
| 85 var timeout = this._asSoonAsPossible ? 0 : this._timeout; | 76 var timeout = this._asSoonAsPossible ? 0 : this._timeout; |
| 86 this._processTimeout = this._setTimeout(this._onTimeout.bind(this), time
out); | 77 this._processTimeout = this._setTimeout(this._onTimeout.bind(this), timeout)
; |
| 87 }, | 78 } |
| 88 | 79 |
| 89 /** | 80 /** |
| 90 * @param {number} timeoutId | 81 * @param {number} timeoutId |
| 91 */ | 82 */ |
| 92 _clearTimeout: function(timeoutId) | 83 _clearTimeout(timeoutId) { |
| 93 { | 84 clearTimeout(timeoutId); |
| 94 clearTimeout(timeoutId); | 85 } |
| 95 }, | |
| 96 | 86 |
| 97 /** | 87 /** |
| 98 * @param {function()} operation | 88 * @param {function()} operation |
| 99 * @param {number} timeout | 89 * @param {number} timeout |
| 100 * @return {number} | 90 * @return {number} |
| 101 */ | 91 */ |
| 102 _setTimeout: function(operation, timeout) | 92 _setTimeout(operation, timeout) { |
| 103 { | 93 return setTimeout(operation, timeout); |
| 104 return setTimeout(operation, timeout); | 94 } |
| 105 } | |
| 106 }; | 95 }; |
| 107 | 96 |
| 108 /** @typedef {function(!Error=)} */ | 97 /** @typedef {function(!Error=)} */ |
| 109 WebInspector.Throttler.FinishCallback; | 98 WebInspector.Throttler.FinishCallback; |
| OLD | NEW |