ShareJsDB.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* eslint-disable
  2. camelcase,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let ShareJsDB
  15. const Keys = require('./UpdateKeys')
  16. const RedisManager = require('./RedisManager')
  17. const Errors = require('./Errors')
  18. module.exports = ShareJsDB = class ShareJsDB {
  19. constructor(project_id, doc_id, lines, version) {
  20. this.project_id = project_id
  21. this.doc_id = doc_id
  22. this.lines = lines
  23. this.version = version
  24. this.appliedOps = {}
  25. // ShareJS calls this detacted from the instance, so we need
  26. // bind it to keep our context that can access @appliedOps
  27. this.writeOp = this._writeOp.bind(this)
  28. }
  29. getOps(doc_key, start, end, callback) {
  30. if (start === end) {
  31. return callback(null, [])
  32. }
  33. // In redis, lrange values are inclusive.
  34. if (end != null) {
  35. end--
  36. } else {
  37. end = -1
  38. }
  39. const [project_id, doc_id] = Array.from(
  40. Keys.splitProjectIdAndDocId(doc_key)
  41. )
  42. return RedisManager.getPreviousDocOps(doc_id, start, end, callback)
  43. }
  44. _writeOp(doc_key, opData, callback) {
  45. if (this.appliedOps[doc_key] == null) {
  46. this.appliedOps[doc_key] = []
  47. }
  48. this.appliedOps[doc_key].push(opData)
  49. return callback()
  50. }
  51. getSnapshot(doc_key, callback) {
  52. if (
  53. doc_key !== Keys.combineProjectIdAndDocId(this.project_id, this.doc_id)
  54. ) {
  55. return callback(
  56. new Errors.NotFoundError(
  57. `unexpected doc_key ${doc_key}, expected ${Keys.combineProjectIdAndDocId(
  58. this.project_id,
  59. this.doc_id
  60. )}`
  61. )
  62. )
  63. } else {
  64. return callback(null, {
  65. snapshot: this.lines.join('\n'),
  66. v: parseInt(this.version, 10),
  67. type: 'text',
  68. })
  69. }
  70. }
  71. // To be able to remove a doc from the ShareJS memory
  72. // we need to called Model::delete, which calls this
  73. // method on the database. However, we will handle removing
  74. // it from Redis ourselves
  75. delete(docName, dbMeta, callback) {
  76. return callback()
  77. }
  78. }