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

73 lines
2.5 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:40:42 -05:00
ShareJsModel = require "./sharejs/server/model"
ShareJsDB = require "./ShareJsDB"
async = require "async"
logger = require "logger-sharelatex"
Settings = require('settings-sharelatex')
Keys = require "./UpdateKeys"
2014-02-12 05:40:42 -05:00
{EventEmitter} = require "events"
util = require "util"
2014-10-07 07:08:36 -04:00
redis = require("redis-sharelatex")
rclient = redis.createClient(Settings.redis.web)
2014-02-12 05:40:42 -05:00
ShareJsModel:: = {}
util.inherits ShareJsModel, EventEmitter
module.exports = ShareJsUpdateManager =
getNewShareJsModel: () ->
db = new ShareJsDB()
model = new ShareJsModel(db, maxDocLength: Settings.max_doc_length)
model.db = db
return model
2014-02-12 05:40:42 -05:00
applyUpdate: (project_id, doc_id, update, callback = (error, updatedDocLines) ->) ->
logger.log project_id: project_id, doc_id: doc_id, update: update, "applying sharejs updates"
2014-02-12 05:40:42 -05:00
jobs = []
# We could use a global model for all docs, but we're hitting issues with the
# internal state of ShareJS not being accessible for clearing caches, and
# getting stuck due to queued callbacks (line 260 of sharejs/server/model.coffee)
# This adds a small but hopefully acceptable overhead (~12ms per 1000 updates on
# my 2009 MBP).
model = @getNewShareJsModel()
@_listenForOps(model)
doc_key = Keys.combineProjectIdAndDocId(project_id, doc_id)
model.applyOp doc_key, update, (error) ->
2014-02-12 05:40:42 -05:00
if error?
if error == "Op already submitted"
logger.warn {project_id, doc_id, update}, "op has already been submitted"
update.dup = true
ShareJsUpdateManager._sendOp(project_id, doc_id, update)
else
ShareJsUpdateManager._sendError(project_id, doc_id, error)
return callback(error)
logger.log project_id: project_id, doc_id: doc_id, error: error, "applied update"
2014-02-12 05:40:42 -05:00
model.getSnapshot doc_key, (error, data) =>
if error?
ShareJsUpdateManager._sendError(project_id, doc_id, error)
2014-02-12 05:40:42 -05:00
return callback(error)
docLines = data.snapshot.split(/\r\n|\n|\r/)
callback(null, docLines, data.v, model.db.appliedOps[doc_key] or [])
2014-02-12 05:40:42 -05:00
_listenForOps: (model) ->
model.on "applyOp", (doc_key, opData) ->
[project_id, doc_id] = Keys.splitProjectIdAndDocId(doc_key)
ShareJsUpdateManager._sendOp(project_id, doc_id, opData)
_sendOp: (project_id, doc_id, opData) ->
data =
project_id: project_id
doc_id: doc_id
op: opData
data = JSON.stringify data
rclient.publish "applied-ops", data
2014-02-12 05:40:42 -05:00
_sendError: (project_id, doc_id, error) ->
data = JSON.stringify
project_id: project_id
doc_id: doc_id
error: error.message || error
rclient.publish "applied-ops", data