ReferencesManager.js 3.5 KB

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