From 088276b482cf29e244a5b982252dd8b97a1532f6 Mon Sep 17 00:00:00 2001 From: Ev Bogue Date: Sun, 14 Jul 2019 15:05:12 -0500 Subject: cache names to avoid scanning the entire log every time a name loads --- bog.js | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/bog.js b/bog.js index c378ff4..e767243 100644 --- a/bog.js +++ b/bog.js @@ -59,22 +59,33 @@ function getName (id, keys) { name.textContent = id.substring(0, 10) + '...' - bog().then(log => { - if (log) { - for (var i = 0; i < log.length; i++ ) { - if ((log[i].named === id) && (log[i].author === keys.publicKey)) { - // if you've identified someone as something else show that something else - return name.textContent = '@' + log[i].name - } else if ((log[i].named === id) && (log[i].author === id)) { - // else if show the name they gave themselves - return name.textContent = '@' + log[i].name - } - // there should probably be some sort of sybil attack resiliance here (weight avatar name based on number of times used by individuals), but this will do for now. - } - } + localforage.getItem('name:' + id).then(cache => { + if (cache) { + console.log('GOT NAME FROM CACHE: ' + cache) + return name.textContent = '@' + cache + } else { + bog().then(log => { + if (log) { + for (var i = 0; i < log.length; i++ ) { + if ((log[i].named === id) && (log[i].author === keys.publicKey)) { + // if you've identified someone as something else show that something else + localforage.setItem('name:' + id, log[i].name) + console.log('FINDING NAME AND SAVING TO CACHE: ' + log[i].name) + return name.textContent = '@' + log[i].name + } else if ((log[i].named === id) && (log[i].author === id)) { + // else if show the name they gave themselves + localforage.setItem('name:' + id, log[i].name) + console.log('FINDING NAME AND SAVING TO CACHE: ' + log[i].name) + return name.textContent = '@' + log[i].name + } + // there should probably be some sort of sybil attack resiliance here (weight avatar name based on number of times used by individuals), but this will do for now. + } + } + }) + } }) - return name + } // bog.regenerate -- regenerates main log by taking all of the feed logs, combinging them, and then sorting them -- cgit v1.2.3-70-g09d2