ShareJsDB.js 2.3 KB

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