From dfe26262ec9197a722aaf1359efd8034e91c030f Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 16 Sep 2016 11:50:44 +0100 Subject: [PATCH] Return a No-op if diff returns nothing --- .../app/coffee/UpdateCompressor.coffee | 22 ++++++++++++++----- .../UpdateCompressorTests.coffee | 15 +++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/services/track-changes/app/coffee/UpdateCompressor.coffee b/services/track-changes/app/coffee/UpdateCompressor.coffee index f55d7462ea..3ae5adf8fb 100644 --- a/services/track-changes/app/coffee/UpdateCompressor.coffee +++ b/services/track-changes/app/coffee/UpdateCompressor.coffee @@ -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] diff --git a/services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee b/services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee index d2e449b646..b4858b79b0 100644 --- a/services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee +++ b/services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee @@ -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 }]