Repository navigation
Expand file tree
/
Copy path4 - Building an XHR connection queue.js
More file actions
227 lines (214 loc) · 5.1 KB
/
Copy path4 - Building an XHR connection queue.js
File metadata and controls
227 lines (214 loc) · 5.1 KB
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
// XMLHttpRequest的核心工具类
var asyncRequest = (function() {
function handleReadyState(o, callback) {
var poll = window.setInterval(
function() {
if (o && o.readyState == 4) {
window.clearInterval(poll);
if (callback) {
callback(o);
}
}
},
50
);
}
var getXHR = function() {
var http;
try {
http = new XMLHttpRequest;
getXHR = function() {
return new XMLHttpRequest;
};
}
catch(e) {
var msxml = [
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP'
];
for (var I = 0, len = msxml.length; i < len; ++i) {
try {
http = new ActiveXObject(msxml[i]);
getXHR = function() {
return new ActiveXObject(msxml[i]);
};
break;
}
catch(e) {}
}
}
return http;
};
return function(method, uri, callback, postData) {
var http = getXHR();
http.open(method, uri, true);
handleReadyState(http, callback);
http.send(postData || null);
return http;
};
})();
// 有了这个方法,就可以用类似于讲述链式调用的第六章中的风格进行开发
Function.prototype.method = function(name, fn) {
this.prototype[name] = fn;
return this;
};
// From the Mozilla Developer Center website at http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6#Array_extras.
if ( !Array.prototype.forEach ) {
Array.method('forEach', function(fn, thisObj) {
var scope = thisObj || window;
for ( var i = 0, len = this.length; i < len; ++i ) {
fn.call(scope, this[i], i, this);
}
});
}
if ( !Array.prototype.filter ) {
Array.method('filter', function(fn, thisObj) {
var scope = thisObj || window;
var a = [];
for ( var i = 0, len = this.length; i < len; ++i ) {
if ( !fn.call(scope, this[i], i, this) ) {
continue;
}
a.push(this[i]);
}
return a;
});
}
// 添加观察者系统
window.DED = window.DED || {};
DED.util = DED.util || {};
DED.util.Observer = function() {
this.fns = [];
}
DED.util.Observer.prototype = {
subscribe: function(fn) {
this.fns.push(fn);
},
unsubscribe: function(fn) {
this.fns = this.fns.filter(
function(el) {
if ( el !== fn ) {
return el;
}
}
);
},
fire: function(o) {
this.fns.forEach(
function(el) {
el(o);
}
);
}
};
// 开发队列的基本框架
DED.Queue = function() {
// Queued requests. 数组字面量,用于保存对每一个请求的引用
this.queue = [];
// Observable Objects that can notify the client of interesting moments
// on each DED.Queue instance.
this.onComplete = new DED.util.Observer;
this.onFailure = new DED.util.Observer;
this.onFlush = new DED.util.Observer;
// Core properties that set up a frontend queueing system.
this.retryCount = 3;
this.currentRetry = 0;
this.paused = false;
this.timeout = 5000;
this.conn = {};
this.timer = {};
};
// 为框架提供需要的方法
DED.Queue.
method('flush', function() {
if (!this.queue.length > 0) {
return;
}
if (this.paused) {
this.paused = false;
return;
}
var that = this;
this.currentRetry++;
var abort = function() {
that.conn.abort();
if (that.currentRetry == that.retryCount) {
that.onFailure.fire();
that.currentRetry = 0;
} else {
that.flush();
}
};
this.timer = window.setTimeout(abort, this.timeout);
var callback = function(o) {
window.clearTimeout(that.timer);
that.currentRetry = 0;
that.queue.shift();
that.onFlush.fire(o.responseText);
if (that.queue.length == 0) {
that.onComplete.fire();
return;
}
// recursive call to flush
that.flush();
};
this.conn = asyncRequest(
this.queue[0]['method'],
this.queue[0]['uri'],
callback,
this.queue[0]['params']
);
}).
method('setRetryCount', function(count) {
this.retryCount = count;
}).
method('setTimeout', function(time) {
this.timeout = time;
}).
method('add', function(o) {
this.queue.push(o);
}).
method('pause', function() {
this.paused = true;
}).
method('dequeue', function() {
this.queue.pop();
}).
method('clear', function() {
this.queue = [];
});
/* Usage. */
var q = new DED.Queue;
// Reset our retry count to be higher for slow connections.
q.setRetryCount(5);
// Decrease timeout limit because we still want fast connections to benefit.
q.setTimeout(1000);
// Add two slots.
q.add({
method: 'GET',
uri: '/path/to/file.php?ajax=true'
});
q.add({
method: 'GET',
uri: '/path/to/file.php?ajax=true&woe=me'
});
// Flush the queue.
q.flush();
// Pause the queue, retaining the requests.
q.pause();
// Clear our queue and start fresh.
q.clear();
// Add two requests.
q.add({
method: 'GET',
uri: '/path/to/file.php?ajax=true'
});
q.add({
method: 'GET',
uri: '/path/to/file.php?ajax=true&woe=me'
});
// Remove the last request from the queue.
q.dequeue();
// Flush the queue again.
q.flush();