forked from CreateJS/PreloadJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXHRLoader.js
231 lines (192 loc) · 5.94 KB
/
XHRLoader.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
/*
* XHRLoader for PreloadJS
* Visit http://createjs.com/ for documentation, updates and examples.
*
*
* Copyright (c) 2012 gskinner.com, inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @module PreloadJS
*/
(function (window) {
/**
* The loader that handles XmlHttpRequests.
* @class XHRLoader
* @constructor
* @param {Object} file The object that defines the file to load.
* @extends AbstractLoader
*/
var XHRLoader = function (file) {
this.init(file);
};
var p = XHRLoader.prototype = new AbstractLoader();
//Protected
p._wasLoaded = false;
p._request = null;
p._loadTimeOutTimeout = null;
/**
* Is the browsers XMLHTTPRequest version 1 or 2.
* There is no way to accurately detect v1 vs v2 ... so its our best guess.
* @private
*/
p._xhrLevel = null;
p.init = function (item) {
this._item = item;
if (!this._createXHR(item)) {
//TODO: Throw error?
}
};
p.getResult = function() {
//[SB] When loading XML IE9 does not return .response, instead it returns responseXML.xml
try {
return this._request.responseText;
} catch (error) {}
return this._request.response;
}
p.cancel = function() {
this._clean();
this._request.abort();
};
p.load = function() {
if (this._request == null) {
this.handleError();
return;
}
//Setup timeout if we're not using XHR2
if (this._xhrLevel == 1) {
this._loadTimeOutTimeout = setTimeout(PreloadJS.proxy(this.handleTimeout, this), PreloadJS.TIMEOUT_TIME);
}
//Events
this._request.onloadstart = PreloadJS.proxy(this.handleLoadStart, this);
this._request.onprogress = PreloadJS.proxy(this.handleProgress, this);
this._request.onabort = PreloadJS.proxy(this.handleAbort, this);
this._request.onerror = PreloadJS.proxy(this.handleError, this);
this._request.ontimeout = PreloadJS.proxy(this.handleTimeout, this);
//this._request.onloadend = PreloadJS.proxy(this.handleLoadEnd, this);
//LM: Firefox does not get onload. Chrome gets both. Might need onload for other things.
this._request.onload = PreloadJS.proxy(this.handleLoad, this);
this._request.onreadystatechange = PreloadJS.proxy(this.handleReadyStateChange, this);
try { // Sometimes we get back 404s immediately, particularly when there is a cross origin request.
this._request.send();
} catch (error) {}
};
p.handleProgress = function(event) {
if (event.loaded > 0 && event.total == 0) {
return; // Sometimes we get no "total", so just ignore the progress event.
}
this._sendProgress({loaded:event.loaded, total:event.total});
};
p.handleLoadStart = function() {
clearTimeout(this._loadTimeOutTimeout);
this._sendLoadStart();
};
p.handleAbort = function() {
this._clean();
this._sendError();
};
p.handleError = function() {
this._clean();
this._sendError();
};
p.handleReadyStateChange = function() {
if (this._request.readyState == 4) {
this.handleLoad();
}
}
p._checkError = function() {
//LM: Probably need additional handlers here.
var status = parseInt(this._request.status);
switch (status) {
case 404: // Not Found
case 0: // Not Loaded
return false;
}
if (this._request.response == null) {
try {
// We have to check this for IE, and other browsers will throw errors, so we have to try/catch it.
if (this._request.responseXML != null) { return true; }
} catch (error) {}
return false; }
return true;
};
p.handleLoad = function(event) {
if (this.loaded) { return; }
this.loaded = true;
if(!this._checkError()) {
this.handleError();
return;
}
this._clean();
this._sendComplete();
};
p.handleTimeout = function() {
this._clean();
this._sendError();
};
p.handleLoadEnd = function() {
this._clean();
};
p._createXHR = function(item) {
this._xhrLevel = 1;
if (window.ArrayBuffer) {
this._xhrLevel = 2;
}
// Old IE versions use a different approach
if (window.XMLHttpRequest) {
this._request = new XMLHttpRequest();
} else {
try {
this._request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
} catch(ex) {
return null;
}
}
//IE9 doesn't suport .overrideMimeType(), so we need to check for it.
if (item.type == PreloadJS.TEXT && this._request.overrideMimeType) {
this._request.overrideMimeType('text/plain; charset=x-user-defined');
}
this._request.open('GET', item.src, true);
if (PreloadJS.isBinary(item.type)) {
this._request.responseType = 'arraybuffer';
}
return true;
};
p._clean = function() {
clearTimeout(this._loadTimeOutTimeout);
var req = this._request;
req.onloadstart = null;
req.onprogress = null;
req.onabort = null;
req.onerror = null;
req.onload = null;
req.ontimeout = null;
req.onloadend = null;
req.onreadystatechange = null;
clearInterval(this._checkLoadInterval);
};
p.toString = function() {
return "[PreloadJS XHRLoader]";
}
PreloadJS.lib.XHRLoader = XHRLoader;
}(window));