Merge pull request #20869 from overleaf/em-ranges-tracker-duplicate-ids

Fix handling of duplicate change ids in RangesTracker

GitOrigin-RevId: 1a75d1dc7e7931c459d2d004973f34d931100d33
This commit is contained in:
Eric Mc Sween 2024-10-08 07:17:41 -04:00 committed by Copybot
parent 139fde750a
commit 16d3d59bc1
5 changed files with 39 additions and 26 deletions

View file

@ -139,22 +139,9 @@ class RangesTracker {
return change return change
} }
getChanges(changeIds) { getChanges(ids) {
const changesResponse = [] const idSet = new Set(ids)
const idsMap = {} return this.changes.filter(change => idSet.has(change.id))
for (const changeId of changeIds) {
idsMap[changeId] = true
}
for (const change of this.changes) {
if (idsMap[change.id]) {
delete idsMap[change.id]
changesResponse.push(change)
}
}
return changesResponse
} }
removeChangeId(changeId) { removeChangeId(changeId) {
@ -165,20 +152,15 @@ class RangesTracker {
this._removeChange(change) this._removeChange(change)
} }
removeChangeIds(changeToRemoveIds) { removeChangeIds(ids) {
if (changeToRemoveIds == null || changeToRemoveIds.length === 0) { if (ids == null || ids.length === 0) {
return return
} }
const removeChangeId = {}
for (const changeId of changeToRemoveIds) {
removeChangeId[changeId] = true
}
const idSet = new Set(ids)
const remainingChanges = [] const remainingChanges = []
for (const change of this.changes) { for (const change of this.changes) {
if (removeChangeId[change.id]) { if (idSet.has(change.id)) {
delete removeChangeId[change.id]
this._markAsDirty(change, 'change', 'removed') this._markAsDirty(change, 'change', 'removed')
} else { } else {
remainingChanges.push(change) remainingChanges.push(change)

View file

@ -19,6 +19,7 @@
"types:check": "tsc --noEmit" "types:check": "tsc --noEmit"
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.3.6",
"mocha": "^10.2.0", "mocha": "^10.2.0",
"typescript": "^5.0.4" "typescript": "^5.0.4"
} }

View file

@ -1 +0,0 @@
// There are no tests yet

View file

@ -0,0 +1,29 @@
const { expect } = require('chai')
const RangesTracker = require('../..')
describe('RangesTracker', function () {
describe('with duplicate change ids', function () {
beforeEach(function () {
this.changes = [
{ id: 'id1', op: { p: 1, i: 'hello' } },
{ id: 'id2', op: { p: 10, i: 'world' } },
{ id: 'id3', op: { p: 20, i: '!!!' } },
{ id: 'id1', op: { p: 30, d: 'duplicate' } },
]
this.rangesTracker = new RangesTracker(this.changes, this.comments)
})
it('getChanges() returns all changes with the given ids', function () {
expect(this.rangesTracker.getChanges(['id1', 'id2'])).to.deep.equal([
this.changes[0],
this.changes[1],
this.changes[3],
])
})
it('removeChangeIds() removes all changes with the given ids', function () {
this.rangesTracker.removeChangeIds(['id1', 'id2'])
expect(this.rangesTracker.changes).to.deep.equal([this.changes[2]])
})
})
})

2
package-lock.json generated
View file

@ -471,6 +471,7 @@
"libraries/ranges-tracker": { "libraries/ranges-tracker": {
"name": "@overleaf/ranges-tracker", "name": "@overleaf/ranges-tracker",
"devDependencies": { "devDependencies": {
"chai": "^4.3.6",
"mocha": "^10.2.0", "mocha": "^10.2.0",
"typescript": "^5.0.4" "typescript": "^5.0.4"
} }
@ -52023,6 +52024,7 @@
"@overleaf/ranges-tracker": { "@overleaf/ranges-tracker": {
"version": "file:libraries/ranges-tracker", "version": "file:libraries/ranges-tracker",
"requires": { "requires": {
"chai": "^4.3.6",
"mocha": "^10.2.0", "mocha": "^10.2.0",
"typescript": "^5.0.4" "typescript": "^5.0.4"
} }