anyproxy/lib/asyncTaskMgr.js
2014-08-13 17:30:16 +08:00

53 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function asyncTaskMgr(){
var self = this;
self.callbackList = {
sampleName:{
status:0, /* 0,stopped,will not callback / 1,loading / 2,loaded */
result:null,
callbackList:[]
}
}
self.addTask = function(name,cb,action){
if(self.callbackList[name]){
var task = self.callbackList[name];
if(task.status == 2){ //done
cb && cb.apply(null,task.result);
}else if(task.status == 1){ //pending
task.callbackList.push(cb);
}else if(task.status == 0){ //stopped
return; //do nothing
}
}else{
var task;
task = self.callbackList[name] = {
status : 1,
result : null,
callbackList : [cb]
};
action && action.call(null,function(){ //action应该带一个回调
if(arguments && arguments[0] === -1){ //返回第一个参数为-1为停止任务
task.status = 0;
task.callbackList = [];
}else{
task.result = arguments;
task.status = 2;
var tmpCb;
while(tmpCb = task.callbackList.shift()){
tmpCb && tmpCb.apply(null,task.result);
}
}
});
}
}
};
module.exports = asyncTaskMgr;