Просмотр исходного кода

Merge pull request #2468 from overleaf/bg-optmise-reindexing-of-refs

avoid reindexing references when bib file is unchanged

GitOrigin-RevId: 723c83a256baece49dcc9a7d51364a25ca3c861f
Brian Gough 6 лет назад
Родитель
Сommit
f42bb68e74
1 измененных файлов с 24 добавлено и 2 удалено
  1. 24 2
      services/web/frontend/js/ide/references/ReferencesManager.js

+ 24 - 2
services/web/frontend/js/ide/references/ReferencesManager.js

@@ -13,13 +13,14 @@
  * DS207: Consider shorter variations of null checks
  * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  */
-define([], function() {
+define(['crypto-js/sha1'], function(CryptoJSSHA1) {
   let ReferencesManager
   return (ReferencesManager = class ReferencesManager {
     constructor(ide, $scope) {
       this.ide = ide
       this.$scope = $scope
       this.$scope.$root._references = this.state = { keys: [] }
+      this.existingIndexHash = {}
 
       this.$scope.$on('document:closed', (e, doc) => {
         let entity
@@ -31,7 +32,7 @@ define([], function() {
             x.match(/.*\.bib$/)
           )
         ) {
-          return this.indexReferences([doc.doc_id], true)
+          return this.indexReferencesIfDocModified(doc, true)
         }
       })
 
@@ -70,6 +71,27 @@ define([], function() {
       return (this.$scope.$root._references.keys = _.union(oldKeys, newKeys))
     }
 
+    indexReferencesIfDocModified(doc, shouldBroadcast) {
+      // avoid reindexing references if the bib file has not changed since the
+      // last time they were indexed
+      const docId = doc.doc_id
+      const snapshot = doc._doc.snapshot
+      const now = Date.now()
+      const sha1 = CryptoJSSHA1(
+        'blob ' + snapshot.length + '\x00' + snapshot
+      ).toString()
+      const CACHE_LIFETIME = 6 * 3600 * 1000 // allow reindexing every 6 hours
+      const cacheEntry = this.existingIndexHash[docId]
+      const isCached =
+        cacheEntry &&
+        cacheEntry.timestamp > now - CACHE_LIFETIME &&
+        cacheEntry.hash === sha1
+      if (!isCached) {
+        this.indexReferences([docId], shouldBroadcast)
+        this.existingIndexHash[docId] = { hash: sha1, timestamp: now }
+      }
+    }
+
     indexReferences(docIds, shouldBroadcast) {
       const opts = {
         docIds,