2020-05-06 06:09:15 -04:00
|
|
|
|
/* eslint-disable
|
|
|
|
|
no-unused-vars,
|
|
|
|
|
*/
|
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04:00
|
|
|
|
/*
|
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
|
* DS201: Simplify complex destructure assignments
|
|
|
|
|
* DS205: Consider reworking code to avoid use of IIFEs
|
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
|
*/
|
2020-05-06 06:09:33 -04:00
|
|
|
|
let UpdateManager
|
|
|
|
|
const LockManager = require('./LockManager')
|
|
|
|
|
const RedisManager = require('./RedisManager')
|
|
|
|
|
const RealTimeRedisManager = require('./RealTimeRedisManager')
|
|
|
|
|
const ShareJsUpdateManager = require('./ShareJsUpdateManager')
|
|
|
|
|
const HistoryManager = require('./HistoryManager')
|
2021-07-12 12:47:15 -04:00
|
|
|
|
const Settings = require('@overleaf/settings')
|
2020-05-06 06:09:33 -04:00
|
|
|
|
const _ = require('lodash')
|
|
|
|
|
const async = require('async')
|
2021-10-06 05:10:28 -04:00
|
|
|
|
const logger = require('@overleaf/logger')
|
2020-05-06 06:09:33 -04:00
|
|
|
|
const Metrics = require('./Metrics')
|
|
|
|
|
const Errors = require('./Errors')
|
|
|
|
|
const DocumentManager = require('./DocumentManager')
|
|
|
|
|
const RangesManager = require('./RangesManager')
|
|
|
|
|
const SnapshotManager = require('./SnapshotManager')
|
|
|
|
|
const Profiler = require('./Profiler')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
|
module.exports = UpdateManager = {
|
2023-03-21 08:06:13 -04:00
|
|
|
|
processOutstandingUpdates(projectId, docId, callback) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
|
|
|
|
const timer = new Metrics.Timer('updateManager.processOutstandingUpdates')
|
2023-06-06 06:19:55 -04:00
|
|
|
|
UpdateManager.fetchAndApplyUpdates(projectId, docId, function (error) {
|
|
|
|
|
timer.done()
|
|
|
|
|
callback(error)
|
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
|
processOutstandingUpdatesWithLock(projectId, docId, callback) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
|
|
|
|
const profile = new Profiler('processOutstandingUpdatesWithLock', {
|
2023-03-21 08:06:13 -04:00
|
|
|
|
project_id: projectId,
|
|
|
|
|
doc_id: docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
|
})
|
2023-06-06 06:19:55 -04:00
|
|
|
|
LockManager.tryLock(docId, (error, gotLock, lockValue) => {
|
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
return callback(error)
|
|
|
|
|
}
|
|
|
|
|
if (!gotLock) {
|
|
|
|
|
return callback()
|
|
|
|
|
}
|
|
|
|
|
profile.log('tryLock')
|
2023-06-06 06:19:55 -04:00
|
|
|
|
UpdateManager.processOutstandingUpdates(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
|
function (error) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
return UpdateManager._handleErrorInsideLock(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
|
lockValue,
|
|
|
|
|
error,
|
|
|
|
|
callback
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
profile.log('processOutstandingUpdates')
|
2023-06-06 06:19:55 -04:00
|
|
|
|
LockManager.releaseLock(docId, lockValue, error => {
|
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
return callback(error)
|
|
|
|
|
}
|
|
|
|
|
profile.log('releaseLock').end()
|
2023-06-06 06:19:55 -04:00
|
|
|
|
UpdateManager.continueProcessingUpdatesWithLock(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
|
callback
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
|
continueProcessingUpdatesWithLock(projectId, docId, callback) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
2023-06-06 06:19:55 -04:00
|
|
|
|
RealTimeRedisManager.getUpdatesLength(docId, (error, length) => {
|
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
return callback(error)
|
|
|
|
|
}
|
|
|
|
|
if (length > 0) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
UpdateManager.processOutstandingUpdatesWithLock(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
|
callback
|
|
|
|
|
)
|
|
|
|
|
} else {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
callback()
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
|
fetchAndApplyUpdates(projectId, docId, callback) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
|
const profile = new Profiler('fetchAndApplyUpdates', {
|
|
|
|
|
project_id: projectId,
|
|
|
|
|
doc_id: docId,
|
|
|
|
|
})
|
2023-06-06 06:19:55 -04:00
|
|
|
|
RealTimeRedisManager.getPendingUpdatesForDoc(docId, (error, updates) => {
|
|
|
|
|
if (error) {
|
|
|
|
|
return callback(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
2023-06-06 06:19:55 -04:00
|
|
|
|
logger.debug(
|
|
|
|
|
{ projectId, docId, count: updates.length },
|
|
|
|
|
'processing updates'
|
|
|
|
|
)
|
|
|
|
|
if (updates.length === 0) {
|
|
|
|
|
return callback()
|
|
|
|
|
}
|
|
|
|
|
profile.log('getPendingUpdatesForDoc')
|
|
|
|
|
const doUpdate = (update, cb) =>
|
|
|
|
|
UpdateManager.applyUpdate(projectId, docId, update, function (err) {
|
|
|
|
|
profile.log('applyUpdate')
|
|
|
|
|
cb(err)
|
|
|
|
|
})
|
|
|
|
|
const finalCallback = function (err) {
|
|
|
|
|
profile.log('async done').end()
|
|
|
|
|
callback(err)
|
|
|
|
|
}
|
|
|
|
|
async.eachSeries(updates, doUpdate, finalCallback)
|
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
|
applyUpdate(projectId, docId, update, _callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
if (_callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
|
_callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
|
|
|
|
const callback = function (error) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
RealTimeRedisManager.sendData({
|
2023-03-21 08:06:13 -04:00
|
|
|
|
project_id: projectId,
|
|
|
|
|
doc_id: docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
|
error: error.message || error,
|
2020-05-06 06:09:33 -04:00
|
|
|
|
})
|
|
|
|
|
profile.log('sendData')
|
|
|
|
|
}
|
|
|
|
|
profile.end()
|
2023-06-06 06:19:55 -04:00
|
|
|
|
_callback(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
2017-05-19 11:00:16 -04:00
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
|
const profile = new Profiler('applyUpdate', {
|
|
|
|
|
project_id: projectId,
|
|
|
|
|
doc_id: docId,
|
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
|
UpdateManager._sanitizeUpdate(update)
|
2021-10-06 05:09:44 -04:00
|
|
|
|
profile.log('sanitizeUpdate', { sync: true })
|
2023-06-06 06:19:55 -04:00
|
|
|
|
DocumentManager.getDoc(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
|
function (error, lines, version, ranges, pathname, projectHistoryId) {
|
|
|
|
|
profile.log('getDoc')
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
|
return callback(error)
|
|
|
|
|
}
|
|
|
|
|
if (lines == null || version == null) {
|
|
|
|
|
return callback(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
new Errors.NotFoundError(`document not found: ${docId}`)
|
2021-07-13 07:04:42 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
const previousVersion = version
|
2021-10-06 05:09:44 -04:00
|
|
|
|
const incomingUpdateVersion = update.v
|
2023-06-06 06:19:55 -04:00
|
|
|
|
ShareJsUpdateManager.applyUpdate(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
|
update,
|
|
|
|
|
lines,
|
|
|
|
|
version,
|
|
|
|
|
function (error, updatedDocLines, version, appliedOps) {
|
2021-10-06 05:09:44 -04:00
|
|
|
|
profile.log('sharejs.applyUpdate', {
|
|
|
|
|
// only synchronous when the update applies directly to the
|
|
|
|
|
// doc version, otherwise getPreviousDocOps is called.
|
|
|
|
|
sync: incomingUpdateVersion === previousVersion,
|
|
|
|
|
})
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
|
return callback(error)
|
|
|
|
|
}
|
2023-06-06 06:19:55 -04:00
|
|
|
|
RangesManager.applyUpdate(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
|
ranges,
|
|
|
|
|
appliedOps,
|
|
|
|
|
updatedDocLines,
|
2023-03-21 08:06:13 -04:00
|
|
|
|
function (error, newRanges, rangesWereCollapsed) {
|
2021-07-13 07:04:42 -04:00
|
|
|
|
UpdateManager._addProjectHistoryMetadataToOps(
|
|
|
|
|
appliedOps,
|
|
|
|
|
pathname,
|
|
|
|
|
projectHistoryId,
|
|
|
|
|
lines
|
|
|
|
|
)
|
2021-10-06 05:09:44 -04:00
|
|
|
|
profile.log('RangesManager.applyUpdate', { sync: true })
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
|
return callback(error)
|
|
|
|
|
}
|
2023-06-06 06:19:55 -04:00
|
|
|
|
RedisManager.updateDocument(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
|
updatedDocLines,
|
|
|
|
|
version,
|
|
|
|
|
appliedOps,
|
2023-03-21 08:06:13 -04:00
|
|
|
|
newRanges,
|
2021-07-13 07:04:42 -04:00
|
|
|
|
update.meta,
|
2023-06-06 06:19:55 -04:00
|
|
|
|
function (error, projectOpsLength) {
|
2021-07-13 07:04:42 -04:00
|
|
|
|
profile.log('RedisManager.updateDocument')
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
|
return callback(error)
|
|
|
|
|
}
|
2023-06-06 06:19:55 -04:00
|
|
|
|
HistoryManager.recordAndFlushHistoryOps(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
2021-07-13 07:04:42 -04:00
|
|
|
|
appliedOps,
|
2023-06-06 06:19:55 -04:00
|
|
|
|
projectOpsLength
|
2021-07-13 07:04:42 -04:00
|
|
|
|
)
|
2023-06-06 06:19:55 -04:00
|
|
|
|
profile.log('recordAndFlushHistoryOps')
|
|
|
|
|
if (rangesWereCollapsed) {
|
|
|
|
|
Metrics.inc('doc-snapshot')
|
|
|
|
|
logger.debug(
|
|
|
|
|
{
|
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
|
|
|
|
previousVersion,
|
|
|
|
|
lines,
|
|
|
|
|
ranges,
|
|
|
|
|
update,
|
|
|
|
|
},
|
|
|
|
|
'update collapsed some ranges, snapshotting previous content'
|
|
|
|
|
)
|
|
|
|
|
// Do this last, since it's a mongo call, and so potentially longest running
|
|
|
|
|
// If it overruns the lock, it's ok, since all of our redis work is done
|
|
|
|
|
SnapshotManager.recordSnapshot(
|
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
|
|
|
|
previousVersion,
|
|
|
|
|
pathname,
|
|
|
|
|
lines,
|
|
|
|
|
ranges,
|
|
|
|
|
function (error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
logger.error(
|
|
|
|
|
{
|
|
|
|
|
err: error,
|
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
|
|
|
|
version,
|
|
|
|
|
lines,
|
|
|
|
|
ranges,
|
|
|
|
|
},
|
|
|
|
|
'error recording snapshot'
|
|
|
|
|
)
|
|
|
|
|
callback(error)
|
|
|
|
|
} else {
|
|
|
|
|
callback()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
callback()
|
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
|
lockUpdatesAndDo(method, projectId, docId, ...rest) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
const adjustedLength = Math.max(rest.length, 1)
|
|
|
|
|
const args = rest.slice(0, adjustedLength - 1)
|
|
|
|
|
const callback = rest[adjustedLength - 1]
|
2023-03-21 08:06:13 -04:00
|
|
|
|
const profile = new Profiler('lockUpdatesAndDo', {
|
|
|
|
|
project_id: projectId,
|
|
|
|
|
doc_id: docId,
|
|
|
|
|
})
|
|
|
|
|
return LockManager.getLock(docId, function (error, lockValue) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
profile.log('getLock')
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
return callback(error)
|
|
|
|
|
}
|
2023-06-06 06:19:55 -04:00
|
|
|
|
UpdateManager.processOutstandingUpdates(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
|
function (error) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
|
return UpdateManager._handleErrorInsideLock(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
|
lockValue,
|
|
|
|
|
error,
|
|
|
|
|
callback
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
profile.log('processOutstandingUpdates')
|
2023-06-06 06:19:55 -04:00
|
|
|
|
method(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
|
...Array.from(args),
|
2023-03-21 08:06:13 -04:00
|
|
|
|
function (error, ...responseArgs) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
|
return UpdateManager._handleErrorInsideLock(
|
2023-03-21 08:06:13 -04:00
|
|
|
|
docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
|
lockValue,
|
|
|
|
|
error,
|
|
|
|
|
callback
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
profile.log('method')
|
2023-06-06 06:19:55 -04:00
|
|
|
|
LockManager.releaseLock(docId, lockValue, function (error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
return callback(error)
|
2021-07-13 07:04:42 -04:00
|
|
|
|
}
|
2023-06-06 06:19:55 -04:00
|
|
|
|
profile.log('releaseLock').end()
|
|
|
|
|
callback(null, ...Array.from(responseArgs))
|
|
|
|
|
// We held the lock for a while so updates might have queued up
|
|
|
|
|
UpdateManager.continueProcessingUpdatesWithLock(
|
|
|
|
|
projectId,
|
|
|
|
|
docId,
|
|
|
|
|
err => {
|
|
|
|
|
if (err) {
|
|
|
|
|
// The processing may fail for invalid user updates.
|
|
|
|
|
// This can be very noisy, put them on level DEBUG
|
|
|
|
|
// and record a metric.
|
|
|
|
|
Metrics.inc('background-processing-updates-error')
|
|
|
|
|
logger.debug(
|
|
|
|
|
{ err, projectId, docId },
|
|
|
|
|
'error processing updates in background'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
|
_handleErrorInsideLock(docId, lockValue, originalError, callback) {
|
2023-06-06 06:19:55 -04:00
|
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
2023-06-06 06:19:55 -04:00
|
|
|
|
LockManager.releaseLock(docId, lockValue, lockError =>
|
2023-03-21 08:06:13 -04:00
|
|
|
|
callback(originalError)
|
2020-05-06 06:09:33 -04:00
|
|
|
|
)
|
|
|
|
|
},
|
2017-09-29 07:57:27 -04:00
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
|
_sanitizeUpdate(update) {
|
|
|
|
|
// In Javascript, characters are 16-bits wide. It does not understand surrogates as characters.
|
|
|
|
|
//
|
|
|
|
|
// From Wikipedia (http://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane):
|
|
|
|
|
// "The High Surrogates (U+D800–U+DBFF) and Low Surrogate (U+DC00–U+DFFF) codes are reserved
|
|
|
|
|
// for encoding non-BMP characters in UTF-16 by using a pair of 16-bit codes: one High Surrogate
|
|
|
|
|
// and one Low Surrogate. A single surrogate code point will never be assigned a character.""
|
|
|
|
|
//
|
|
|
|
|
// The main offender seems to be \uD835 as a stand alone character, which would be the first
|
|
|
|
|
// 16-bit character of a blackboard bold character (http://www.fileformat.info/info/unicode/char/1d400/index.htm).
|
|
|
|
|
// Something must be going on client side that is screwing up the encoding and splitting the
|
|
|
|
|
// two 16-bit characters so that \uD835 is standalone.
|
|
|
|
|
for (const op of Array.from(update.op || [])) {
|
|
|
|
|
if (op.i != null) {
|
|
|
|
|
// Replace high and low surrogate characters with 'replacement character' (\uFFFD)
|
|
|
|
|
op.i = op.i.replace(/[\uD800-\uDFFF]/g, '\uFFFD')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return update
|
|
|
|
|
},
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
|
_addProjectHistoryMetadataToOps(updates, pathname, projectHistoryId, lines) {
|
2023-03-21 08:06:13 -04:00
|
|
|
|
let docLength = _.reduce(lines, (chars, line) => chars + line.length, 0)
|
|
|
|
|
docLength += lines.length - 1 // count newline characters
|
2020-05-06 06:09:33 -04:00
|
|
|
|
return updates.forEach(function (update) {
|
|
|
|
|
update.projectHistoryId = projectHistoryId
|
|
|
|
|
if (!update.meta) {
|
|
|
|
|
update.meta = {}
|
|
|
|
|
}
|
|
|
|
|
update.meta.pathname = pathname
|
2023-03-21 08:06:13 -04:00
|
|
|
|
update.meta.doc_length = docLength
|
2020-05-06 06:09:33 -04:00
|
|
|
|
// Each update may contain multiple ops, i.e.
|
|
|
|
|
// [{
|
|
|
|
|
// ops: [{i: "foo", p: 4}, {d: "bar", p:8}]
|
|
|
|
|
// }, {
|
|
|
|
|
// ops: [{d: "baz", p: 40}, {i: "qux", p:8}]
|
|
|
|
|
// }]
|
|
|
|
|
// We want to include the doc_length at the start of each update,
|
|
|
|
|
// before it's ops are applied. However, we need to track any
|
|
|
|
|
// changes to it for the next update.
|
|
|
|
|
return (() => {
|
|
|
|
|
const result = []
|
|
|
|
|
for (const op of Array.from(update.op)) {
|
|
|
|
|
if (op.i != null) {
|
2023-03-21 08:06:13 -04:00
|
|
|
|
docLength += op.i.length
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|
|
|
|
|
if (op.d != null) {
|
2023-03-21 08:06:13 -04:00
|
|
|
|
result.push((docLength -= op.d.length))
|
2020-05-06 06:09:33 -04:00
|
|
|
|
} else {
|
|
|
|
|
result.push(undefined)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
})()
|
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
|
}
|