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 = {}; |
11 | 11 |
12 /** | 12 /** |
13 * Creates a class for executing several asynchronous closures in a fifo queue. | 13 * Creates a class for executing several asynchronous closures in a fifo queue. |
14 * Added tasks will be executed sequentially in order they were added. | 14 * Added tasks will be executed sequentially in order they were added. |
15 * | 15 * |
| 16 * Once the queue is cancelled, any pending tasks in the queue as well as later |
| 17 * added ones will not be executed. The current one being executed will not be |
| 18 * terminated. |
| 19 * |
16 * @constructor | 20 * @constructor |
17 */ | 21 */ |
18 AsyncUtil.Queue = function() { | 22 AsyncUtil.Queue = function() { |
19 this.running_ = false; | 23 this.running_ = false; |
20 this.closures_ = []; | 24 this.cancelled_ = false; |
| 25 this.tasks_ = []; |
| 26 }; |
| 27 |
| 28 /** |
| 29 * Cancels the queue. Any pending and later added tasks will not be executed. |
| 30 * If there is a task being executed, then it will not be terminated. Once |
| 31 * the queue is cancelled, it can't be resumed. |
| 32 */ |
| 33 AsyncUtil.Queue.prototype.cancel = function() { |
| 34 this.cancelled_ = true; |
| 35 this.running_ = false; |
| 36 for (var index = 0; index < this.tasks_.length; index++) { |
| 37 this.tasks_[index].onCancelled(); |
| 38 } |
| 39 this.tasks_ = []; |
| 40 }; |
| 41 |
| 42 /** |
| 43 * Returns true if the queue is cancelled. Once cancelled, it can't be resumed. |
| 44 * @return {boolean} True if cancelled, false otherwise. |
| 45 */ |
| 46 AsyncUtil.Queue.prototype.isCancelled = function() { |
| 47 return this.cancelled_; |
21 }; | 48 }; |
22 | 49 |
23 /** | 50 /** |
24 * Enqueues a closure to be executed. | 51 * Enqueues a closure to be executed. |
| 52 * |
25 * @param {function(function())} closure Closure with a completion callback to | 53 * @param {function(function())} closure Closure with a completion callback to |
26 * be executed. | 54 * be executed. |
| 55 * @param {function()=} opt_onCancelled Called when the task hasn't been |
| 56 * executed because the queue has been terminated. |
27 */ | 57 */ |
28 AsyncUtil.Queue.prototype.run = function(closure) { | 58 AsyncUtil.Queue.prototype.run = function(closure, opt_onCancelled) { |
29 this.closures_.push(closure); | 59 var onCancelled = opt_onCancelled || function() {}; |
| 60 |
| 61 if (this.cancelled_) { |
| 62 onCancelled(); |
| 63 return; |
| 64 } |
| 65 |
| 66 this.tasks_.push({closure: closure, onCancelled: onCancelled}); |
30 if (!this.running_) | 67 if (!this.running_) |
31 this.continue_(); | 68 this.continue_(); |
32 }; | 69 }; |
33 | 70 |
34 /** | 71 /** |
35 * Serves the next closure from the queue. | 72 * Serves the next closure from the queue. |
36 * @private | 73 * @private |
37 */ | 74 */ |
38 AsyncUtil.Queue.prototype.continue_ = function() { | 75 AsyncUtil.Queue.prototype.continue_ = function() { |
39 if (!this.closures_.length) { | 76 if (!this.tasks_.length) { |
40 this.running_ = false; | 77 this.running_ = false; |
41 return; | 78 return; |
42 } | 79 } |
43 | 80 |
44 // Run the next closure. | 81 // Run the next closure. |
45 this.running_ = true; | 82 if (!this.cancelled_) { |
46 var closure = this.closures_.shift(); | 83 this.running_ = true; |
47 closure(this.continue_.bind(this)); | 84 var task = this.tasks_.shift(); |
| 85 task.closure(this.continue_.bind(this)); |
| 86 } |
48 }; | 87 }; |
49 | 88 |
50 /** | 89 /** |
51 * Creates a class for executing several asynchronous closures in a group in | 90 * Creates a class for executing several asynchronous closures in a group in |
52 * a dependency order. | 91 * a dependency order. |
53 * | 92 * |
54 * @constructor | 93 * @constructor |
55 */ | 94 */ |
56 AsyncUtil.Group = function() { | 95 AsyncUtil.Group = function() { |
57 this.addedTasks_ = {}; | 96 this.addedTasks_ = {}; |
(...skipping 19 matching lines...) Expand all Loading... |
77 closure: closure, | 116 closure: closure, |
78 dependencies: opt_dependencies || [], | 117 dependencies: opt_dependencies || [], |
79 name: name | 118 name: name |
80 }; | 119 }; |
81 | 120 |
82 this.addedTasks_[name] = task; | 121 this.addedTasks_[name] = task; |
83 this.pendingTasks_[name] = task; | 122 this.pendingTasks_[name] = task; |
84 }; | 123 }; |
85 | 124 |
86 /** | 125 /** |
87 * Runs the enqueued closured in order of dependencies. | 126 * Runs the enqueued closures in order of dependencies. |
88 * | 127 * |
89 * @param {function()=} opt_onCompletion Completion callback. | 128 * @param {function()=} opt_onCompletion Completion callback. |
90 */ | 129 */ |
91 AsyncUtil.Group.prototype.run = function(opt_onCompletion) { | 130 AsyncUtil.Group.prototype.run = function(opt_onCompletion) { |
92 if (opt_onCompletion) | 131 if (opt_onCompletion) |
93 this.completionCallbacks_.push(opt_onCompletion); | 132 this.completionCallbacks_.push(opt_onCompletion); |
94 this.continue_(); | 133 this.continue_(); |
95 }; | 134 }; |
96 | 135 |
97 /** | 136 /** |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 /** | 245 /** |
207 * Cancels all scheduled runs (if any). | 246 * Cancels all scheduled runs (if any). |
208 * @private | 247 * @private |
209 */ | 248 */ |
210 AsyncUtil.Aggregation.prototype.cancelScheduledRuns_ = function() { | 249 AsyncUtil.Aggregation.prototype.cancelScheduledRuns_ = function() { |
211 if (this.scheduledRunsTimer_) { | 250 if (this.scheduledRunsTimer_) { |
212 clearTimeout(this.scheduledRunsTimer_); | 251 clearTimeout(this.scheduledRunsTimer_); |
213 this.scheduledRunsTimer_ = null; | 252 this.scheduledRunsTimer_ = null; |
214 } | 253 } |
215 }; | 254 }; |
OLD | NEW |