// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-'use strict';
-
/**
* Namespace for async utility functions.
*/
* sequentially done.
*
* @param {Array.<T>} array The array to be iterated.
- * @param {function(function(), T, number, Array.<T>} callback The iteration
+ * @param {function(function(), T, number, Array.<T>)} callback The iteration
* callback. The first argument is a callback to notify the completion of
* the iteration.
* @param {function()} completionCallback Called when all iterations are
};
/**
- * @return {boolean} Number of running tasks.
+ * @return {number} Number of running tasks.
*/
AsyncUtil.ConcurrentQueue.prototype.getRunningTasksCount = function() {
return this.pendingTasks_.length;
};
/**
+ * Returns string representation of current AsyncUtil.ConcurrentQueue instance.
+ * @return {string} String representation of the instance.
+ */
+AsyncUtil.ConcurrentQueue.prototype.toString = function() {
+ return 'AsyncUtil.ConcurrentQueue\n' +
+ '- WaitingTasksCount: ' + this.getWaitingTasksCount() + '\n' +
+ '- RunningTasksCount: ' + this.getRunningTasksCount() + '\n' +
+ '- isCancelled: ' + this.isCancelled();
+};
+
+/**
* Creates a class for executing several asynchronous closures in a fifo queue.
* Added tasks will be executed sequentially in order they were added.
*
};
/**
+ * A task which is executed by AsyncUtil.Group.
+ *
+ * @param {!function(function())} closure Closure with a completion callback to
+ * be executed.
+ * @param {!Array.<string>} dependencies Array of dependencies.
+ * @param {!string} name Task identifier. Specify to use in dependencies.
+ *
+ * @constructor
+ */
+AsyncUtil.GroupTask = function(closure, dependencies, name) {
+ this.closure = closure;
+ this.dependencies = dependencies;
+ this.name = name;
+};
+
+/**
+ * Returns string representation of AsyncUti.GroupTask instance.
+ * @return {string} String representation of the instance.
+ */
+AsyncUtil.GroupTask.prototype.toString = function() {
+ return 'AsyncUtil.GroupTask\n' +
+ '- name: ' + this.name + '\n' +
+ '- dependencies: ' + this.dependencies.join();
+};
+
+/**
* Creates a class for executing several asynchronous closures in a group in
* a dependency order.
*
this.completionCallbacks_ = [];
};
+AsyncUtil.Group.prototype = {
+ /**
+ * @return {Object.<string, AsyncUtil.GroupTask>} Pending tasks
+ */
+ get pendingTasks() {
+ return this.pendingTasks_;
+ }
+};
+
/**
* Enqueues a closure to be executed after dependencies are completed.
*
var length = Object.keys(this.addedTasks_).length;
var name = opt_name || ('(unnamed#' + (length + 1) + ')');
- var task = {
- closure: closure,
- dependencies: opt_dependencies || [],
- name: name
- };
+ var task = new AsyncUtil.GroupTask(closure, opt_dependencies || [], name);
this.addedTasks_[name] = task;
this.pendingTasks_[name] = task;