overleaf/services/document-updater/app/coffee/ShareJsDB.coffee

45 lines
1.4 KiB
CoffeeScript
Raw Normal View History

Keys = require('./UpdateKeys')
2014-02-12 05:40:42 -05:00
RedisManager = require "./RedisManager"
Errors = require "./Errors"
module.exports = class ShareJsDB
2016-11-28 05:14:42 -05:00
constructor: (@project_id, @doc_id, @lines, @version) ->
@appliedOps = {}
# ShareJS calls this detacted from the instance, so we need
# bind it to keep our context that can access @appliedOps
@writeOp = @_writeOp.bind(@)
2014-02-12 05:40:42 -05:00
getOps: (doc_key, start, end, callback) ->
if start == end
return callback null, []
# In redis, lrange values are inclusive.
if end?
end--
else
end = -1
[project_id, doc_id] = Keys.splitProjectIdAndDocId(doc_key)
RedisManager.getPreviousDocOps doc_id, start, end, callback
2014-02-12 05:40:42 -05:00
_writeOp: (doc_key, opData, callback) ->
@appliedOps[doc_key] ?= []
@appliedOps[doc_key].push opData
callback()
2014-02-12 05:40:42 -05:00
getSnapshot: (doc_key, callback) ->
2016-11-28 05:14:42 -05:00
if doc_key != Keys.combineProjectIdAndDocId(@project_id, @doc_id)
return callback(new Errors.NotFoundError("unexpected doc_key #{doc_key}, expected #{Keys.combineProjectIdAndDocId(@project_id, @doc_id)}"))
else
return callback null, {
snapshot: @lines.join("\n")
v: parseInt(@version, 10)
type: "text"
}
2014-02-12 05:40:42 -05:00
# To be able to remove a doc from the ShareJS memory
# we need to called Model::delete, which calls this
# method on the database. However, we will handle removing
# it from Redis ourselves
delete: (docName, dbMeta, callback) -> callback()