-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeferred.js
189 lines (170 loc) · 3.71 KB
/
deferred.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
doh.Deferred = function(canceller){
this.chain = [];
this.id = this._nextId();
this.fired = -1;
this.paused = 0;
this.results = [null, null];
this.canceller = canceller;
this.silentlyCancelled = false;
};
doh.util.extend(doh.Deferred, {
getTestErrback: function(cb, scope){
// summary: Replaces outer getTextCallback's in nested situations to avoid multiple callback(true)'s
var _this = this;
return function(){
try{
cb.apply(scope||doh.util.global||_this, arguments);
}catch(e){
_this.errback(e);
}
};
},
getTestCallback: function(cb, scope){
var _this = this;
return function(){
try{
cb.apply(scope||doh.util.global||_this, arguments);
}catch(e){
_this.errback(e);
return;
}
_this.callback(true);
};
},
getFunctionFromArgs: function(){
var a = arguments;
if((a[0])&&(!a[1])){
if(typeof a[0] == "function"){
return a[0];
}else if(typeof a[0] == "string"){
return doh.util.global[a[0]];
}
}else if((a[0])&&(a[1])){
return doh.util.hitch(a[0], a[1]);
}
return null;
},
makeCalled: function() {
var deferred = new doh.Deferred();
deferred.callback();
return deferred;
},
_nextId: (function(){
var n = 1;
return function(){ return n++; };
})(),
cancel: function(){
if(this.fired == -1){
if (this.canceller){
this.canceller(this);
}else{
this.silentlyCancelled = true;
}
if(this.fired == -1){
this.errback(new Error("Deferred(unfired)"));
}
}else if(this.fired == 0 &&
(this.results[0] instanceof doh.Deferred)){
this.results[0].cancel();
}
},
_pause: function(){
this.paused++;
},
_unpause: function(){
this.paused--;
if ((this.paused == 0) && (this.fired >= 0)) {
this._fire();
}
},
_continue: function(res){
this._resback(res);
this._unpause();
},
_resback: function(res){
this.fired = ((res instanceof Error) ? 1 : 0);
this.results[this.fired] = res;
this._fire();
},
_check: function(){
if(this.fired != -1){
if(!this.silentlyCancelled){
throw new Error("already called!");
}
this.silentlyCancelled = false;
return;
}
},
callback: function(res){
this._check();
this._resback(res);
},
errback: function(res){
this._check();
if(!(res instanceof Error)){
res = new Error(res);
}
this._resback(res);
},
addBoth: function(cb, cbfn){
var enclosed = this.getFunctionFromArgs(cb, cbfn);
if(arguments.length > 2){
enclosed = doh.util.hitch(null, enclosed, arguments, 2);
}
return this.addCallbacks(enclosed, enclosed);
},
addCallback: function(cb, cbfn){
var enclosed = this.getFunctionFromArgs(cb, cbfn);
if(arguments.length > 2){
enclosed = doh.util.hitch(null, enclosed, arguments, 2);
}
return this.addCallbacks(enclosed, null);
},
addErrback: function(cb, cbfn){
var enclosed = this.getFunctionFromArgs(cb, cbfn);
if(arguments.length > 2){
enclosed = doh.util.hitch(null, enclosed, arguments, 2);
}
return this.addCallbacks(null, enclosed);
},
addCallbacks: function(cb, eb){
this.chain.push([cb, eb]);
if(this.fired >= 0){
this._fire();
}
return this;
},
_fire: function(){
var chain = this.chain;
var fired = this.fired;
var res = this.results[fired];
var self = this;
var cb = null;
while(chain.length > 0 && this.paused == 0){
// Array
var pair = chain.shift();
var f = pair[fired];
if(f == null){
continue;
}
try {
res = f(res);
fired = ((res instanceof Error) ? 1 : 0);
if(res instanceof doh.Deferred){
cb = function(res){
self._continue(res);
};
this._pause();
}
}catch(err){
fired = 1;
res = err;
}
}
this.fired = fired;
this.results[fired] = res;
if((cb)&&(this.paused)){
res.addBoth(cb);
}
}
});