forked from ysmood/yaku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminPromiseA+.coffee
225 lines (191 loc) · 4.74 KB
/
minPromiseA+.coffee
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
###*
* Before read this source file, open web page [Promises/A+](https://promisesaplus.com).
###
do -> class Yaku
###*
* This class follows the [Promises/A+](https://promisesaplus.com) and
* [ES6](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects) spec
* with some extra helpers.
* @param {Function} executor Function object with two arguments resolve and reject.
* The first argument fulfills the promise, the second argument rejects it.
* We can call these functions, once our operation is completed.
###
constructor: (executor) ->
executor genSettler(@, $resolved), genSettler(@, $rejected)
###*
* Appends fulfillment and rejection handlers to the promise,
* and returns a new promise resolving to the return value of the called handler.
* @param {Function} onFulfilled Optional. Called when the Promise is resolved.
* @param {Function} onRejected Optional. Called when the Promise is rejected.
* @return {Yaku} It will return a new Yaku which will resolve or reject after
* the current Promise.
###
then: (onFulfilled, onRejected) ->
addHandler @, new Yaku(->), onFulfilled, onRejected
# ********************** Private **********************
###
* All static variable name will begin with `$`. Such as `$rejected`.
* @private
###
###*
* These are some static symbolys.
* @private
###
$rejected = 0
$resolved = 1
$pending = 2
# These are some symbols. They won't be used to store data.
$circularError = 'circular promise resolution chain'
# Default state
_state: $pending
###*
* The number of current promises that attach to this Yaku instance.
* @private
###
_pCount: 0
# *************************** Promise Hepers ****************************
###*
* It will produce a settlePromise function to user.
* Such as the resolve and reject in this `new Yaku (resolve, reject) ->`.
* @private
* @param {Yaku} self
* @param {Integer} state The value is one of `$pending`, `$resolved` or `$rejected`.
* @return {Function} `(value) -> undefined` A resolve or reject function.
###
genSettler = (self, state) -> (value) ->
settlePromise self, state, value
###*
* Link the promise1 to the promise2.
* @private
* @param {Yaku} p1
* @param {Yaku} p2
* @param {Function} onFulfilled
* @param {Function} onRejected
###
addHandler = (p1, p2, onFulfilled, onRejected) ->
# 2.2.1
if typeof onFulfilled == 'function'
p2._onFulfilled = onFulfilled
if typeof onRejected == 'function'
p2._onRejected = onRejected
# 2.2.6
if p1._state == $pending
p1[p1._pCount++] = p2
else
scheduleHandler p1, p2
# 2.2.7
p2
###*
* Resolve the value returned by onFulfilled or onRejected.
* @private
* @param {Yaku} p1
* @param {Yaku} p2
###
scheduleHandler = (p1, p2) -> setTimeout ->
handler = if p1._state then p2._onFulfilled else p2._onRejected
# 2.2.7.3
# 2.2.7.4
if handler == undefined
settlePromise p2, p1._state, p1._value
return
try
# 2.2.5
x = handler p1._value
catch e
# 2.2.7.2
settlePromise p2, $rejected, e
return
# 2.2.7.1
settleWithX p2, x
return
###*
* Resolve or reject a promise.
* @param {Yaku} p
* @param {Integer} state
* @param {Any} value
###
settlePromise = (p, state, value) ->
# 2.1.2
# 2.1.3
return if p._state != $pending
# 2.1.1.1
p._state = state
p._value = value
i = 0
len = p._pCount
# 2.2.2
# 2.2.3
while i < len
# 2.2.4
scheduleHandler p, p[i++]
p
###*
* Resolve or reject primise with value x. The x can also be a thenable.
* @private
* @param {Yaku} p
* @param {Any | Thenable} x A normal value or a thenable.
###
settleWithX = (p, x) ->
# 2.3.1
if x == p and x
settlePromise p, $rejected, new TypeError $circularError
return
# 2.3.2
# 2.3.3
type = typeof x
if x != null and (type == 'function' or type == 'object')
try
# 2.3.2.1
xthen = x.then
catch e
# 2.3.3.2
settlePromise p, $rejected, e
return
if typeof xthen == 'function'
settleXthen p, x, xthen
else
# 2.3.3.4
settlePromise p, $resolved, x
else
# 2.3.4
settlePromise p, $resolved, x
p
###*
* Resolve then with its promise.
* @private
* @param {Yaku} p
* @param {Thenable} x
* @param {Function} xthen
###
settleXthen = (p, x, xthen) ->
try
# 2.3.3.3
xthen.call x, (y) ->
# 2.3.3.3.3
return if not x
x = null
# 2.3.3.3.1
settleWithX p, y
return
, (r) ->
# 2.3.3.3.3
return if not x
x = null
# 2.3.3.3.2
settlePromise p, $rejected, r
return
catch e
# 2.3.3.3.4.1
if x
# 2.3.3.3.4.2
settlePromise p, $rejected, e
x = null
return
# CMD & AMD Support
try
module.exports = Yaku
catch
try
define -> Yaku
catch
window.Yaku = Yaku