$('#gar-portlet').click(gar.callback);
}
loadTaskManager();
});
gar.usersToNotify = [];
var tm = new Morebits.taskManagerXXXtaskManager();
tm.add(gar.tasks.getNumber, []);
tm.add(gar.tasks.createNomPage, [ gar.tasks.getNumber ]);
};
function loadTaskManager() {
Morebits.taskManagerXXX = function() { // named so to avoid naming conflict with real one
this.taskDependencyMap = new Map();
this.deferreds = new Map();
this.allDeferreds = []; // Hack: IE doesn't support Map.prototype.values
/**
* Register a task along with its dependencies (tasks which should have
* finished execution before we can begin this one).
* Each task is a function that must return a promise.
* @param {function} func - a task
* @param {function[]} deps - its dependencies
*/
this.add = function(func, deps) {
this.taskDependencyMap.set(func, deps);
var deferred = $.Deferred();
this.deferreds.set(func, deferred);
this.allDeferreds.push(deferred);
};
/**
* Run all the tasks in correct sequence. Multiple tasks may be run at once.
*/
this.execute = function() {
var self = this; // proxy for `this` for use inside functions where `this` is something else
this.taskDependencyMap.forEach(function(deps, task) {
var depsPromisesArray = deps.map(function(dep) {
return self.deferreds.get(dep);
});
$.when.apply(null, depsPromisesArray).then(function() {
console.log('Executing ' + task.name);
task().then(function() {
console.log(task.name + ' is resolved');
self.deferreds.get(task).resolve();
});
});
});
return $.when.apply(null, this.allDeferreds); // resolved when eveything is done!
};
};
}
/* </nowiki> */
|