Index: chrome/browser/resources/file_manager/js/async_util.js |
diff --git a/chrome/browser/resources/file_manager/js/async_util.js b/chrome/browser/resources/file_manager/js/async_util.js |
index 59199eb86c8089e6c7aa0a594b145817943d5816..7649eca7ee8f3f4f5101366f57cb9fb0d6ad5055 100644 |
--- a/chrome/browser/resources/file_manager/js/async_util.js |
+++ b/chrome/browser/resources/file_manager/js/async_util.js |
@@ -13,20 +13,57 @@ var AsyncUtil = {}; |
* Creates a class for executing several asynchronous closures in a fifo queue. |
* Added tasks will be executed sequentially in order they were added. |
* |
+ * Once the queue is cancelled, any pending tasks in the queue as well as later |
+ * added ones will not be executed. The current one being executed will not be |
+ * terminated. |
+ * |
* @constructor |
*/ |
AsyncUtil.Queue = function() { |
this.running_ = false; |
- this.closures_ = []; |
+ this.cancelled_ = false; |
+ this.tasks_ = []; |
+}; |
+ |
+/** |
+ * Cancels the queue. Any pending and later added tasks will not be executed. |
+ * If there is a task being executed, then it will not be terminated. Once |
+ * the queue is cancelled, it can't be resumed. |
+ */ |
+AsyncUtil.Queue.prototype.cancel = function() { |
+ this.cancelled_ = true; |
+ this.running_ = false; |
+ for (var index = 0; index < this.tasks_.length; index++) { |
+ this.tasks_[index].onCancelled(); |
+ } |
+ this.tasks_ = []; |
+}; |
+ |
+/** |
+ * Returns true if the queue is cancelled. Once cancelled, it can't be resumed. |
+ * @return {boolean} True if cancelled, false otherwise. |
+ */ |
+AsyncUtil.Queue.prototype.isCancelled = function() { |
+ return this.cancelled_; |
}; |
/** |
* Enqueues a closure to be executed. |
+ * |
* @param {function(function())} closure Closure with a completion callback to |
* be executed. |
+ * @param {function()=} opt_onCancelled Called when the task hasn't been |
+ * executed because the queue has been terminated. |
*/ |
-AsyncUtil.Queue.prototype.run = function(closure) { |
- this.closures_.push(closure); |
+AsyncUtil.Queue.prototype.run = function(closure, opt_onCancelled) { |
+ var onCancelled = opt_onCancelled || function() {}; |
+ |
+ if (this.cancelled_) { |
+ onCancelled(); |
+ return; |
+ } |
+ |
+ this.tasks_.push({closure: closure, onCancelled: onCancelled}); |
if (!this.running_) |
this.continue_(); |
}; |
@@ -36,15 +73,17 @@ AsyncUtil.Queue.prototype.run = function(closure) { |
* @private |
*/ |
AsyncUtil.Queue.prototype.continue_ = function() { |
- if (!this.closures_.length) { |
+ if (!this.tasks_.length) { |
this.running_ = false; |
return; |
} |
// Run the next closure. |
- this.running_ = true; |
- var closure = this.closures_.shift(); |
- closure(this.continue_.bind(this)); |
+ if (!this.cancelled_) { |
+ this.running_ = true; |
+ var task = this.tasks_.shift(); |
+ task.closure(this.continue_.bind(this)); |
+ } |
}; |
/** |
@@ -84,7 +123,7 @@ AsyncUtil.Group.prototype.add = function(closure, opt_dependencies, opt_name) { |
}; |
/** |
- * Runs the enqueued closured in order of dependencies. |
+ * Runs the enqueued closures in order of dependencies. |
* |
* @param {function()=} opt_onCompletion Completion callback. |
*/ |