-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
143 lines (130 loc) · 3.85 KB
/
cache.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
define(['./io', './FauxXHR', './scaffold'], function (io, FauxXHR, scaffold) {
'use strict';
// cache I/O requests
// this is a browser-only module.
function cache (options, prep, level) {
var key = prep.key;
if (options.wait || options.bust ||
io.track && io.track.deferred[key] ||
!io.cache.optIn(options)) {
return null;
}
// retrieve data, if available
var data = io.cache.storage.retrieve(key);
if (typeof data !== 'undefined') {
return io.Deferred.resolve(new io.Result(new FauxXHR(data), options, null));
}
// pass the request, and cache the result
var promise = io.request(options, prep, level - 1);
promise.then(function (result) { saveByKey(key, result); });
return promise;
}
function saveByKey (key, result) {
var xhr;
if (result instanceof XMLHttpRequest || result instanceof FauxXHR) {
xhr = result;
} else if (result && (result.xhr instanceof XMLHttpRequest || result.xhr instanceof FauxXHR)) {
xhr = result.xhr;
}
if (xhr) {
if (xhr.status >= 200 && xhr.status < 300) {
io.cache.storage.store(key, {
status: xhr.status,
statusText: xhr.statusText,
responseType: xhr.responseType,
responseText: xhr.responseText,
headers: xhr.getAllResponseHeaders()
});
}
} else {
io.cache.storage.store(key, {
status: 200,
statusText: 'OK',
responseType: 'json',
response: result,
headers: 'Content-Type: application/json'
});
}
}
function save (options, result) {
options = io.processOptions(typeof options == 'string' ? {url: options, method: 'GET'} : options);
io.cache.saveByKey(io.makeKey(options), result);
}
function remove(options) {
if (typeof options == 'function') {
var keys = io.cache.storage.getKeys(),
regexp = new RegExp('^.{' + io.prefix.length + '}(.*)$');
for (var i = 0; i < keys.length; ++i) {
var key = keys[i], m = regexp.exec(key);
if (m && options(m[1])) {
io.cache.storage.remove(key);
}
}
return;
}
if (options instanceof RegExp) {
var keys = io.cache.storage.getKeys(),
regexp = new RegExp('^.{' + io.prefix.length + '}(.*)$');
for (var i = 0; i < keys.length; ++i) {
var key = keys[i], m = regexp.exec(key);
if (m && options.test(m[1])) {
io.cache.storage.remove(key);
}
}
return;
}
options = io.processOptions(typeof options == "string" ? {url: options, method: "GET"} : options);
var url = options.url;
if (url && url.charAt(url.length - 1) == "*") {
var prefix = url.slice(0, url.length - 1), pl = prefix.length,
keys = io.cache.storage.getKeys(),
regexp = new RegExp('^.{' + (io.prefix.length + 1) + '}\\w+\\-(.*)$');
for (var i = 0; i < keys.length; ++i) {
var key = keys[i], m = regexp.exec(key);
if (m && m[1].slice(0, pl) == prefix) {
io.cache.storage.remove(key);
}
}
return;
}
io.cache.storage.remove(io.makeKey(options));
}
function clear() { io.cache.storage.clear(); }
function makeStorage (type) {
return {
retrieve: function (key) {
var data = window[type].getItem(key);
return data === null ? void 0 : JSON.parse(data);
},
store: function (key, data) {
window[type].setItem(key, JSON.stringify(data));
},
remove: function (key) {
window[type].removeItem(key);
},
clear: function () {
window[type].clear();
},
getKeys: function() {
var storage = window[type], keys = [], prefix = io.prefix, pl = prefix.length;
for (var i = 0, n = storage.length; i < n; ++i) {
var key = storage.key(i);
if (prefix == key.slice(0, pl)) {
keys.push(key);
}
}
return keys;
}
};
}
// export
io.cache = {
saveByKey: saveByKey,
save: save,
remove: remove,
clear: clear,
makeStorage: makeStorage,
storage: makeStorage('sessionStorage')
};
return scaffold(io, 'cache', 50, cache);
});