Improve drive loading speed

pull/1/head
yflory 7 years ago
parent 06ede6b294
commit 7f88d1d43a

@ -1,8 +1,9 @@
define([ define([
'/common/common-util.js', '/common/common-util.js',
'/customize/messages.js',
'/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-crypto/crypto.js',
'/bower_components/tweetnacl/nacl-fast.min.js' '/bower_components/tweetnacl/nacl-fast.min.js'
], function (Util, Crypto) { ], function (Util, Messages, Crypto) {
var Nacl = window.nacl; var Nacl = window.nacl;
var Hash = {}; var Hash = {};
@ -361,5 +362,19 @@ Version 1
'/' + curvePublic.replace(/\//g, '-') + '/'; '/' + curvePublic.replace(/\//g, '-') + '/';
}; };
// Create untitled documents when no name is given
var getLocaleDate = function () {
if (window.Intl && window.Intl.DateTimeFormat) {
var options = {weekday: "short", year: "numeric", month: "long", day: "numeric"};
return new window.Intl.DateTimeFormat(undefined, options).format(new Date());
}
return new Date().toString().split(' ').slice(0,4).join(' ');
};
Hash.getDefaultName = function (parsed) {
var type = parsed.type;
var name = (Messages.type)[type] + ' - ' + getLocaleDate();
return name;
};
return Hash; return Hash;
}); });

@ -348,73 +348,13 @@ define([
/* /*
* localStorage formatting * localStorage formatting
*/ */
/*
the first time this gets called, your local storage will migrate to a
new format. No more indices for values, everything is named now.
* href
* atime (access time)
* title
* ??? // what else can we put in here?
*/
var checkObjectData = function (pad, cb) {
if (typeof(pad.atime) !== "number") { pad.atime = +new Date(pad.atime); }
if (!pad.ctime) { pad.ctime = pad.atime; }
if (typeof(pad.actime) !== "number") { pad.ctime = +new Date(pad.ctime); }
if (/^https*:\/\//.test(pad.href)) { pad.href = Hash.getRelativeHref(pad.href); }
var parsed = Hash.parsePadUrl(pad.href);
if (!parsed || !parsed.hash) { return; }
if (typeof(cb) === 'function') { cb(parsed); }
if (!pad.title) { pad.title = common.getDefaultName(parsed); }
return parsed.hashData;
};
// Remove everything from RecentPads that is not an object and check the objects
var checkRecentPads = common.checkRecentPads = function (pads) {
Object.keys(pads).forEach(function (id, i) {
var pad = pads[id];
if (pad && typeof(pad) === 'object') {
var parsedHash = checkObjectData(pad);
if (!parsedHash || !parsedHash.type) {
console.error("[Cryptpad.checkRecentPads] pad had unexpected value", pad);
getStore().removeData(i);
return;
}
return pad;
}
console.error("[Cryptpad.checkRecentPads] pad had unexpected value", pad);
getStore().removeData(i);
});
};
// Create untitled documents when no name is given
var getLocaleDate = function () {
if (window.Intl && window.Intl.DateTimeFormat) {
var options = {weekday: "short", year: "numeric", month: "long", day: "numeric"};
return new window.Intl.DateTimeFormat(undefined, options).format(new Date());
}
return new Date().toString().split(' ').slice(0,4).join(' ');
};
var getDefaultName = common.getDefaultName = function (parsed) {
var type = parsed.type;
var name = (Messages.type)[type] + ' - ' + getLocaleDate();
return name;
};
common.isDefaultName = function (parsed, title) {
var name = getDefaultName(parsed);
return title === name;
};
var makePad = common.makePad = function (href, title) { var makePad = common.makePad = function (href, title) {
var now = +new Date(); var now = +new Date();
return { return {
href: href, href: href,
atime: now, atime: now,
ctime: now, ctime: now,
title: title || getDefaultName(Hash.parsePadUrl(href)), title: title || Hash.getDefaultName(Hash.parsePadUrl(href)),
}; };
}; };
@ -595,7 +535,6 @@ define([
var getRecentPads = common.getRecentPads = function (cb) { var getRecentPads = common.getRecentPads = function (cb) {
getStore().getDrive('filesData', function (err, recentPads) { getStore().getDrive('filesData', function (err, recentPads) {
if (typeof(recentPads) === "object") { if (typeof(recentPads) === "object") {
checkRecentPads(recentPads);
cb(void 0, recentPads); cb(void 0, recentPads);
return; return;
} }
@ -723,7 +662,7 @@ define([
if (title.trim() === "") { if (title.trim() === "") {
var parsed = Hash.parsePadUrl(href || window.location.href); var parsed = Hash.parsePadUrl(href || window.location.href);
title = getDefaultName(parsed); title = Hash.getDefaultName(parsed);
} }
common.setPadTitle(title, href, function (err) { common.setPadTitle(title, href, function (err) {
@ -1290,8 +1229,8 @@ define([
var data = { var data = {
href: href, href: href,
title: Messages.driveReadmeTitle, title: Messages.driveReadmeTitle,
atime: new Date().toISOString(), atime: +new Date(),
ctime: new Date().toISOString() ctime: +new Date()
}; };
common.getFO().pushData(data, function (e, id) { common.getFO().pushData(data, function (e, id) {
if (e) { if (e) {

@ -80,5 +80,27 @@ define([], function () {
Cryptpad.feedback('Migrate-4', true); Cryptpad.feedback('Migrate-4', true);
userObject.version = version = 4; userObject.version = version = 4;
} }
// Migration 5: dates to Number
var migrateDates = function () {
var data = userObject.drive && userObject.drive.filesData;
if (data) {
for (var id in data) {
if (typeof data[id].ctime !== "number") {
data[id].ctime = +new Date(data[id].ctime);
}
if (typeof data[id].atime !== "number") {
data[id].atime = +new Date(data[id].atime);
}
}
}
};
if (version < 5) {
migrateDates();
Cryptpad.feedback('Migrate-5', true);
userObject.version = version = 5;
}
}; };
}); });

@ -109,7 +109,7 @@ define([
if (!secret.keys) { secret.keys = secret.key; } if (!secret.keys) { secret.keys = secret.key; }
var parsed = Utils.Hash.parsePadUrl(window.location.href); var parsed = Utils.Hash.parsePadUrl(window.location.href);
if (!parsed.type) { throw new Error(); } if (!parsed.type) { throw new Error(); }
var defaultTitle = Cryptpad.getDefaultName(parsed); var defaultTitle = Utils.Hash.getDefaultName(parsed);
var proxy = Cryptpad.getProxy(); var proxy = Cryptpad.getProxy();
var updateMeta = function () { var updateMeta = function () {
//console.log('EV_METADATA_UPDATE'); //console.log('EV_METADATA_UPDATE');

@ -1053,12 +1053,21 @@ define([
toClean.push(id); toClean.push(id);
continue; continue;
} }
if (/^https*:\/\//.test(el.href)) { el.href = Hash.getRelativeHref(el.href); }
if (!el.ctime) { el.ctime = el.atime; }
var parsed = Hash.parsePadUrl(el.href); var parsed = Hash.parsePadUrl(el.href);
if (!el.title) { el.title = Hash.getDefaultName(parsed); }
if (!parsed.hash) { if (!parsed.hash) {
debug("Removing an element in filesData with a invalid href.", el); debug("Removing an element in filesData with a invalid href.", el);
toClean.push(id); toClean.push(id);
continue; continue;
} }
if (!parsed.type) {
debug("Removing an element in filesData with a invalid type.", el);
toClean.push(id);
continue;
}
if ((loggedIn || config.testMode) && rootFiles.indexOf(id) === -1) { if ((loggedIn || config.testMode) && rootFiles.indexOf(id) === -1) {
debug("An element in filesData was not in ROOT, TEMPLATE or TRASH.", id, el); debug("An element in filesData was not in ROOT, TEMPLATE or TRASH.", id, el);

Loading…
Cancel
Save