You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

37 lines
892 B

var domain = require('domain');
function CallbackFiller() {
this.queues = {};
}
CallbackFiller.prototype.fill = function(key, err, data) {
var self = this;
var waiting = self.queues[key];
delete self.queues[key];
waiting.forEach(function(task) {
var taskDomain = task.domain || domain.create();
if (typeof task.cb === 'object') {
if (err) {
return taskDomain.bind(task.cb.reject)(err);
}
return taskDomain.bind(task.cb.resolve)(data);
}
taskDomain.bind(task.cb)(err, data);
});
};
CallbackFiller.prototype.has = function(key) {
return this.queues[key];
};
CallbackFiller.prototype.add = function(key, funcObj) {
if (this.queues[key]) {
this.queues[key].push(funcObj);
} else {
this.queues[key] = [funcObj];
}
};
module.exports = CallbackFiller;