-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsdeferred.js
422 lines (388 loc) · 13.7 KB
/
jsdeferred.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// JSDeferred 0.4.0 Copyright (c) 2007 cho45 ( www.lowreal.net )
// See http://github.com/cho45/jsdeferred
function Deferred () { return (this instanceof Deferred) ? this.init() : new Deferred() }
Deferred.ok = function (x) { return x };
Deferred.ng = function (x) { throw x };
Deferred.prototype = {
_id : 1111,
init : function () {
this._next = null;
this.callback = {
ok: Deferred.ok,
ng: Deferred.ng
};
return this;
},
// 当new Deferred()时,会调用该next函数
next : function (fun) {
//this为第一个next返回的deferred,这里写死了ok
return this._post("ok", fun) },
error : function (fun) { return this._post("ng", fun) },
// call的参数会传给本Deferred的callback,也即next中的function
call : function (val) {
//这里写死了ok
// console.log('call ' + val);
return this._fire("ok", val) },
fail : function (err) { return this._fire("ng", err) },
cancel : function () {
(this.canceller || function () {})();
return this.init();
},
_post : function (okng, fun) {
this._next = new Deferred();
this._next.callback[okng] = fun;
// 返回this._next,形成链式调用
return this._next;
},
_fire : function (okng, value) {
var next = "ok";
try {
value = this.callback[okng].call(this, value);
// console.log(value);
} catch (e) {
next = "ng";
value = e;
if (Deferred.onerror) Deferred.onerror(e);
}
if (Deferred.isDeferred(value)) {
// 此时value为wait,wait执行后再执行_next(如果value为deferred对象,则先执行其call方法,延迟this._next)
value._next = this._next;
} else {
// console.log('not deferred');
if (this._next) this._next._fire(next, value);
}
return this;
}
};
Deferred.isDeferred = function (obj) {
return !!(obj && obj._id === Deferred.prototype._id);
};
Deferred.next_default = function (fun) {
var d = new Deferred();
var id = setTimeout(function () { d.call() }, 0);
d.canceller = function () { clearTimeout(id) };
if (fun) d.callback.ok = fun;
return d;
};
Deferred.next_faster_way_readystatechange = ((typeof window === 'object') && (location.protocol == "http:") && !window.opera && /\bMSIE\b/.test(navigator.userAgent)) && function (fun) {
var d = new Deferred();
var t = new Date().getTime();
if (t - arguments.callee._prev_timeout_called < 150) {
var cancel = false;
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "data:text/javascript,";
script.onreadystatechange = function () {
if (!cancel) {
d.canceller();
d.call();
}
};
d.canceller = function () {
if (!cancel) {
cancel = true;
script.onreadystatechange = null;
document.body.removeChild(script);
}
};
document.body.appendChild(script);
} else {
arguments.callee._prev_timeout_called = t;
var id = setTimeout(function () { d.call() }, 0);
d.canceller = function () { clearTimeout(id) };
}
if (fun) d.callback.ok = fun;
return d;
};
Deferred.next_faster_way_Image = ((typeof window === 'object') && (typeof(Image) != "undefined") && !window.opera && document.addEventListener) && function (fun) {
//这里是new Deferred,获得了prototype中的方法
var d = new Deferred();
var img = new Image();
var handler = function () {
d.canceller();
//链式完next后,才执行第一个next的call
d.call();
};
img.addEventListener("load", handler, false);
img.addEventListener("error", handler, false);
d.canceller = function () {
img.removeEventListener("load", handler, false);
img.removeEventListener("error", handler, false);
};
img.src = "data:image/png," + Math.random();
if (fun) d.callback.ok = fun;
// console.log('first');
return d;
};
//nodejs
Deferred.next_tick = (typeof process === 'object' && typeof process.nextTick === 'function') && function (fun) {
var d = new Deferred();
process.nextTick(function() { d.call() });
if (fun) d.callback.ok = fun;
return d;
};
// 当使用Deferred.define()时,会调用该next函数
Deferred.next =
Deferred.next_faster_way_readystatechange ||
Deferred.next_faster_way_Image ||
Deferred.next_tick ||
Deferred.next_default;
// 将多个函数拚装成next next next的形式
Deferred.chain = function () {
var chain = Deferred.next();
for (var i = 0, len = arguments.length; i < len; i++) (function (obj) {
switch (typeof obj) {
case "function":
var name = null;
try {
name = obj.toString().match(/^\s*function\s+([^\s()]+)/)[1];
} catch (e) { }
if (name != "error") {
chain = chain.next(obj);
} else {
chain = chain.error(obj);
}
break;
case "object":
chain = chain.next(function() { return Deferred.parallel(obj) });
break;
default:
throw "unknown type in process chains";
}
})(arguments[i]);
return chain;
};
Deferred.wait = function (n) {
var d = new Deferred(), t = new Date();
var id = setTimeout(function () {
// console.log('wait');
d.call((new Date()).getTime() - t.getTime());
}, n * 1000);
d.canceller = function () { clearTimeout(id) };
//先返回再执行
// console.log('return wait');
return d;
};
Deferred.call = function (fun) {
var args = Array.prototype.slice.call(arguments, 1);
return Deferred.next(function () {
return fun.apply(this, args);
});
};
Deferred.parallel = function (dl) {
// dl传进来的时候已经是返回了Deferred对象,也就是已经执行了
var isArray = false;
// console.log('here');
if (arguments.length > 1) {
dl = Array.prototype.slice.call(arguments);
isArray = true;
} else if (Array.isArray && Array.isArray(dl) || typeof dl.length == "number") {
isArray = true;
}
var ret = new Deferred(), values = {}, num = 0;
// 将parallel的数组封装为一个链
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
// if (typeof d == "function") {
// console.log('function');
// dl[i] = d = Deferred.next(d);
// }
// console.log('start');
d.next(function (v) {
values[i] = v;
// console.log('num='+num);
if (--num <= 0) {
if (isArray) {
values.length = dl.length;
values = Array.prototype.slice.call(values, 0);
}
// console.log('ret call');
// 循环运行到最后才执行call,并传入所有结果
ret.call(values);
}
// console.log('num='+num);
//error是next的this._next
}).error(function (e) {
ret.fail(e);
});
// console.log('end');
num++;
})(dl[i], i);
// console.log('!num start');
if (!num) {
// console.log('next next');
Deferred.next(function () { ret.call() });
}
// console.log('!num end');
ret.canceller = function () {
for (var i in dl) if (dl.hasOwnProperty(i)) {
dl[i].cancel();
}
};
// console.log('return');
return ret;
};
// 最先执行的deferred将会传入value给next,然后调用canceller取消其他deferred
Deferred.earlier = function (dl) {
var isArray = false;
if (arguments.length > 1) {
dl = Array.prototype.slice.call(arguments);
isArray = true;
} else if (Array.isArray && Array.isArray(dl) || typeof dl.length == "number") {
isArray = true;
}
var ret = new Deferred(), values = {}, num = 0;
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
d.next(function (v) {
values[i] = v;
if (isArray) {
values.length = dl.length;
values = Array.prototype.slice.call(values, 0);
}
ret.call(values);
//取消其他deferred
ret.canceller();
}).error(function (e) {
ret.fail(e);
});
num++;
})(dl[i], i);
if (!num) Deferred.next(function () { ret.call() });
ret.canceller = function () {
for (var i in dl) if (dl.hasOwnProperty(i)) {
dl[i].cancel();
}
};
return ret;
};
Deferred.loop = function (n, fun) {
var o = {
begin : n.begin || 0,
end : (typeof n.end == "number") ? n.end : n - 1,
step : n.step || 1,
last : false,
prev : null
};
var ret, step = o.step;
return Deferred.next(function () {
function _loop (i) {
if (i <= o.end) {
if ((i + step) > o.end) {
o.last = true;
o.step = o.end - i + 1;
}
o.prev = ret;
ret = fun.call(this, i, o);
// 如果外面的loop方法的function参数返回的是deferred对象,执行下面操作
if (Deferred.isDeferred(ret)) {
// 等待外面有call调用时,下面的next就会执行,否则不会执行
return ret.next(function (r) {
ret = r;
return Deferred.call(_loop, i + step);
});
} else {
return Deferred.call(_loop, i + step);
}
} else {
return ret;
}
}
return (o.begin <= o.end) ? Deferred.call(_loop, o.begin) : null;
});
};
// 没20ms执行一次,防止柱塞UI线程
Deferred.repeat = function (n, fun) {
var i = 0, end = {}, ret = null;
return Deferred.next(function () {
// alert('call one');
var t = (new Date()).getTime();
do {
if (i >= n) return null;
ret = fun(i++);
} while ((new Date()).getTime() - t < 20);
// arguments.callee不断地调用next中的function方法,即多次执行do while, 确保每次循环在20ms内
return Deferred.call(arguments.callee);
});
};
// 添加注册方法到Deferred的prototype中
Deferred.register = function (name, fun) {
this.prototype[name] = function () {
var a = arguments;
return this.next(function () {
return fun.apply(this, a);
});
};
};
Deferred.register("loop", Deferred.loop);
Deferred.register("wait", Deferred.wait);
Deferred.connect = function (funo, options) {
var target, func, obj;
if (typeof arguments[1] == "string") {
target = arguments[0];
func = target[arguments[1]];
obj = arguments[2] || {};
} else {
func = arguments[0];
obj = arguments[1] || {};
target = obj.target;
}
var partialArgs = obj.args ? Array.prototype.slice.call(obj.args, 0) : [];
var callbackArgIndex = isFinite(obj.ok) ? obj.ok : obj.args ? obj.args.length : undefined;
var errorbackArgIndex = obj.ng;
return function () {
var d = new Deferred().next(function (args) {
var next = this._next.callback.ok;
this._next.callback.ok = function () {
return next.apply(this, args.args);
};
});
var args = partialArgs.concat(Array.prototype.slice.call(arguments, 0));
if (!(isFinite(callbackArgIndex) && callbackArgIndex !== null)) {
callbackArgIndex = args.length;
}
var callback = function () { d.call(new Deferred.Arguments(arguments)) };
args.splice(callbackArgIndex, 0, callback);
if (isFinite(errorbackArgIndex) && errorbackArgIndex !== null) {
var errorback = function () { d.fail(arguments) };
args.splice(errorbackArgIndex, 0, errorback);
}
Deferred.next(function () { func.apply(target, args) });
return d;
};
};
Deferred.Arguments = function (args) { this.args = Array.prototype.slice.call(args, 0) };
Deferred.retry = function (retryCount, funcDeferred, options) {
if (!options) options = {};
var wait = options.wait || 0;
var d = new Deferred();
var retry = function () {
var m = funcDeferred(retryCount);
m.
next(function (mes) {
// 执行到这里说明函数执行成功,所以执行了next到达这里,接着d.call执行后就能执行到外层的next了,即retry成功
d.call(mes);
}).
error(function (e) {
if (--retryCount <= 0) {
d.fail(['retry failed', e]);
} else {
// 继续retry
setTimeout(retry, wait * 1000);
}
});
};
setTimeout(retry, 0);
return d;
};
Deferred.methods = ["parallel", "wait", "next", "call", "loop", "repeat", "chain"];
Deferred.define = function (obj, list) {
if (!list) list = Deferred.methods;
// 不传obj则闭包执行,将方法都赋给window
if (!obj) obj = (function getGlobal () { return this })();
for (var i = 0; i < list.length; i++) {
var n = list[i];
//赋值给this,全局变量
obj[n] = Deferred[n];
}
return Deferred;
};
this.Deferred = Deferred;