diff options
| author | Ev Bogue <ev@evbogue.com> | 2019-07-14 15:05:12 -0500 | 
|---|---|---|
| committer | Ev Bogue <ev@evbogue.com> | 2019-07-14 15:05:12 -0500 | 
| commit | 088276b482cf29e244a5b982252dd8b97a1532f6 (patch) | |
| tree | eb18dae3d54109e5dd3bc42357d40724c5344551 | |
| parent | ad394bdbe7dbff6dc386ec1699454806f97d2f87 (diff) | |
cache names to avoid scanning the entire log every time a name loads
| -rw-r--r-- | bog.js | 39 | 
1 files changed, 25 insertions, 14 deletions
| @@ -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 | 
