2020-06-23 04:45:38 -04:00
|
|
|
import _ from 'lodash'
|
2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
import CryptoJSSHA1 from 'crypto-js/sha1'
|
|
|
|
let ReferencesManager
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
export default ReferencesManager = class ReferencesManager {
|
2020-05-19 05:02:56 -04:00
|
|
|
constructor(ide, $scope) {
|
|
|
|
this.ide = ide
|
|
|
|
this.$scope = $scope
|
|
|
|
this.$scope.$root._references = this.state = { keys: [] }
|
|
|
|
this.existingIndexHash = {}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.$on('document:closed', (e, doc) => {
|
|
|
|
let entity
|
|
|
|
if (doc.doc_id) {
|
|
|
|
entity = this.ide.fileTreeManager.findEntityById(doc.doc_id)
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
__guard__(entity != null ? entity.name : undefined, x =>
|
|
|
|
x.match(/.*\.bib$/)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return this.indexReferencesIfDocModified(doc, true)
|
|
|
|
}
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.$on('references:should-reindex', (e, data) => {
|
|
|
|
return this.indexAllReferences(true)
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
// When we join the project:
|
|
|
|
// index all references files
|
|
|
|
// and don't broadcast to all clients
|
|
|
|
this.inited = false
|
|
|
|
this.$scope.$on('project:joined', e => {
|
|
|
|
// We only need to grab the references when the editor first loads,
|
|
|
|
// not on every reconnect
|
|
|
|
if (!this.inited) {
|
|
|
|
this.inited = true
|
|
|
|
this.ide.socket.on('references:keys:updated', keys =>
|
|
|
|
this._storeReferencesKeys(keys)
|
|
|
|
)
|
|
|
|
this.indexAllReferences(false)
|
2019-12-17 11:03:04 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_storeReferencesKeys(newKeys) {
|
|
|
|
// console.log '>> storing references keys'
|
|
|
|
const oldKeys = this.$scope.$root._references.keys
|
|
|
|
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 }
|
2019-12-17 11:03:04 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2019-12-17 11:03:04 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
indexReferences(docIds, shouldBroadcast) {
|
|
|
|
const opts = {
|
|
|
|
docIds,
|
|
|
|
shouldBroadcast,
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return this.ide.$http
|
|
|
|
.post(`/project/${this.$scope.project_id}/references/index`, opts)
|
|
|
|
.then(response => {
|
|
|
|
return this._storeReferencesKeys(response.data.keys)
|
|
|
|
})
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
indexAllReferences(shouldBroadcast) {
|
|
|
|
const opts = {
|
|
|
|
shouldBroadcast,
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return this.ide.$http
|
|
|
|
.post(`/project/${this.$scope.project_id}/references/indexAll`, opts)
|
|
|
|
.then(response => {
|
|
|
|
return this._storeReferencesKeys(response.data.keys)
|
|
|
|
})
|
|
|
|
}
|
2020-12-15 05:23:54 -05:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|