-
Notifications
You must be signed in to change notification settings - Fork 19
/
deferred.js
207 lines (198 loc) · 6.85 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
(function() {
//无依赖版
var noop = function(){}
function Deferred(mixin) {
var state = "pending", dirty = false
function ok(x) {
state = "fulfilled"
return x
}
function ng(e) {
state = "rejected"
throw e
}
var dfd = {
callback: {
resolve: ok,
reject: ng,
notify: noop,
ensure: noop
},
dirty: function() {
return dirty
},
state: function() {
return state
},
promise: {
then: function() {
return _post.apply(null, arguments)
},
otherwise: function(onReject) {
return _post(0, onReject)
},
//https://github.com/cujojs/when/issues/103
ensure: function(onEnsure) {
return _post(0, 0, 0, onEnsure)
},
_next: null
}
}
if (typeof mixin === "function") {
mixin(dfd.promise)
} else if (mixin && typeof mixin === "object") {
for (var i in mixin) {
if (!dfd.promise[i]) {
dfd.promise[i] = mixin[i]
}
}
}
//http://thanpol.as/javascript/promises-a-performance-hits-you-should-be-aware-of/
"resolve,reject,notify".replace(/\w+/g, function(method) {
dfd[method] = function() {
var that = this, args = arguments
//http://promisesaplus.com/ 4.1
if (that.dirty()) {
_fire.call(that, method, args)
} else {
Deferred.nextTick(function() {
_fire.call(that, method, args)
})
}
}
})
return dfd
function _post() {
var index = -1, fns = arguments;
"resolve,reject,notify,ensure".replace(/\w+/g, function(method) {
var fn = fns[++index];
if (typeof fn === "function") {
dirty = true
if (method === "resolve" || method === "reject") {
dfd.callback[method] = function() {
try {
var value = fn.apply(this, arguments)
state = "fulfilled"
return value
} catch (err) {
state = "rejected"
return err
}
}
} else {
dfd.callback[method] = fn;
}
}
})
var deferred = dfd.promise._next = Deferred(mixin)
return deferred.promise;
}
function _fire(method, array) {
var next = "resolve", value
if (this.state() === "pending" || method === "notify") {
var fn = this.callback[method]
try {
value = fn.apply(this, array);
} catch (e) {//处理notify的异常
value = e
}
if (this.state() === "rejected") {
next = "reject"
} else if (method === "notify") {
next = "notify"
}
array = [value]
}
var ensure = this.callback.ensure
if (noop !== ensure) {
try {
ensure.call(this)//模拟finally
} catch (e) {
next = "reject";
array = [e];
}
}
var nextDeferred = this.promise._next
if (Deferred.isPromise(value)) {
value._next = nextDeferred
} else {
if (nextDeferred) {
_fire.call(nextDeferred, next, array);
}
}
}
}
window.Deferred = Deferred;
Deferred.isPromise = function(obj) {
return !!(obj && typeof obj.then === "function");
};
function some(any, promises) {
var deferred = Deferred(), n = 0, result = [], end
function loop(promise, index) {
promise.then(function(ret) {
if (!end) {
result[index] = ret//保证回调的顺序
n++;
if (any || n >= promises.length) {
deferred.resolve(any ? ret : result);
end = true
}
}
}, function(e) {
end = true
deferred.reject(e);
})
}
for (var i = 0, l = promises.length; i < l; i++) {
loop(promises[i], i)
}
return deferred.promise;
}
Deferred.all = function() {
return some(false, arguments)
}
Deferred.any = function() {
return some(true, arguments)
};
//http://www.raychase.net/1329
//http://www.cnblogs.com/iamzhanglei/archive/2013/02/24/2924680.html
var BrowserMutationObserver = window.MutationObserver || window.WebKitMutationObserver;
if (BrowserMutationObserver) {//chrome firefox
Deferred.nextTick = function(callback) {
var input = document.createElement("input")
var observer = new BrowserMutationObserver(function(mutations) {
mutations.forEach(function() {
callback()
});
});
observer.observe(input, {attributes: true});
input.setAttribute("value", Math.random())
}
} else if (window.VBArray) {//IE
Deferred.nextTick = function(callback) {
var node = document.createElement("script");
node.onreadystatechange = function() {
callback()
node.onreadystatechange = null
node.parentNode && node.parentNode.removeChild(node);
node = null;
};
document.documentElement.appendChild(node);
}
} else if (window.postMessage && window.addEventListener) {//safar opera
Deferred.nextTick = function(callback) {
function onGlobalMessage(event) {
if (typeof event.data === "string" && event.data.indexOf("usePostMessage") === 0) {
callback()
}
}
window.addEventListener("message", onGlobalMessage);
var now = new Date - 0;
window.postMessage("usePostMessage" + now, "*");
}
} else {
Deferred.nextTick = function(callback) {
setTimeout(callback, 0)
}
}
})()