| 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(!WebInspector.Throttler.FinishCallback)} */ | 14 /** @type {?function():(!Promise.<?>)} */ |
| 15 this._process = null; | 15 this._process = null; |
| 16 } | 16 } |
| 17 | 17 |
| 18 WebInspector.Throttler.prototype = { | 18 WebInspector.Throttler.prototype = { |
| 19 /** | 19 _processCompleted: function() |
| 20 * @param {!Error=} error | |
| 21 */ | |
| 22 _processCompleted: function(error) | |
| 23 { | 20 { |
| 24 if (error) | |
| 25 console.error(error); | |
| 26 this._isRunningProcess = false; | 21 this._isRunningProcess = false; |
| 27 if (this._process) | 22 if (this._process) |
| 28 this._innerSchedule(false); | 23 this._innerSchedule(false); |
| 29 this._processCompletedForTests(); | 24 this._processCompletedForTests(); |
| 30 }, | 25 }, |
| 31 | 26 |
| 32 _processCompletedForTests: function() | 27 _processCompletedForTests: function() |
| 33 { | 28 { |
| 34 // For sniffing in tests. | 29 // For sniffing in tests. |
| 35 }, | 30 }, |
| 36 | 31 |
| 37 _onTimeout: function() | 32 _onTimeout: function() |
| 38 { | 33 { |
| 39 delete this._processTimeout; | 34 delete this._processTimeout; |
| 40 this._asSoonAsPossible = false; | 35 this._asSoonAsPossible = false; |
| 41 this._isRunningProcess = true; | 36 this._isRunningProcess = true; |
| 42 | 37 |
| 43 // Process might issue synchronous calls to this throttler. | 38 Promise.resolve() |
| 44 var process = this._process; | 39 .then(this._process) |
| 40 .catch(console.error.bind(console)) |
| 41 .then(this._processCompleted.bind(this)); |
| 45 this._process = null; | 42 this._process = null; |
| 46 try { | |
| 47 process(this._processCompleted.bind(this)); | |
| 48 } catch (e) { | |
| 49 // Process might have called completed() and threw exception later. | |
| 50 if (this._isRunningProcess) | |
| 51 this._processCompleted(e); | |
| 52 } | |
| 53 }, | 43 }, |
| 54 | 44 |
| 55 /** | 45 /** |
| 56 * @param {function(!WebInspector.Throttler.FinishCallback)} process | 46 * @param {function():(!Promise.<?>)} process |
| 57 * @param {boolean=} asSoonAsPossible | 47 * @param {boolean=} asSoonAsPossible |
| 58 */ | 48 */ |
| 59 schedule: function(process, asSoonAsPossible) | 49 schedule: function(process, asSoonAsPossible) |
| 60 { | 50 { |
| 61 // Deliberately skip previous process. | 51 // Deliberately skip previous process. |
| 62 this._process = process; | 52 this._process = process; |
| 63 | 53 |
| 64 // Run the first scheduled task instantly. | 54 // Run the first scheduled task instantly. |
| 65 var hasScheduledTasks = !!this._processTimeout || this._isRunningProcess
; | 55 var hasScheduledTasks = !!this._processTimeout || this._isRunningProcess
; |
| 66 asSoonAsPossible = !!asSoonAsPossible || !hasScheduledTasks; | 56 asSoonAsPossible = !!asSoonAsPossible || !hasScheduledTasks; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 * @return {number} | 91 * @return {number} |
| 102 */ | 92 */ |
| 103 _setTimeout: function(operation, timeout) | 93 _setTimeout: function(operation, timeout) |
| 104 { | 94 { |
| 105 return setTimeout(operation, timeout); | 95 return setTimeout(operation, timeout); |
| 106 } | 96 } |
| 107 } | 97 } |
| 108 | 98 |
| 109 /** @typedef {function(!Error=)} */ | 99 /** @typedef {function(!Error=)} */ |
| 110 WebInspector.Throttler.FinishCallback; | 100 WebInspector.Throttler.FinishCallback; |
| OLD | NEW |