-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcache.uncompressed.js
104 lines (89 loc) · 2.75 KB
/
cache.uncompressed.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
/**
* AMD-cache, a loader plugin for AMD loaders.
*
* Available via the MIT or new BSD license.
*
* Copyright (c) 2011 Jens Arps
*
* The xhr code is taken from the RequireJS text plugin:
*
* @license RequireJS text 0.26.0 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
* see: http://github.com/jrburke/requirejs for details
*/
(function () {
var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],
hasLocalStorage = (function(){
var supported = false;
try{
supported = window && ("localStorage" in window) && ("getItem" in localStorage);
}catch(e){}
return supported;
})();
define(function () {
var cache = {
createXhr: function () {
//Would love to dump the ActiveX crap in here. Need IE 6 to die first.
var xhr, i, progId;
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else {
for (i = 0; i < 3; i++) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
} catch (e) {}
if (xhr) {
progIds = [progId]; // so faster next time
break;
}
}
}
if (!xhr) {
throw new Error("createXhr(): XMLHttpRequest not available");
}
return xhr;
},
get: function (url, callback) {
var xhr = cache.createXhr();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (evt) {
//Do not explicitly handle errors, those should be
//visible via console output in the browser.
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
};
xhr.send(null);
},
load: function (name, req, load, config) {
var cached, url = req.toUrl(name); // TODO: prepend location.pathname?
if (hasLocalStorage) { // in build context, this will be false, too
cached = localStorage.getItem(url);
if (cached !== null) {
load.fromText(name, cached);
} else {
cache.get(url, function (content) {
content += '\n //@ sourceURL='+name;
load.fromText(name, content);
// can't just fall through here, as we
// will already have returned at this time.
req([name], function (content) {
load(content);
});
try { // need to wrap this to catch potential QUOTA_EXCEEDED
localStorage.setItem(url, content);
} catch(e) {}
});
// need to return here to prevent a second
// request being sent over the network.
return;
}
}
req([name], function (content) {
load(content);
});
}
};
return cache;
});
}());