ReferencesManager.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 =>
  50. this._storeReferencesKeys(keys)
  51. )
  52. this.indexAllReferences(false)
  53. }
  54. })
  55. }
  56. _storeReferencesKeys(newKeys) {
  57. // console.log '>> storing references keys'
  58. const oldKeys = this.$scope.$root._references.keys
  59. const keys = _.union(oldKeys, newKeys)
  60. window.dispatchEvent(
  61. new CustomEvent('project:references', {
  62. detail: keys,
  63. })
  64. )
  65. return (this.$scope.$root._references.keys = keys)
  66. }
  67. indexReferencesIfDocModified(doc, shouldBroadcast) {
  68. // avoid reindexing references if the bib file has not changed since the
  69. // last time they were indexed
  70. const docId = doc.doc_id
  71. const snapshot = doc._doc.snapshot
  72. const now = Date.now()
  73. const sha1 = CryptoJSSHA1(
  74. 'blob ' + snapshot.length + '\x00' + snapshot
  75. ).toString()
  76. const CACHE_LIFETIME = 6 * 3600 * 1000 // allow reindexing every 6 hours
  77. const cacheEntry = this.existingIndexHash[docId]
  78. const isCached =
  79. cacheEntry &&
  80. cacheEntry.timestamp > now - CACHE_LIFETIME &&
  81. cacheEntry.hash === sha1
  82. if (!isCached) {
  83. this.indexReferences([docId], shouldBroadcast)
  84. this.existingIndexHash[docId] = { hash: sha1, timestamp: now }
  85. }
  86. }
  87. indexReferences(docIds, shouldBroadcast) {
  88. const opts = {
  89. docIds,
  90. shouldBroadcast,
  91. _csrf: window.csrfToken,
  92. }
  93. return this.ide.$http
  94. .post(`/project/${this.$scope.project_id}/references/index`, opts)
  95. .then(response => {
  96. return this._storeReferencesKeys(response.data.keys)
  97. })
  98. }
  99. indexAllReferences(shouldBroadcast) {
  100. const opts = {
  101. shouldBroadcast,
  102. _csrf: window.csrfToken,
  103. }
  104. return this.ide.$http
  105. .post(`/project/${this.$scope.project_id}/references/indexAll`, opts)
  106. .then(response => {
  107. return this._storeReferencesKeys(response.data.keys)
  108. })
  109. }
  110. }
  111. function __guard__(value, transform) {
  112. return typeof value !== 'undefined' && value !== null
  113. ? transform(value)
  114. : undefined
  115. }