-
Notifications
You must be signed in to change notification settings - Fork 6
/
adehun.js
201 lines (176 loc) · 5.04 KB
/
adehun.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
190
191
192
193
194
195
196
197
198
199
200
201
"use strict";
var validStates = {
PENDING: 0,
FULFILLED: 1,
REJECTED: 2
};
var isValidState = function (state) {
return ((state === validStates.PENDING) ||
(state === validStates.REJECTED) ||
(state === validStates.FULFILLED));
};
var Utils = {
runAsync: function (fn) {
setTimeout(fn, 0);
},
isFunction: function (val) {
return val && typeof val === "function";
},
isObject: function (val) {
return val && typeof val === "object";
},
isPromise: function (val) {
return val && val.constructor === Adehun;
}
};
var then = function (onFulfilled, onRejected) {
var queuedPromise = new Adehun();
if (Utils.isFunction(onFulfilled)) {
queuedPromise.handlers.fulfill = onFulfilled;
}
if (Utils.isFunction(onRejected)) {
queuedPromise.handlers.reject = onRejected;
}
this.queue.push(queuedPromise);
this.process();
return queuedPromise;
};
var transition = function (state, value) {
if (this.state === state ||
this.state !== validStates.PENDING ||
!isValidState(state) ||
arguments.length !== 2) {
return;
}
this.value = value;
this.state = state;
this.process();
};
var process = function () {
var that = this,
fulfillFallBack = function (value) {
return value;
},
rejectFallBack = function (reason) {
throw reason;
};
if (this.state === validStates.PENDING) {
return;
}
Utils.runAsync(function () {
while (that.queue.length) {
var queuedPromise = that.queue.shift(),
handler = null,
value;
if (that.state === validStates.FULFILLED) {
handler = queuedPromise.handlers.fulfill || fulfillFallBack;
} else if (that.state === validStates.REJECTED) {
handler = queuedPromise.handlers.reject || rejectFallBack;
}
try {
value = handler(that.value);
} catch (e) {
queuedPromise.transition(validStates.REJECTED, e);
continue;
}
Resolve(queuedPromise, value);
}
});
};
function Resolve(promise, x) {
if (promise === x) {
promise.transition(validStates.REJECTED, new TypeError("The promise and its value refer to the same object"));
} else if (Utils.isPromise(x)) {
if (x.state === validStates.PENDING) {
x.then(function (val) {
Resolve(promise, val);
}, function (reason) {
promise.transition(validStates.REJECTED, reason);
});
} else {
promise.transition(x.state, x.value);
}
} else if (Utils.isObject(x) || Utils.isFunction(x)) {
var called = false,
thenHandler;
try {
thenHandler = x.then;
if (Utils.isFunction(thenHandler)) {
thenHandler.call(x,
function (y) {
if (!called) {
Resolve(promise, y);
called = true;
}
},
function (r) {
if (!called) {
promise.reject(r);
called = true;
}
});
} else {
promise.fulfill(x);
called = true;
}
} catch (e) {
if (!called) {
promise.reject(e);
called = true;
}
}
} else {
promise.fulfill(x);
}
}
var fulfill = function (value) {
this.transition(validStates.FULFILLED, value);
};
var reject = function (reason) {
this.transition(validStates.REJECTED, reason);
};
var Adehun = function (fn) {
var that = this;
this.value = null;
this.state = validStates.PENDING;
this.queue = [];
this.handlers = {
fulfill : null,
reject : null
};
if (fn) {
fn(function (value) {
Resolve(that, value);
}, function (reason) {
that.reject(reason);
});
}
};
Adehun.prototype.transition = transition;
Adehun.prototype.process = process;
Adehun.prototype.then = then;
Adehun.prototype.fulfill = fulfill;
Adehun.prototype.reject = reject;
module.exports = {
resolved: function (value) {
return new Adehun(function (resolve) {
resolve(value);
});
},
rejected: function (reason) {
return new Adehun(function (resolve, reject) {
reject(reason);
});
},
deferred: function () {
var resolve, reject;
return {
promise: new Adehun(function (rslv, rjct) {
resolve = rslv;
reject = rjct;
}),
resolve: resolve,
reject: reject
};
}
};