-
Notifications
You must be signed in to change notification settings - Fork 16
/
angular-promise-cache.js
270 lines (242 loc) · 9.46 KB
/
angular-promise-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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
@license
The MIT License (MIT)
Copyright (c) 2013 Chris Roberson
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.
*/
(function (root, factory) {
if(typeof define === 'function' && define.amd) {
define(['angular'], factory);
} else if(typeof module === 'object' && module.exports) {
module.exports = factory(require('angular'));
} else {
root['angular-promise-cache'] = factory(root['angular']);
}
}(this, function(angular) {
'use strict';
angular.module('angular-promise-cache', [])
.factory('promiseCache', ['$q', '$rootScope', function($q, $rootScope) {
var memos = {},
DEFAULT_TTL_IN_MS = 5000,
keyDelimiter = '$',
whitespaceRegex = /\s+/g,
dateReferences = {},
hasOwnProperty = Object.prototype.hasOwnProperty,
toString = Object.prototype.toString,
lsInaccessible = false,
store = function(key, complexValue) {
ls.setItem(key, JSON.stringify(complexValue));
},
remove = function(key) {
ls.removeItem(key);
},
fetch = function(key) {
var str = ls.getItem(key);
try {
str = JSON.parse(str);
}
catch (e) {
!lsInaccessible && console.warn('Unable to parse json response from local storage', str);
}
return str;
},
getTimestamp = function(key, strPromise) {
return parseInt(key.split(keyDelimiter)[1]) || dateReferences[strPromise];
},
formatCacheKey = function(ts) {
return keyDelimiter + ts + keyDelimiter;
},
getStrPromise = function(opts) {
return opts.key || opts.promise.toString().replace(whitespaceRegex, '')
},
isLsEnabled = function(opts) {
return !!opts.localStorageEnabled;
},
getLsKey = function(opts, strPromise) {
return opts.localStorageKey || strPromise;
},
memoize = function memoize(func, resolver) {
var keyPrefix = +new Date + '',
memoized = function() {
var cache = memoized.cache,
key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0];
return hasOwnProperty.call(cache, key)
? cache[key]
: (cache[key] = func.apply(this, arguments));
}
memoized.cache = {};
return memoized;
};
// This can be null/undefined or throw access exceptions
var ls = null;
try {
ls = window.localStorage;
} catch (e) {
// Do nothing - probably access denied
lsInaccessible = true;
}
ls = ls || {
setItem: function() { },
removeItem: function() { },
getItem: function() { }
};
var promiseCacheFunction = function(opts) {
// TODO: BETTER ERROR HANDLING
var promise = opts.promise,
ttl = parseInt(opts.ttl) || DEFAULT_TTL_IN_MS,
bustCache = !!opts.bustCache,
// v0.0.3: Adding ability to specify a callback function to forcefully expire the cache
// for a promise that returns a failure
expireOnFailure = opts.expireOnFailure,
args = opts.args,
now = new Date().getTime(),
strPromise = getStrPromise(opts),
// v0.0.5: Local storage support
lsEnabled = isLsEnabled(opts),
lsKey = getLsKey(opts, strPromise),
lsObj = fetch(lsKey),
lsTs,
lsMemoCache,
lsDuration,
lsDeferred;
dateReferences[strPromise] = dateReferences[strPromise] || now;
if (lsEnabled) {
if (!lsObj || typeof lsObj !== 'object' || !hasOwnProperty.call(lsObj, 'resolver') || !hasOwnProperty.call(lsObj, 'response')) {
lsObj = {};
}
else {
// v0.0.5: Local Storage support
// Extract the timestamp from the local storage object
// This timestamp represents the last time this promise
// expired
lsTs = getTimestamp(lsObj.resolver, strPromise);
// Determine how much longer it has to live
lsDuration = lsTs + ttl - now;
// Memoize the promise using the timestamp from the
// local storage object rather than dateReference
memos[strPromise] = memoize(promise, function() {
return formatCacheKey(lsTs);
});
// We want to fill the cache immediately but do not
// want to execute the promise and since the cache
// property is just a simple key/value object, we
// can create that and set it without any harm
lsMemoCache = memos[strPromise].cache || {};
lsDeferred = $q.defer();
lsDeferred.resolve(lsObj.response);
lsMemoCache[formatCacheKey(lsTs)] = lsDeferred.promise;
memos[strPromise].cache = lsMemoCache;
}
}
if (!hasOwnProperty.call(memos, strPromise)) {
memos[strPromise] = memoize(promise, function() {
return formatCacheKey(dateReferences[strPromise]);
});
memos[strPromise].opts = opts;
$rootScope.$broadcast('angular-promise-cache.new', formatCacheKey(dateReferences[strPromise]), strPromise);
}
else {
memos[strPromise].opts = opts;
memos[strPromise].cache = (function() {
var updatedCache = {},
cache = memos[strPromise].cache,
forceExpiration = !!memos[strPromise].forceExpiration,
key,
timestamp,
omit;
for (key in cache) {
timestamp = getTimestamp(key, strPromise);
// v0.0.7: TTL < 0 means it never expires
omit = bustCache || forceExpiration || (ttl > 0 && timestamp + ttl < now);
if (omit) {
$rootScope.$broadcast('angular-promise-cache.expired', key, strPromise);
dateReferences[strPromise] = now;
if (lsEnabled) {
lsTs = dateReferences[strPromise];
remove(lsKey);
}
}
else {
$rootScope.$broadcast('angular-promise-cache.active', key, timestamp + ttl, strPromise);
updatedCache[key] = cache[key];
}
}
// Always reset this after expiring the cache
// so it is not "stuck on"
memos[strPromise].forceExpiration = false;
return updatedCache;
}());
}
return memos[strPromise].apply(this, args || []).then(
function(response) {
if (lsEnabled) {
lsObj.response = arguments[0];
lsObj.resolver = formatCacheKey(lsTs || dateReferences[strPromise]);
store(lsKey, lsObj);
}
return response;
},
function(error) {
if (angular.isFunction(expireOnFailure) && expireOnFailure.apply(this, arguments)) {
var memo = memos[strPromise];
if (memo) {
memo.forceExpiration = true;
}
}
return $q.reject(error);
}
);
};
// v0.0.7
promiseCacheFunction.remove = function(key, keepInLS) {
// v0.0.13
var keys = [];
if (typeof key === 'object') {
var toStr = toString.call(key);
switch (toString.call(key)) {
case '[object RegExp]':
keys = Object.keys(memos).filter(function(_key) {
return key.test(_key);
});
break;
case '[object Array]':
keys = key.slice(0);
break;
default:
throw 'Unsupported parameter to .remove(). Acceptable paramters are: string, array, regexp';
}
}
else {
keys.push(key);
}
keys.forEach(function(key) {
if (!memos[key]) return;
var opts = memos[key].opts;
dateReferences[key] = new Date().getTime();
if (!keepInLS && isLsEnabled(opts)) {
remove(getLsKey(opts, key));
}
delete memos[key];
$rootScope.$broadcast('angular-promise-cache.removed', key);
});
};
promiseCacheFunction.removeAll = function(keepInLS) {
promiseCacheFunction.remove(Object.keys(memos), keepInLS);
};
return promiseCacheFunction;
}]);
return 'angular-promise-cache';
}));