UNPKG

angular-cached-resource

Version:

An AngularJS module to interact with RESTful resources, even when browser is offline

115 lines (103 loc) 3.4 kB
// Generated by CoffeeScript 1.10.0 var Cache, localStorage; localStorage = window.localStorage; Cache = (function() { Cache.prototype.memoryCache = {}; function Cache(arg) { this.$log = arg.$log, this.localStoragePrefix = arg.localStoragePrefix; } Cache.prototype.getItem = function(key, fallbackValue) { var item, out; key = this._buildKey(key); item = this.memoryCache[key]; if (item == null) { item = localStorage.getItem(key); } out = item != null ? angular.fromJson(item) : fallbackValue; this.$log.debug("CACHE GET: " + key, out); return out; }; Cache.prototype.setItem = function(key, value) { var e, error, stringValue; key = this._buildKey(key); stringValue = angular.toJson(value); try { localStorage.setItem(key, stringValue); if (this.memoryCache[key] != null) { delete this.memoryCache[key]; } } catch (error) { e = error; this.$log.error("Failed to write to localStorage.", { error: e, key: key, value: stringValue }); this.memoryCache[key] = stringValue; } this.$log.debug("CACHE PUT: " + key, angular.fromJson(angular.toJson(value))); return value; }; Cache.prototype.clear = function(arg) { var cacheKey, cacheKeys, exceptFor, exception, i, j, k, key, l, len, len1, len2, m, ref, ref1, results, results1, skipKey, where; ref = arg != null ? arg : {}, key = ref.key, exceptFor = ref.exceptFor, where = ref.where; if (where && exceptFor) { return this.$log.error("Using where and exceptFor arguments at once in clear() method is forbidden!"); } if (exceptFor) { if (exceptFor == null) { exceptFor = []; } cacheKeys = []; for (i = j = 0, ref1 = localStorage.length; 0 <= ref1 ? j < ref1 : j > ref1; i = 0 <= ref1 ? ++j : --j) { cacheKey = localStorage.key(i); if (!this._cacheKeyHasPrefix(cacheKey, key)) { continue; } skipKey = false; for (k = 0, len = exceptFor.length; k < len; k++) { exception = exceptFor[k]; if (!(this._cacheKeyHasPrefix(cacheKey, exception))) { continue; } skipKey = true; break; } if (skipKey) { continue; } cacheKeys.push(cacheKey); } results = []; for (l = 0, len1 = cacheKeys.length; l < len1; l++) { cacheKey = cacheKeys[l]; results.push(localStorage.removeItem(cacheKey)); } return results; } else { results1 = []; for (m = 0, len2 = where.length; m < len2; m++) { cacheKey = where[m]; results1.push(localStorage.removeItem(this._buildKey(cacheKey))); } return results1; } }; Cache.prototype._buildKey = function(key) { return "" + this.localStoragePrefix + key; }; Cache.prototype._cacheKeyHasPrefix = function(cacheKey, prefix) { var index, nextChar; if (prefix == null) { return cacheKey.indexOf(this.localStoragePrefix) === 0; } prefix = this._buildKey(prefix); index = cacheKey.indexOf(prefix); nextChar = cacheKey[prefix.length]; return index === 0 && ((nextChar == null) || (nextChar === '?' || nextChar === '/')); }; return Cache; })(); module.exports = function(providerParams) { return new Cache(providerParams); };