From 2b57d82e8aad62747251f1d3b56ad8aec41b9444 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 6 Sep 2019 15:07:27 +0200 Subject: [PATCH] Optimize findStronger in common-hash --- www/common/common-hash.js | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/www/common/common-hash.js b/www/common/common-hash.js index e49b0e217..0449f405d 100644 --- a/www/common/common-hash.js +++ b/www/common/common-hash.js @@ -462,33 +462,28 @@ Version 1 Hash.findStronger = function (href, channel, recents) { var parsed = parsePadUrl(href); if (!parsed.hash) { return false; } + var parsedHash = parsed.hashData; + // We can't have a stronger hash if we're already in edit mode - if (parsed.hashData && parsed.hashData.mode === 'edit') { return; } + if (!parsedHash || parsedHash.mode === 'edit') { return; } + + // We don't have stronger/weaker versions of files or users + if (parsedHash.type !== 'pad') { return; } + var stronger; Object.keys(recents).some(function (id) { var pad = recents[id]; - if (!pad.href) { - // This pad doesn't have an edit link, so it can't be stronger - return; - } - var p = parsePadUrl(pad.href); - if (p.type !== parsed.type) { return; } // Not the same type - if (p.hash === parsed.hash) { return; } // Same hash, not stronger - if (channel !== pad.channel) { return; } // Not the same channel - var pHash = p.hashData; - var parsedHash = parsed.hashData; - if (!parsedHash || !pHash) { return; } + // Not the same channel? reject + if (channel !== pad.channel) { return; } - // We don't have stronger/weaker versions of files or users - if (pHash.type !== 'pad' && parsedHash.type !== 'pad') { return; } + // If this pad doesn't have an edit link, it can't be stronger + if (!pad.href || !pad.roHref) { return; } - if (pHash.version !== parsedHash.version) { return; } - if (pHash.mode === 'edit' && parsedHash.mode === 'view') { - stronger = pad; - return true; - } - return; + // This is a pad with an EDIT href and using the same channel as our target + // ==> it is stronger + stronger = pad; + return true; }); return stronger; };