let the abstraction take care of stringification

pull/1/head
ansuz 8 years ago
parent 9b16d529ca
commit 551d38c930

@ -12,8 +12,8 @@ define(function () {
// Store uses nodebacks... // Store uses nodebacks...
Store.set = function (key, val, cb) { Store.set = function (key, val, cb) {
localStorage.setItem(key, val); localStorage.setItem(key, JSON.stringify(val));
cb(void 0, val); cb();
}; };
// implement in alternative store // implement in alternative store
@ -24,15 +24,26 @@ define(function () {
cb(void 0, map); cb(void 0, map);
}; };
var safeGet = window.safeGet = function (key) {
var val = localStorage.getItem(key);
try {
return JSON.parse(val);
} catch (err) {
console.log(val);
console.error(err);
return val;
}
};
Store.get = function (key, cb) { Store.get = function (key, cb) {
cb(void 0, localStorage.getItem(key)); cb(void 0, safeGet(key));
}; };
// implement in alternative store // implement in alternative store
Store.getBatch = function (keys, cb) { Store.getBatch = function (keys, cb) {
var res = {}; var res = {};
keys.forEach(function (key) { keys.forEach(function (key) {
res[key] = localStorage.getItem(key); res[key] = safeGet(key);
}); });
cb(void 0, res); cb(void 0, res);
}; };
@ -50,13 +61,8 @@ define(function () {
cb(); cb();
}; };
// implement in alternative store... Store.keys = function (cb) {
Store.dump = function (cb) { cb(void 0, Object.keys(localStorage));
var map = {};
Object.keys(localStorage).forEach(function (key) {
map[key] = localStorage.getItem(key);
});
cb(void 0, map);
}; };
return Store; return Store;

@ -29,7 +29,6 @@ define([
return secret; return secret;
}; };
var storageKey = common.storageKey = 'CryptPad_RECENTPADS'; var storageKey = common.storageKey = 'CryptPad_RECENTPADS';
/* /*
@ -90,24 +89,18 @@ define([
/* fetch and migrate your pad history from localStorage */ /* fetch and migrate your pad history from localStorage */
var getRecentPads = common.getRecentPads = function (cb) { var getRecentPads = common.getRecentPads = function (cb) {
Store.get(storageKey, function (err, recentPadsStr) { Store.get(storageKey, function (err, recentPads) {
var recentPads = []; if (isArray(recentPads)) {
if (recentPadsStr) { cb(void 0, migrateRecentPads(recentPads));
try { return;
recentPads = JSON.parse(recentPadsStr);
} catch (E) {
// couldn't parse the localStorage?
// just overwrite it.
}
} }
cb(void 0, []);
cb(void 0, migrateRecentPads(recentPads));
}); });
}; };
/* commit a list of pads to localStorage */ /* commit a list of pads to localStorage */
var setRecentPads = common.setRecentPads = function (pads, cb) { var setRecentPads = common.setRecentPads = function (pads, cb) {
Store.set(storageKey, JSON.stringify(pads), function (err, data) { Store.set(storageKey, pads, function (err, data) {
cb(err, data); cb(err, data);
}); });
}; };
@ -125,24 +118,24 @@ define([
} }
getRecentPads(function (err, recentPads) { getRecentPads(function (err, recentPads) {
console.log(recentPads);
setRecentPads(recentPads.filter(function (pad) { setRecentPads(recentPads.filter(function (pad) {
return pad.href !== href; if (pad.href !== href) {
return true;
}
}), function (err, data) { }), function (err, data) {
if (err) { if (err) {
cb(err); cb(err);
return; return;
} }
Store.dump(function (err, storage) { Store.keys(function (err, keys) {
if (err) { if (err) {
cb(err); cb(err);
return; return;
} }
var toRemove = []; var toRemove = keys.filter(function (k) {
Object.keys(storage).forEach(function (k) { return k.indexOf(hash) === 0;
if (k.indexOf(hash) === 0) {
toRemove.push(k);
}
}); });
Store.removeBatch(toRemove, function (err, data) { Store.removeBatch(toRemove, function (err, data) {
@ -153,6 +146,16 @@ define([
}); });
}; };
var makePad = function (href, title) {
var now = new Date();
return {
href: href,
atime: now,
ctime: now,
title: title || window.location.hash.slice(1, 9),
};
};
var rememberPad = common.rememberPad = window.rememberPad = function (title, cb) { var rememberPad = common.rememberPad = window.rememberPad = function (title, cb) {
// bail out early // bail out early
if (!/#/.test(window.location.hash)) { return; } if (!/#/.test(window.location.hash)) { return; }
@ -182,13 +185,8 @@ define([
}); });
if (!isUpdate) { if (!isUpdate) {
// href, atime, name // href, ctime, atime, title
out.push({ out.push(makePad(href, title));
href: href,
atime: now,
ctime: now,
title: title || window.location.hash.slice(1,9),
});
} }
setRecentPads(out, function (err, data) { setRecentPads(out, function (err, data) {
cb(err, data); cb(err, data);
@ -204,8 +202,12 @@ define([
cb(err); cb(err);
return; return;
} }
var contains;
var renamed = recent.map(function (pad) { var renamed = recent.map(function (pad) {
if (pad.href === href) { if (pad.href === href) {
contains = true;
// update the atime // update the atime
pad.atime = new Date().toISOString(); pad.atime = new Date().toISOString();
@ -215,6 +217,10 @@ define([
return pad; return pad;
}); });
if (!contains) {
renamed.push(makePad(href, name));
}
setRecentPads(renamed, function (err, data) { setRecentPads(renamed, function (err, data) {
cb(err, data); cb(err, data);
}); });

Loading…
Cancel
Save