-
Notifications
You must be signed in to change notification settings - Fork 19
/
_deferred.js
187 lines (175 loc) · 5.48 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
function Deferred() {
return (this instanceof Deferred) ? this.init() : new Deferred()
}
Deferred.ok = function(x) {
return x
};
Deferred.ng = function(x) {
throw x
};
//http://www.cnblogs.com/iamzhanglei/archive/2013/02/24/2924680.html
Deferred.prototype = {
init: function() {
this.callback = {
resolve: Deferred.ok,
reject: Deferred.ng,
notify: Deferred.ok,
ensure: Deferred.ok
};
var that = this
this.state = "pending"
this.dirty = false;
this.promise = {
then: function(onResolve, onReject, onNotify) {
return that._post(onResolve, onReject, onNotify)
},
otherwise: function(onReject) {
return that._post(null, onReject, null)
},
//https://github.com/cujojs/when/issues/103
ensure: function(onEnsure) {
return that._post(0, 0, 0, onEnsure)
},
_next: null
}
return this;
},
_post: function(fn0, fn1, fn2, fn3) {
var deferred
if (!this.dirty ) {
deferred = this;
} else {
deferred = this.promise._next = new Deferred();
}
var index = -1, fns = arguments;
"resolve,reject,notify, ensure".replace(/\w+/g, function(method) {
var fn = fns[++index];
if (typeof fn === "function") {
deferred.callback[method] = fn;
deferred.dirty = true;
}
})
return deferred.promise;
},
_fire: function(method, value) {
var next = "resolve";
try {
if (this.state == "pending" || method == "notify") {
var fn = this.callback[method]
value = fn.call(this, value);
if (method !== "notify") {
this.state = method
} else {
next = "notify"
}
}
} catch (e) {
next = "reject";
value = e;
}
var ensure = this.callback.ensure
if (Deferred.ok !== ensure) {
try {
ensure.call(this)
} catch (e) {
next = "reject";
value = e;
}
}
if (Deferred.isPromise(value)) {
value._next = this.promise._next
} else {
if (this.promise._next) {
this.promise._next._fire(next, value);
}
}
return this;
}
};
//http://thanpol.as/javascript/promises-a-performance-hits-you-should-be-aware-of/
"resolve,reject,notify".replace(/\w+/g, function(method) {
Deferred.prototype[method] = function(val) {
//http://promisesaplus.com/ 4.1
if (!this.dirty) {
var that = this;
setTimeout(function() {
that._fire(method, val)
}, 0)
} else {
return this._fire(method, val)
}
}
})
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)
};
(function() {
return
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)
}
}
})();