Improve drive loading speed

This commit is contained in:
yflory 2017-11-21 11:09:59 +01:00
parent 06ede6b294
commit 7f88d1d43a
5 changed files with 52 additions and 67 deletions

View file

@ -1,8 +1,9 @@
define([
'/common/common-util.js',
'/customize/messages.js',
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/tweetnacl/nacl-fast.min.js'
], function (Util, Crypto) {
], function (Util, Messages, Crypto) {
var Nacl = window.nacl;
var Hash = {};
@ -361,5 +362,19 @@ Version 1
'/' + 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;
});

View file

@ -348,73 +348,13 @@ define([
/*
* 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 now = +new Date();
return {
href: href,
atime: 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) {
getStore().getDrive('filesData', function (err, recentPads) {
if (typeof(recentPads) === "object") {
checkRecentPads(recentPads);
cb(void 0, recentPads);
return;
}
@ -723,7 +662,7 @@ define([
if (title.trim() === "") {
var parsed = Hash.parsePadUrl(href || window.location.href);
title = getDefaultName(parsed);
title = Hash.getDefaultName(parsed);
}
common.setPadTitle(title, href, function (err) {
@ -1290,8 +1229,8 @@ define([
var data = {
href: href,
title: Messages.driveReadmeTitle,
atime: new Date().toISOString(),
ctime: new Date().toISOString()
atime: +new Date(),
ctime: +new Date()
};
common.getFO().pushData(data, function (e, id) {
if (e) {

View file

@ -80,5 +80,27 @@ define([], function () {
Cryptpad.feedback('Migrate-4', true);
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;
}
};
});

View file

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

View file

@ -1053,12 +1053,21 @@ define([
toClean.push(id);
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);
if (!el.title) { el.title = Hash.getDefaultName(parsed); }
if (!parsed.hash) {
debug("Removing an element in filesData with a invalid href.", el);
toClean.push(id);
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) {
debug("An element in filesData was not in ROOT, TEMPLATE or TRASH.", id, el);