2011-07-23 01:22:57 +00:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var db;
|
|
|
|
|
|
|
|
function Stringify(obj) {
|
|
|
|
var str;
|
|
|
|
try {
|
|
|
|
str = JSON.stringify(obj);
|
|
|
|
} catch(e) {
|
|
|
|
str = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Parse(str) {
|
|
|
|
var obj = null;
|
|
|
|
try {
|
|
|
|
obj = JSON.parse(str);
|
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
function JsonStorage(w3cStorage) {
|
|
|
|
this.db = w3cStorage;
|
|
|
|
this.keysAreDirty = true;
|
|
|
|
}
|
|
|
|
db = JsonStorage;
|
|
|
|
|
|
|
|
db.prototype.clear = function () {
|
|
|
|
this.keysAreDirty = true;
|
2011-09-05 21:40:52 +00:00
|
|
|
this.keys().forEach(function (uuid) {
|
2011-07-23 01:22:57 +00:00
|
|
|
this.remove(uuid);
|
|
|
|
}, this);
|
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.remove = function (uuid) {
|
|
|
|
this.keysAreDirty = true;
|
|
|
|
this.db.removeItem(uuid);
|
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.get = function (uuid) {
|
|
|
|
return Parse(this.db.getItem(uuid));
|
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.set = function (uuid, val) {
|
|
|
|
this.keysAreDirty = true;
|
|
|
|
return this.db.setItem(uuid, Stringify(val));
|
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.keys = function () {
|
2011-09-05 21:40:52 +00:00
|
|
|
var i
|
|
|
|
;
|
2011-07-23 01:22:57 +00:00
|
|
|
|
|
|
|
if (!this.keysAreDirty) {
|
2011-09-05 21:40:52 +00:00
|
|
|
return this.__keys.concat([]);
|
2011-07-23 01:22:57 +00:00
|
|
|
}
|
|
|
|
|
2011-09-05 21:40:52 +00:00
|
|
|
this.__keys = [];
|
2011-07-23 01:22:57 +00:00
|
|
|
for (i = 0; i < this.db.length; i += 1) {
|
2011-09-05 21:40:52 +00:00
|
|
|
this.__keys.push(this.db.key(i));
|
2011-07-23 01:22:57 +00:00
|
|
|
}
|
|
|
|
this.keysAreDirty = false;
|
2011-09-05 21:40:52 +00:00
|
|
|
|
|
|
|
return this.__keys.concat([]);
|
2011-07-23 01:22:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.size = function () {
|
|
|
|
return this.db.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
function create(w3cStorage) {
|
|
|
|
return new JsonStorage(w3cStorage);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = create;
|
|
|
|
}());
|