Return a No-op if diff returns nothing

This commit is contained in:
James Allen 2016-09-16 11:50:44 +01:00
parent 80375ae2dd
commit dfe26262ec
2 changed files with 23 additions and 14 deletions

View file

@ -162,16 +162,28 @@ module.exports = UpdateCompressor =
else if firstOp.d? and secondOp.i? and firstOp.p == secondOp.p
offset = firstOp.p
diff_ops = @diffAsShareJsOps(firstOp.d, secondOp.i)
return diff_ops.map (op) ->
op.p += offset
return {
if diff_ops.length == 0
return [{ # Noop
meta:
start_ts: firstUpdate.meta.start_ts
end_ts: secondUpdate.meta.end_ts
user_id: firstUpdate.meta.user_id
op: op
op:
p: firstOp.p
i: ""
v: secondUpdate.v
}
}]
else
return diff_ops.map (op) ->
op.p += offset
return {
meta:
start_ts: firstUpdate.meta.start_ts
end_ts: secondUpdate.meta.end_ts
user_id: firstUpdate.meta.user_id
op: op
v: secondUpdate.v
}
else
return [firstUpdate, secondUpdate]

View file

@ -7,7 +7,8 @@ SandboxedModule = require('sandboxed-module')
describe "UpdateCompressor", ->
beforeEach ->
@UpdateCompressor = SandboxedModule.require modulePath
@UpdateCompressor = SandboxedModule.require modulePath, requires:
"../lib/diff_match_patch": require("../../../../app/lib/diff_match_patch")
@user_id = "user-id-1"
@other_user_id = "user-id-2"
@bigstring = ("a" for [0 .. 2*1024*1024]).join("")
@ -366,23 +367,19 @@ describe "UpdateCompressor", ->
meta: start_ts: @ts1, end_ts: @ts2, user_id: @user_id
v: 43
}]
it "should do a diff of the content", ->
it "should return a no-op if the delete and insert are the same", ->
expect(@UpdateCompressor.compressUpdates [{
op: { p: 3, d: "one two three four five six seven eight" }
meta: ts: @ts1, user_id: @user_id
v: 42
}, {
op: { p: 3, i: "one 2 three four five six seven eight" }
op: { p: 3, i: "one two three four five six seven eight" }
meta: ts: @ts2, user_id: @user_id
v: 43
}])
.to.deep.equal [{
op: { p: 7, d: "two" }
meta: start_ts: @ts1, end_ts: @ts2, user_id: @user_id
v: 43
}, {
op: { p: 7, i: "2" }
op: { p: 3, i: "" }
meta: start_ts: @ts1, end_ts: @ts2, user_id: @user_id
v: 43
}]