ReferencesManager.js 3.5 KB

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