mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-30 05:55:30 -05:00
Merge pull request #14 from sharelatex/ja-calculate-doclength
Calculate and send doc length before each update
This commit is contained in:
commit
e3efbaffac
2 changed files with 34 additions and 10 deletions
|
@ -79,7 +79,7 @@ module.exports = UpdateManager =
|
||||||
profile.log("sharejs.applyUpdate")
|
profile.log("sharejs.applyUpdate")
|
||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
RangesManager.applyUpdate project_id, doc_id, ranges, appliedOps, updatedDocLines, (error, new_ranges) ->
|
RangesManager.applyUpdate project_id, doc_id, ranges, appliedOps, updatedDocLines, (error, new_ranges) ->
|
||||||
UpdateManager._addProjectHistoryMetadataToOps(appliedOps, pathname, updatedDocLines)
|
UpdateManager._addProjectHistoryMetadataToOps(appliedOps, pathname, lines)
|
||||||
profile.log("RangesManager.applyUpdate")
|
profile.log("RangesManager.applyUpdate")
|
||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
RedisManager.updateDocument project_id, doc_id, updatedDocLines, version, appliedOps, new_ranges, (error, doc_ops_length, project_ops_length) ->
|
RedisManager.updateDocument project_id, doc_id, updatedDocLines, version, appliedOps, new_ranges, (error, doc_ops_length, project_ops_length) ->
|
||||||
|
@ -129,12 +129,26 @@ module.exports = UpdateManager =
|
||||||
op.i = op.i.replace(/[\uD800-\uDFFF]/g, "\uFFFD")
|
op.i = op.i.replace(/[\uD800-\uDFFF]/g, "\uFFFD")
|
||||||
return update
|
return update
|
||||||
|
|
||||||
_addProjectHistoryMetadataToOps: (ops, pathname, lines) ->
|
_addProjectHistoryMetadataToOps: (updates, pathname, lines) ->
|
||||||
doc_length = _.reduce lines,
|
doc_length = _.reduce lines,
|
||||||
(chars, line) -> chars + line.length,
|
(chars, line) -> chars + line.length,
|
||||||
0
|
0
|
||||||
doc_length += lines.length - 1 # count newline characters
|
doc_length += lines.length - 1 # count newline characters
|
||||||
ops.forEach (op) ->
|
updates.forEach (update) ->
|
||||||
op.meta ||= {}
|
update.meta ||= {}
|
||||||
op.meta.pathname = pathname
|
update.meta.pathname = pathname
|
||||||
op.meta.doc_length = doc_length
|
update.meta.doc_length = doc_length
|
||||||
|
# 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.
|
||||||
|
for op in update.op
|
||||||
|
if op.i?
|
||||||
|
doc_length += op.i.length
|
||||||
|
if op.d?
|
||||||
|
doc_length -= op.d.length
|
||||||
|
|
|
@ -245,20 +245,30 @@ describe "UpdateManager", ->
|
||||||
'test'
|
'test'
|
||||||
'data'
|
'data'
|
||||||
]
|
]
|
||||||
appliedOps = [ {v: 42, op: "mock-op-42"}, { v: 45, op: "mock-op-45" }]
|
appliedOps = [
|
||||||
|
{ v: 42, op: [{i: "foo", p: 4}, { i: "bar", p: 6 }] },
|
||||||
|
{ v: 45, op: [{d: "qux", p: 4}, { i: "bazbaz", p: 14 }] },
|
||||||
|
{ v: 49, op: [{i: "penguin", p: 18}] }
|
||||||
|
]
|
||||||
@UpdateManager._addProjectHistoryMetadataToOps(appliedOps, @pathname, lines)
|
@UpdateManager._addProjectHistoryMetadataToOps(appliedOps, @pathname, lines)
|
||||||
appliedOps.should.deep.equal [{
|
appliedOps.should.deep.equal [{
|
||||||
v: 42
|
v: 42
|
||||||
op: "mock-op-42"
|
op: [{i: "foo", p: 4}, { i: "bar", p: 6 }]
|
||||||
meta:
|
meta:
|
||||||
pathname: @pathname
|
pathname: @pathname
|
||||||
doc_length: 14
|
doc_length: 14
|
||||||
}, {
|
}, {
|
||||||
v: 45
|
v: 45
|
||||||
op: "mock-op-45"
|
op: [{d: "qux", p: 4}, { i: "bazbaz", p: 14 }]
|
||||||
meta:
|
meta:
|
||||||
pathname: @pathname
|
pathname: @pathname
|
||||||
doc_length: 14
|
doc_length: 20 # 14 + 'foo' + 'bar'
|
||||||
|
}, {
|
||||||
|
v: 49
|
||||||
|
op: [{i: "penguin", p: 18}]
|
||||||
|
meta:
|
||||||
|
pathname: @pathname
|
||||||
|
doc_length: 23 # 14 - 'qux' + 'bazbaz'
|
||||||
}]
|
}]
|
||||||
|
|
||||||
describe "lockUpdatesAndDo", ->
|
describe "lockUpdatesAndDo", ->
|
||||||
|
|
Loading…
Reference in a new issue