Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Namespace for async utility functions. | 8 * Namespace for async utility functions. |
| 9 */ | 9 */ |
| 10 var AsyncUtil = {}; | 10 var AsyncUtil = {}; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 * Added tasks will be executed sequentially in order they were added. | 46 * Added tasks will be executed sequentially in order they were added. |
| 47 * | 47 * |
| 48 * @constructor | 48 * @constructor |
| 49 */ | 49 */ |
| 50 AsyncUtil.Queue = function() { | 50 AsyncUtil.Queue = function() { |
| 51 this.running_ = false; | 51 this.running_ = false; |
| 52 this.closures_ = []; | 52 this.closures_ = []; |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * @return {boolean} True when a task is running, otherwise false. | |
| 57 */ | |
| 58 AsyncUtil.Queue.prototype.isRunning = function() { | |
| 59 return this.running_; | |
| 60 }; | |
| 61 | |
| 62 /** | |
| 56 * Enqueues a closure to be executed. | 63 * Enqueues a closure to be executed. |
| 57 * @param {function(function())} closure Closure with a completion callback to | 64 * @param {function(function())} closure Closure with a completion callback to |
| 58 * be executed. | 65 * be executed. |
| 59 */ | 66 */ |
| 60 AsyncUtil.Queue.prototype.run = function(closure) { | 67 AsyncUtil.Queue.prototype.run = function(closure) { |
| 61 this.closures_.push(closure); | 68 this.closures_.push(closure); |
| 62 if (!this.running_) | 69 if (!this.running_) |
| 63 this.continue_(); | 70 this.continue_(); |
| 64 }; | 71 }; |
| 65 | 72 |
| 66 /** | 73 /** |
| 67 * Serves the next closure from the queue. | 74 * Serves the next closure from the queue. |
| 68 * @private | 75 * @private |
| 69 */ | 76 */ |
| 70 AsyncUtil.Queue.prototype.continue_ = function() { | 77 AsyncUtil.Queue.prototype.continue_ = function() { |
| 71 if (!this.closures_.length) { | 78 if (!this.closures_.length) { |
| 72 this.running_ = false; | 79 this.running_ = false; |
| 73 return; | 80 return; |
| 74 } | 81 } |
| 75 | 82 |
| 76 // Run the next closure. | 83 // Run the next closure. |
| 77 this.running_ = true; | 84 this.running_ = true; |
| 78 var closure = this.closures_.shift(); | 85 var closure = this.closures_.shift(); |
| 79 closure(this.continue_.bind(this)); | 86 closure(this.continue_.bind(this)); |
| 80 }; | 87 }; |
| 81 | 88 |
| 82 /** | 89 /** |
| 90 * Cancels all pending tasks. Note that this does NOT cancel the task running | |
| 91 * currently. | |
| 92 */ | |
| 93 AsyncUtil.Queue.prototype.cancel = function() { | |
|
mtomasz
2013/10/01 02:13:44
FYI: A more complete cancellation logic is planned
hidehiko
2013/10/01 04:12:23
Good to know. As we talked offline, please let me
| |
| 94 this.closures_.splice(0, this.closures_.length); | |
|
mtomasz
2013/10/01 02:13:44
nit: How about just this.closures_ = []?
hidehiko
2013/10/01 04:12:23
Done.
| |
| 95 }; | |
| 96 | |
| 97 /** | |
| 83 * Creates a class for executing several asynchronous closures in a group in | 98 * Creates a class for executing several asynchronous closures in a group in |
| 84 * a dependency order. | 99 * a dependency order. |
| 85 * | 100 * |
| 86 * @constructor | 101 * @constructor |
| 87 */ | 102 */ |
| 88 AsyncUtil.Group = function() { | 103 AsyncUtil.Group = function() { |
| 89 this.addedTasks_ = {}; | 104 this.addedTasks_ = {}; |
| 90 this.pendingTasks_ = {}; | 105 this.pendingTasks_ = {}; |
| 91 this.finishedTasks_ = {}; | 106 this.finishedTasks_ = {}; |
| 92 this.completionCallbacks_ = []; | 107 this.completionCallbacks_ = []; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 /** | 253 /** |
| 239 * Cancels all scheduled runs (if any). | 254 * Cancels all scheduled runs (if any). |
| 240 * @private | 255 * @private |
| 241 */ | 256 */ |
| 242 AsyncUtil.Aggregation.prototype.cancelScheduledRuns_ = function() { | 257 AsyncUtil.Aggregation.prototype.cancelScheduledRuns_ = function() { |
| 243 if (this.scheduledRunsTimer_) { | 258 if (this.scheduledRunsTimer_) { |
| 244 clearTimeout(this.scheduledRunsTimer_); | 259 clearTimeout(this.scheduledRunsTimer_); |
| 245 this.scheduledRunsTimer_ = null; | 260 this.scheduledRunsTimer_ = null; |
| 246 } | 261 } |
| 247 }; | 262 }; |
| OLD | NEW |