ShareJsDB.coffee 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. Keys = require('./UpdateKeys')
  2. RedisManager = require "./RedisManager"
  3. Errors = require "./Errors"
  4. module.exports = class ShareJsDB
  5. constructor: (@project_id, @doc_id, @lines, @version) ->
  6. @appliedOps = {}
  7. # ShareJS calls this detacted from the instance, so we need
  8. # bind it to keep our context that can access @appliedOps
  9. @writeOp = @_writeOp.bind(@)
  10. getOps: (doc_key, start, end, callback) ->
  11. if start == end
  12. return callback null, []
  13. # In redis, lrange values are inclusive.
  14. if end?
  15. end--
  16. else
  17. end = -1
  18. [project_id, doc_id] = Keys.splitProjectIdAndDocId(doc_key)
  19. RedisManager.getPreviousDocOps doc_id, start, end, callback
  20. _writeOp: (doc_key, opData, callback) ->
  21. @appliedOps[doc_key] ?= []
  22. @appliedOps[doc_key].push opData
  23. callback()
  24. getSnapshot: (doc_key, callback) ->
  25. if doc_key != Keys.combineProjectIdAndDocId(@project_id, @doc_id)
  26. return callback(new Errors.NotFoundError("unexpected doc_key #{doc_key}, expected #{Keys.combineProjectIdAndDocId(@project_id, @doc_id)}"))
  27. else
  28. return callback null, {
  29. snapshot: @lines.join("\n")
  30. v: parseInt(@version, 10)
  31. type: "text"
  32. }
  33. # To be able to remove a doc from the ShareJS memory
  34. # we need to called Model::delete, which calls this
  35. # method on the database. However, we will handle removing
  36. # it from Redis ourselves
  37. delete: (docName, dbMeta, callback) -> callback()