-
Notifications
You must be signed in to change notification settings - Fork 29
/
deferred.js
269 lines (253 loc) · 7.78 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
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
262
263
264
265
266
267
268
269
// Generated by CoffeeScript 1.10.0
(function() {
var Deferred, PENDING, REJECTED, RESOLVED, VERSION, _when, after, execute, flatten, has, installInto, isArguments, isPromise, wrap,
slice = [].slice;
VERSION = '3.1.0';
PENDING = "pending";
RESOLVED = "resolved";
REJECTED = "rejected";
has = function(obj, prop) {
return obj != null ? obj.hasOwnProperty(prop) : void 0;
};
isArguments = function(obj) {
return has(obj, 'length') && has(obj, 'callee');
};
isPromise = function(obj) {
return has(obj, 'promise') && typeof (obj != null ? obj.promise : void 0) === 'function';
};
flatten = function(array) {
if (isArguments(array)) {
return flatten(Array.prototype.slice.call(array));
}
if (!Array.isArray(array)) {
return [array];
}
return array.reduce(function(memo, value) {
if (Array.isArray(value)) {
return memo.concat(flatten(value));
}
memo.push(value);
return memo;
}, []);
};
after = function(times, func) {
if (times <= 0) {
return func();
}
return function() {
if (--times < 1) {
return func.apply(this, arguments);
}
};
};
wrap = function(func, wrapper) {
return function() {
var args;
args = [func].concat(Array.prototype.slice.call(arguments, 0));
return wrapper.apply(this, args);
};
};
execute = function(callbacks, args, context) {
var callback, i, len, ref, results;
ref = flatten(callbacks);
results = [];
for (i = 0, len = ref.length; i < len; i++) {
callback = ref[i];
results.push(callback.call.apply(callback, [context].concat(slice.call(args))));
}
return results;
};
Deferred = function() {
var candidate, close, closingArguments, doneCallbacks, failCallbacks, progressCallbacks, state;
state = PENDING;
doneCallbacks = [];
failCallbacks = [];
progressCallbacks = [];
closingArguments = {
'resolved': {},
'rejected': {},
'pending': {}
};
this.promise = function(candidate) {
var pipe, storeCallbacks;
candidate = candidate || {};
candidate.state = function() {
return state;
};
storeCallbacks = function(shouldExecuteImmediately, holder, holderState) {
return function() {
if (state === PENDING) {
holder.push.apply(holder, flatten(arguments));
}
if (shouldExecuteImmediately()) {
execute(arguments, closingArguments[holderState]);
}
return candidate;
};
};
candidate.done = storeCallbacks((function() {
return state === RESOLVED;
}), doneCallbacks, RESOLVED);
candidate.fail = storeCallbacks((function() {
return state === REJECTED;
}), failCallbacks, REJECTED);
candidate.progress = storeCallbacks((function() {
return state !== PENDING;
}), progressCallbacks, PENDING);
candidate.always = function() {
var ref;
return (ref = candidate.done.apply(candidate, arguments)).fail.apply(ref, arguments);
};
pipe = function(doneFilter, failFilter, progressFilter) {
var filter, master;
master = new Deferred();
filter = function(source, funnel, callback) {
if (!callback) {
return candidate[source](master[funnel]);
}
return candidate[source](function() {
var args, value;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
value = callback.apply(null, args);
if (isPromise(value)) {
return value.done(master.resolve).fail(master.reject).progress(master.notify);
} else {
return master[funnel](value);
}
});
};
filter('done', 'resolve', doneFilter);
filter('fail', 'reject', failFilter);
filter('progress', 'notify', progressFilter);
return master;
};
candidate.pipe = pipe;
candidate.then = pipe;
if (candidate.promise == null) {
candidate.promise = function() {
return candidate;
};
}
return candidate;
};
this.promise(this);
candidate = this;
close = function(finalState, callbacks, context) {
return function() {
if (state === PENDING) {
state = finalState;
closingArguments[finalState] = arguments;
execute(callbacks, closingArguments[finalState], context);
return candidate;
}
return this;
};
};
this.resolve = close(RESOLVED, doneCallbacks);
this.reject = close(REJECTED, failCallbacks);
this.notify = close(PENDING, progressCallbacks);
this.resolveWith = function(context, args) {
return close(RESOLVED, doneCallbacks, context).apply(null, args);
};
this.rejectWith = function(context, args) {
return close(REJECTED, failCallbacks, context).apply(null, args);
};
this.notifyWith = function(context, args) {
return close(PENDING, progressCallbacks, context).apply(null, args);
};
return this;
};
_when = function() {
var def, defs, finish, i, len, resolutionArgs, trigger;
defs = Array.prototype.slice.apply(arguments);
if (defs.length === 1) {
if (isPromise(defs[0])) {
return defs[0];
} else {
return (new Deferred()).resolve(defs[0]).promise();
}
}
trigger = new Deferred();
if (!defs.length) {
return trigger.resolve().promise();
}
resolutionArgs = [];
finish = after(defs.length, function() {
return trigger.resolve.apply(trigger, resolutionArgs);
});
defs.forEach(function(def, index) {
if (isPromise(def)) {
return def.done(function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
resolutionArgs[index] = args.length > 1 ? args : args[0];
return finish();
});
} else {
resolutionArgs[index] = def;
return finish();
}
});
for (i = 0, len = defs.length; i < len; i++) {
def = defs[i];
isPromise(def) && def.fail(trigger.reject);
}
return trigger.promise();
};
installInto = function(fw) {
fw.Deferred = function() {
return new Deferred();
};
fw.ajax = wrap(fw.ajax, function(ajax, options) {
var createWrapper, def, promise, xhr;
if (options == null) {
options = {};
}
def = new Deferred();
createWrapper = function(wrapped, finisher) {
return wrap(wrapped, function() {
var args, func;
func = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
if (func) {
func.apply(null, args);
}
return finisher.apply(null, args);
});
};
options.success = createWrapper(options.success, def.resolve);
options.error = createWrapper(options.error, def.reject);
xhr = ajax(options);
promise = def.promise();
promise.abort = function() {
return xhr.abort();
};
return promise;
});
return fw.when = _when;
};
if (typeof exports !== 'undefined') {
exports.Deferred = function() {
return new Deferred();
};
exports.when = _when;
exports.installInto = installInto;
} else if (typeof define === 'function' && define.amd) {
define(function() {
if (typeof Zepto !== 'undefined') {
return installInto(Zepto);
} else {
Deferred.when = _when;
Deferred.installInto = installInto;
return Deferred;
}
});
} else if (typeof Zepto !== 'undefined') {
installInto(Zepto);
} else {
this.Deferred = function() {
return new Deferred();
};
this.Deferred.when = _when;
this.Deferred.installInto = installInto;
}
}).call(this);