-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDeferred.js
161 lines (130 loc) · 2.6 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
/**
*
* @param [fn]
* @returns {*}
* @constructor
*/
var Deferred = function(fn) {
if(!(this instanceof Deferred)) {
return (new Deferred).success();
}
this._state = Deferred.State.PENDING;
this.data = null;
this.error = null;
this.chain = [];
this.final = null;
this.finalized = false;
this.fn = fn;
this.n_resolved = 0;
this.n_total = 0;
};
Deferred.State = {
PENDING: 0,
SUCCESS: 1,
ERROR: 2
};
Deferred.prototype.__defineSetter__('state', function(state) {
if(this._state != Deferred.State.PENDING) {
// State is already set, can't reverse it!
} else {
this._state = state;
this.fire();
}
});
Deferred.prototype.__defineGetter__('state', function() {
return this._state;
});
Deferred.prototype.resolveChild = function() {
this.n_resolved++;
this.tryFinalize();
};
Deferred.prototype.tryFinalize = function() {
if(this.finalized)
return;
if(this.state == Deferred.State.SUCCESS
&& this.n_resolved == this.n_total) {
if(this.final) {
this.finalized = true;
this.final(this.data);
}
}
};
Deferred.prototype.fire = function() {
if(this.state == Deferred.State.SUCCESS) {
for(var i = 0; i < this.chain.length; i++) {
this.chain[i](this.data);
}
this.chain = [];
} else if(this.state == Deferred.State.ERROR) {
// this.onerror ...
} else {
// Do nothing
}
this.tryFinalize();
};
Deferred.prototype.finally = function(action) {
var p = new Deferred(action);
var parent = this;
this.final = function() {
if(typeof action == "function") {
p.success(action(parent.data));
} else {
p.success(action);
}
};
this.tryFinalize();
return p;
};
Deferred.prototype.then = function(action) {
return this.onload(action);
};
Deferred.prototype.onload = function(fn) {
var p;
if(fn instanceof Deferred) {
p = new Deferred(function() {
return fn;
});
}
else {
p = new Deferred(fn);
}
var parent = this;
this.n_total++;
this.chain.push(function(data) {
var p2 = p.fn(data);
if(p2 instanceof Deferred) {
// Queue next chain if deferred
p2.onload(function(data) {
p.success(data);
parent.resolveChild();
});
} else {
// Or just pass on value
p.success(p2);
parent.resolveChild();
}
});
if(this.state != Deferred.State.PENDING)
this.fire();
return p;
};
/**
*
* @param [data]
* @returns {Deferred}
*/
Deferred.prototype.success = function(data) {
this.data = data;
this.state = Deferred.State.SUCCESS;
return this;
};
/**
*
* @param [err]
* @returns {Deferred}
*/
Deferred.prototype.error = function(err) {
this.error = err;
this.state = Deferred.State.ERROR;
return this;
};