ReferencesManager.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import _ from 'lodash'
  2. /* eslint-disable
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS103: Rewrite code to no longer use __guard__
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. import CryptoJSSHA1 from 'crypto-js/sha1'
  17. let ReferencesManager
  18. export default ReferencesManager = class ReferencesManager {
  19. constructor(ide, $scope) {
  20. this.ide = ide
  21. this.$scope = $scope
  22. this.$scope.$root._references = this.state = { keys: [] }
  23. this.existingIndexHash = {}
  24. this.$scope.$on('document:closed', (e, doc) => {
  25. let entity
  26. if (doc.doc_id) {
  27. entity = this.ide.fileTreeManager.findEntityById(doc.doc_id)
  28. }
  29. if (
  30. __guard__(entity != null ? entity.name : undefined, x =>
  31. x.match(/.*\.bib$/)
  32. )
  33. ) {
  34. return this.indexReferencesIfDocModified(doc, true)
  35. }
  36. })
  37. this.$scope.$on('references:should-reindex', (e, data) => {
  38. return this.indexAllReferences(true)
  39. })
  40. // When we join the project:
  41. // index all references files
  42. // and don't broadcast to all clients
  43. this.inited = false
  44. this.$scope.$on('project:joined', e => {
  45. // We only need to grab the references when the editor first loads,
  46. // not on every reconnect
  47. if (!this.inited) {
  48. this.inited = true
  49. this.ide.socket.on('references:keys:updated', (keys, allDocs) =>
  50. this._storeReferencesKeys(keys, allDocs)
  51. )
  52. this.indexAllReferences(false)
  53. }
  54. })
  55. }
  56. _storeReferencesKeys(newKeys, replaceExistingKeys) {
  57. const oldKeys = this.$scope.$root._references.keys
  58. const keys = replaceExistingKeys ? newKeys : _.union(oldKeys, newKeys)
  59. window.dispatchEvent(
  60. new CustomEvent('project:references', {
  61. detail: keys,
  62. })
  63. )
  64. return (this.$scope.$root._references.keys = keys)
  65. }
  66. indexReferencesIfDocModified(doc, shouldBroadcast) {
  67. // avoid reindexing references if the bib file has not changed since the
  68. // last time they were indexed
  69. const docId = doc.doc_id
  70. const snapshot = doc._doc.snapshot
  71. const now = Date.now()
  72. const sha1 = CryptoJSSHA1(
  73. 'blob ' + snapshot.length + '\x00' + snapshot
  74. ).toString()
  75. const CACHE_LIFETIME = 6 * 3600 * 1000 // allow reindexing every 6 hours
  76. const cacheEntry = this.existingIndexHash[docId]
  77. const isCached =
  78. cacheEntry &&
  79. cacheEntry.timestamp > now - CACHE_LIFETIME &&
  80. cacheEntry.hash === sha1
  81. if (!isCached) {
  82. this.indexAllReferences(shouldBroadcast)
  83. this.existingIndexHash[docId] = { hash: sha1, timestamp: now }
  84. }
  85. }
  86. indexAllReferences(shouldBroadcast) {
  87. const opts = {
  88. shouldBroadcast,
  89. _csrf: window.csrfToken,
  90. }
  91. return this.ide.$http
  92. .post(`/project/${this.$scope.project_id}/references/indexAll`, opts)
  93. .then(response => {
  94. return this._storeReferencesKeys(response.data.keys, true)
  95. })
  96. }
  97. }
  98. function __guard__(value, transform) {
  99. return typeof value !== 'undefined' && value !== null
  100. ? transform(value)
  101. : undefined
  102. }