-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
171 lines (162 loc) · 5.51 KB
/
index.js
File metadata and controls
171 lines (162 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var underscore = require('underscore')._;
var Event = {
__callback:{},
trigger:function(event){
if(!this.__callback[event])return this;
var args = underscore.toArray(arguments).slice(1);
underscore.each(this.__callback[event],function(val){
val[0].apply(val[1],args);
});
return this;
},
bind:function(event,callback,context){
if(!this.__callback[event])this.__callback[event] = [];
this.__callback[event].push([callback,context]);
return this;
},
unbind:function(event){
this.__callback[event] = null;
return this;
}
}
var Promise = underscore.extend({},{
state:{
unfulfilled:true,
fulfilled:false,
failed:false
},
result:null,
progressValue:null,
getState:function(type){
return this.state[type];
},
isUnfulfilled:function(){
return this.getState('unfulfilled');
},
isFulfilled:function(){
return this.getState('fulfilled');
},
isFailed:function(){
return this.getState('failed');
},
progress:function(name,data){
var p = {
name:name,
value:data
}
this.progressValue = p;
this.trigger('progress',p);
return this;
},
onProgress:function(fn,ctx){
if(!this.isUnfulfilled())return this;
//check if there are any previous progress
var p = this.progressValue;
//if exist, trigger this fn
if(p)fn.call(ctx,p);
//then bind this fn to progress event
this.bind('progress',fn,ctx);
return this;
},
then:function(fulfill,error,context,debug){
if(this.isUnfulfilled()){
if(underscore.isFunction(fulfill))this.bind('fulfill',fulfill,context);
if(underscore.isFunction(error))this.bind('failed',error,context);
}else if(this.isFulfilled()){
if(underscore.isFunction(fulfill))
fulfill.call(context,this.result);
}else if(this.isFailed()){
if(underscore.isFunction(error))
error.call(context,this.result);
}
if(debug == 'debug'){
console.log(this,arguments);
}
return this;
},
resolve:function(state,result){
if(!this.isUnfulfilled())return;
this.result = result;
this.state.unfulfilled = false;
if(state == 'fulfill'){
this.state.fulfilled = true;
}else if(state == 'failed'){
this.state.failed = true;
}
this.unbind('progress');
this.trigger(state,result);
},
fulfill:function(result){
this.resolve('fulfill',result);
return this;
},
fail:function(result){
this.resolve('failed',result);
return this;
},
done:function(fn,c,debug){
if(debug == 'debug'){console.log('enter done')}
return this.then(fn,null,c,debug);
},
rejected:function(fn,c){
return this.then(null,fn,c);
},
always:function(fn,c){
return this.then(fn,fn,c);
}
},Event);
var Driver = {
clone:function(){
var d = JSON.parse(JSON.stringify(Event));
d.trigger= Event.trigger;
d.bind = Event.bind;
return d;
},
getPromise:function(){
var p = JSON.parse(JSON.stringify(Promise));
p = underscore.defaults({},p,Promise);
return p;
},
getDelay:function(time){
var p = this.getPromise();
var t = underscore.delay(function(){
p.fulfill();
},time);
p.destroy = function(){
clearTimeout(t);
}
return p;
},
spreadPromise:function(args){
var p =this.getPromise();
//var args = underscore.toArray(arguments);
var after = underscore.after(args.length,function(){
p.fulfill(val);
})
var val = [];
var e = 0;
underscore.each(args,function(o,i){
var pr;
if(underscore.isFunction(o)){
pr = o();
}else{
pr = o;
}
pr.always(function(){
val[i] = arguments;
var idx = underscore.uniqueId("U_");
p.progress(idx,{
total:args.length,
progress:++e
});
after();
})
})
// p.onProgress(function(data){
// var val = data.value;
// console.log("Spreader progress :",val.progress,"/",val.total);
// })
return p;
}
};
module.exports = Driver;