Skip to content

Commit 73eeea7

Browse files
committed
deferred
1 parent 5cc634a commit 73eeea7

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

deferred/deferred.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
(function(deferred){
2+
if (typeof module !== 'undefined') {
3+
var DeferredURLRequest = require('./DeferredURLRequest')
4+
for (var k in DeferredURLRequest) {
5+
DeferredAPI[k] = DeferredURLRequest[k]
6+
}
7+
module.exports = DeferredAPI
8+
}
9+
var global = function() {
10+
return this || (1, eval)('this')
11+
}();
12+
global['Aaron'] = deferred;
13+
})(new function(){
14+
/**
15+
* 接口
16+
* @type {Object}
17+
*/
18+
var API = {
19+
'deferred' : deferred,
20+
'next' : Next,
21+
'wait' : Wait
22+
// all : all,
23+
// Deferred : Deferred,
24+
// DeferredList : DeferredList,
25+
// wrapResult : wrapResult,
26+
// wrapFailure : wrapFailure,
27+
// Failure : Failure
28+
}
29+
30+
function ok(x) {
31+
return x
32+
};
33+
34+
function ng(x) {
35+
throw x
36+
};
37+
38+
function deferred(t) {
39+
return new Deferred(t)
40+
}
41+
42+
function Deferred() {
43+
this._next = null;
44+
this.callback = {
45+
'ok': ok,
46+
'ng': ng
47+
};
48+
return this;
49+
}
50+
51+
Deferred.prototype.then = function(fun) {
52+
return this._post("ok", fun);
53+
};
54+
55+
Deferred.prototype.error = function(fun) {
56+
return this._post("ng", fun);
57+
};
58+
59+
Deferred.prototype.resolve = function(val) {
60+
return this._fire("ok", val);
61+
};
62+
63+
Deferred.prototype.fail = function(val) {
64+
return this._fire("ng", err);
65+
};
66+
67+
Deferred.prototype.cancel = function(val) {
68+
(this.canceller || function () {
69+
}).apply(this);
70+
return this.init();
71+
};
72+
73+
Deferred.prototype._post = function(okng, fun) {
74+
this._next = new Deferred();
75+
this._next.callback[okng] = fun;
76+
return this._next;
77+
};
78+
79+
Deferred.prototype._fire = function(okng, value) {
80+
var next = "ok";
81+
try {
82+
//执行回调函数
83+
value = this.callback[okng].call(this, value);
84+
} catch (e) {
85+
next = "ng";
86+
value = e;
87+
if (Deferred.onerror) Deferred.onerror(e);
88+
}
89+
if (isDeferred(value)) { //如果返回值是deferred对象
90+
value._next = this._next;
91+
} else {
92+
if (this._next) this._next._fire(next, value);
93+
}
94+
return this;
95+
};
96+
97+
function isDeferred(obj) {
98+
return !!(obj && obj._id === Deferred.prototype._id);
99+
};
100+
101+
function Next(fun) {
102+
var d = new Deferred();
103+
var img = new Image();
104+
var handler = function() {
105+
d.canceller();
106+
d.call();
107+
};
108+
img.addEventListener("load", handler, false);
109+
img.addEventListener("error", handler, false);
110+
d.canceller = function() {
111+
img.removeEventListener("load", handler, false);
112+
img.removeEventListener("error", handler, false);
113+
};
114+
img.src = "data:image/png," + Math.random();
115+
if (fun) d.callback.ok = fun;
116+
return d;
117+
}
118+
119+
function Wait(n) {
120+
var d = new Deferred(),
121+
t = new Date();
122+
var id = setTimeout(function() {
123+
d.call((new Date()).getTime() - t.getTime());
124+
}, n * 1000);
125+
d.canceller = function() {
126+
clearTimeout(id)
127+
};
128+
return d;
129+
};
130+
131+
function register(name, fun) {
132+
Deferred.prototype[name] = function() {
133+
var a = arguments;
134+
return this.next(function() {
135+
return fun.apply(this, a);
136+
});
137+
};
138+
};
139+
140+
register("wait", Wait);
141+
142+
return API;
143+
});

deferred/test.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<title>JSDeferred Test</title>
5+
6+
<!--<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>-->
7+
<script type="text/javascript" src="deferred.js"></script>
8+
<!--<script type="text/javascript" src="binding/jquery.js"></script>-->
9+
10+
<!--script type="text/javascript" src="jsdeferred.js"></script-->
11+
<script type="text/javascript" src="test.js"></script>
12+
13+
</head>
14+
<body>
15+
16+
</body>
17+
</html>

deferred/test.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
window.onload = function(){
2+
3+
4+
Aaron.next(function func1() {
5+
setTimeout(function(){
6+
console.log(11111111)
7+
},100)
8+
return Aaron.wait(2);
9+
})
10+
.next(function func2() {
11+
setTimeout(function(){
12+
console.log(22222222)
13+
},300)
14+
})
15+
.next(function func2() {
16+
setTimeout(function(){
17+
console.log(3333333)
18+
},50)
19+
})
20+
}

0 commit comments

Comments
 (0)