let the abstraction take care of stringification

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

@ -12,8 +12,8 @@ define(function () {
// Store uses nodebacks...
Store.set = function (key, val, cb) {
localStorage.setItem(key, val);
cb(void 0, val);
localStorage.setItem(key, JSON.stringify(val));
cb();
};
// implement in alternative store
@ -24,15 +24,26 @@ define(function () {
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) {
cb(void 0, localStorage.getItem(key));
cb(void 0, safeGet(key));
};
// implement in alternative store
Store.getBatch = function (keys, cb) {
var res = {};
keys.forEach(function (key) {
res[key] = localStorage.getItem(key);
res[key] = safeGet(key);
});
cb(void 0, res);
};
@ -50,13 +61,8 @@ define(function () {
cb();
};
// implement in alternative store...
Store.dump = function (cb) {
var map = {};
Object.keys(localStorage).forEach(function (key) {
map[key] = localStorage.getItem(key);
});
cb(void 0, map);
Store.keys = function (cb) {
cb(void 0, Object.keys(localStorage));
};
return Store;

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

Loading…
Cancel
Save