cache-storage
Version:
[ABANDONED] Advanced cache storage for node js
90 lines (71 loc) • 2.49 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var BrowserLocalStorage, Cache, Storage,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Storage = require('./Storage');
Cache = require('../../Cache');
BrowserLocalStorage = (function(_super) {
__extends(BrowserLocalStorage, _super);
BrowserLocalStorage.TEST_VALUE = '__--cache-storage--__';
BrowserLocalStorage.prototype.allData = null;
BrowserLocalStorage.prototype.data = null;
BrowserLocalStorage.prototype.meta = null;
function BrowserLocalStorage() {
if (!BrowserLocalStorage.isSupported()) {
throw new Error('Cache storage: Local storage is not supported');
}
}
BrowserLocalStorage.isSupported = function() {
var e;
try {
localStorage.setItem(BrowserLocalStorage.TEST_VALUE, BrowserLocalStorage.TEST_VALUE);
localStorage.getItem(BrowserLocalStorage.TEST_VALUE);
return true;
} catch (_error) {
e = _error;
return false;
}
};
BrowserLocalStorage.prototype.getName = function() {
return '__' + this.cache.namespace;
};
BrowserLocalStorage.prototype.loadData = function() {
var data;
if (this.allData === null) {
data = localStorage.getItem(this.getName());
if (data === null) {
this.allData = {
data: {},
meta: {}
};
} else {
this.allData = JSON.parse(data);
}
}
return this.allData;
};
BrowserLocalStorage.prototype.getData = function() {
if (this.data === null) {
this.data = this.loadData().data;
}
return this.data;
};
BrowserLocalStorage.prototype.getMeta = function() {
if (this.meta === null) {
this.meta = this.loadData().meta;
}
return this.meta;
};
BrowserLocalStorage.prototype.writeData = function(data, meta) {
this.data = data;
this.meta = meta;
return localStorage.setItem(this.getName(), JSON.stringify({
data: this.data,
meta: this.meta
}));
};
return BrowserLocalStorage;
})(Storage);
module.exports = BrowserLocalStorage;
}).call(this);