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')
|
2016-06-07 12:58:18 -04:00
|
|
|
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 =
|
2015-11-06 07:52:03 -05:00
|
|
|
getNewShareJsModel: () -> new ShareJsModel(ShareJsDB, maxDocLength: Settings.max_doc_length)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
applyUpdates: (project_id, doc_id, updates, callback = (error, updatedDocLines) ->) ->
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, updates: updates, "applying sharejs updates"
|
|
|
|
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)
|
|
|
|
for update in updates
|
|
|
|
do (update) =>
|
|
|
|
jobs.push (callback) =>
|
2015-11-19 05:54:28 -05:00
|
|
|
model.applyOp doc_key, update, (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)
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
callback(error)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
async.series jobs, (error) =>
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, error: error, "applied updates"
|
|
|
|
if error?
|
|
|
|
@_sendError(project_id, doc_id, error)
|
|
|
|
return callback(error)
|
|
|
|
model.getSnapshot doc_key, (error, data) =>
|
|
|
|
if error?
|
|
|
|
@_sendError(project_id, doc_id, error)
|
|
|
|
return callback(error)
|
2014-04-10 07:44:46 -04:00
|
|
|
docLines = data.snapshot.split(/\r\n|\n|\r/)
|
2014-02-12 05:40:42 -05:00
|
|
|
callback(null, docLines, data.v)
|
|
|
|
|
|
|
|
_listenForOps: (model) ->
|
|
|
|
model.on "applyOp", (doc_key, opData) ->
|
|
|
|
[project_id, doc_id] = Keys.splitProjectIdAndDocId(doc_key)
|
2015-11-19 05:54:28 -05:00
|
|
|
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
|
|
|
|
|