-
Notifications
You must be signed in to change notification settings - Fork 3
/
Deferred.coffee
261 lines (176 loc) · 5.63 KB
/
Deferred.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package "cafe"
Deferred: class Deferred
_state: null
_delay: 0
_chain: null
_results: null
_errorTimeoutId: null
__chained: false
constructor: () ->
@_state = Deferred.States.Ready
@_chain = []
@_results = [null, null]
callback: (result) ->
@_check(result)
@_state = Deferred.States.Success
@_resultBack(result)
errorback: (error) ->
@_check(error)
error = new Error(error || "Unknown error") if not (error instanceof Error) and error isnt null
@_state = Deferred.States.Error
@_resultBack(error)
addBoth: (context, fn) ->
argv = arguments
if argv.length < 2
fn = argv[0]
return @addCallbacks(fn, fn)
return @addCallbacks(context, fn, fn)
addCallback: (context, fn) ->
argv = arguments
if argv.length < 2
return @addCallbacks(argv[0], null)
return @addCallbacks(context, fn, null)
addErrorback: (context, fn) ->
argv = arguments
if argv.length < 2
return @addCallbacks(null, argv[0])
return @addCallbacks(context, null, fn)
addCallbacks: (context, callBack, errorBack) ->
throw new Error("Chained Deferreds can not be re-used") if @__chained
argv = arguments
if argv.length < 3
errorBack = argv[1]
callBack = argv[0]
else if context
if typeof callBack is "function"
callBack = ((fn) ->
return () ->
return fn.apply(context, arguments)
)(callBack)
if typeof errorBack is "function"
errorBack = ((fn) ->
return () ->
return fn.apply(context, arguments)
)(errorBack)
@_chain.push([callBack, errorBack])
@_fire() unless @_state is Deferred.States.Ready
return this
_resultBack: (result) ->
@_results[@_state] = result
@_fire()
_check: (object) ->
throw new Error("Already fired") unless @_state is Deferred.States.Ready
throw new Error("Deferred instances can only be chained if they are the result of a callback") if object instanceof Deferred
_paused: () ->
return @_delay isnt 0
_fire: () ->
clearTimeout(@_errorTimeoutId) and @_errorTimeoutId = null unless @_errorTimeoutId is null
return if @_paused()
state = @_state
result = @_results[state]
try
pairs = @_chain
while pair = pairs.shift()
fn = pair[state]
if fn
try
result = fn(result)
state = Deferred.States.Success
if result instanceof Deferred
@_delay++
break
catch ex
state = Deferred.States.Error
result = ex
if @_paused() && result instanceof Deferred
result.addCallbacks(
(res) =>
@_delay--
@_state = Deferred.States.Success
@_resultBack(res)
(err) =>
@_delay--
@_state = Deferred.States.Error
@_resultBack(err)
)
result.__chained = true
return
@_state = state
@_results[@_state] = result
if @_state is Deferred.States.Error
@_errorTimeoutId = setTimeout(
() =>
console.error("Unhandled error in Deferred (possibly?):")
console.error(@_results[@_state])
1000
)
catch ex
throw ex
finally
result = null
@States: {
Ready : -1
Success : 0
Error : 1
}
@succeed: (result, timeout) ->
deferred = new Deferred()
if timeout > 0
setTimeout((-> deferred.callback(result)), timeout)
else
deferred.callback(result)
return deferred
@fail: (error) ->
deferred = new Deferred()
if timeout > 0
setTimeout((-> deferred.errorback(error)), timeout)
else
deferred.errorback(error)
return deferred
@processing: () ->
deferred = Deferred.succeed()
processes = if arguments.length is 1 and arguments[0] instanceof Array then arguments[0].slice() else Array.prototype.slice.call(arguments)
deferred.addCallback(getProcessCallback(process)) for process in processes
return deferred
getChainCallback = (results, value) ->
if value not instanceof Deferred
return () ->
results.push(value)
return value
value.addCallback (result) ->
results.push(result)
return result
return value
getProcessCallback = (process) ->
return (lastResult) ->
current = new Deferred()
try
res = if typeof process is "function" then process(lastResult) else process
if res instanceof Deferred
res.addCallback (result) ->
current.callback(result)
return result
res.addErrorback (error) ->
current.errorback(error)
return null
else if res instanceof Array
count = res.length
if count is 0
current.callback(res)
else
results = []
deferredCollection = (getChainCallback(results, res[i]) for i in [0...count])
Deferred.processing(deferredCollection).
addCallback((result) ->
current.callback(results)
return results
).
addErrorback((error) ->
current.errorback(error)
return null
)
else
current.callback(res)
catch ex
current.errorback(ex)
return current