Fix issues with consuming delete updates beyond the end of the diff

This commit is contained in:
James Allen 2014-03-17 14:54:02 +00:00
parent 540aaf0672
commit 22a806a200
2 changed files with 30 additions and 6 deletions

View file

@ -94,19 +94,20 @@ module.exports = DiffGenerator =
consumedDiff.push DiffGenerator._slicePart part, 0, partOffset
if partOffset < length
remainingDiff.unshift DiffGenerator._slicePart part, partOffset
return {
consumedDiff: consumedDiff
remainingDiff: remainingDiff
}
break
else
position += length
consumedDiff.push part
throw new Error("Ran out of diff to consume. Offset is too small")
return {
consumedDiff: consumedDiff
remainingDiff: remainingDiff
}
_consumeDiffAffectedByDeleteOp: (remainingDiff, deleteOp, meta) ->
consumedDiff = []
remainingOp = deleteOp
while remainingOp
while remainingOp and remainingDiff.length > 0
{newPart, remainingDiff, remainingOp} = DiffGenerator._consumeDeletedPart remainingDiff, remainingOp, meta
consumedDiff.push newPart if newPart?
return {

View file

@ -311,4 +311,27 @@ describe "DiffGenerator", ->
)
).to.throw(@DiffGenerator.ConsistencyError)
describe "when the last update in the existing diff is a delete", ->
it "should insert the new update before the delete", ->
diff = @DiffGenerator.applyUpdateToDiff(
[ { u: "foo" }, { d: "bar", meta: @meta } ],
{ op: [{ p: 3, i: "baz" }], meta: @meta }
)
expect(diff).to.deep.equal([
{ u: "foo" }
{ i: "baz", meta: @meta }
{ d: "bar", meta: @meta }
])
describe "when the only update in the existing diff is a delete", ->
it "should insert the new update after the delete", ->
diff = @DiffGenerator.applyUpdateToDiff(
[ { d: "bar", meta: @meta } ],
{ op: [{ p: 0, i: "baz" }], meta: @meta }
)
expect(diff).to.deep.equal([
{ d: "bar", meta: @meta }
{ i: "baz", meta: @meta }
])