diff --git a/lib/data/entity.js b/lib/data/entity.js index 13c0d3f3f..bfd1743db 100644 --- a/lib/data/entity.js +++ b/lib/data/entity.js @@ -395,25 +395,14 @@ const diffEntityData = (defData) => { return diffs; }; -// Copied from frontend though it may not all be necessary +// Copied from frontend // Offline branch class Branch { // firstUpdate is the first offline update (not create) to be processed from - // the branch. entityRoot is the first version of the entity. - constructor(firstUpdate, entityRoot) { + // the branch. + constructor(firstUpdate) { if (firstUpdate.trunkVersion != null) { - // The first version from the branch to be processed (not necessarily the - // first in the original branch order) - this.first = firstUpdate; - - // How many versions that have been processed are from the branch? - this.length = 1; - - // Was this.first processed in branch order, or was it processed before an - // earlier change in the branch? const { trunkVersion } = firstUpdate; - this.firstInOrder = firstUpdate.branchBaseVersion === trunkVersion; - /* this.lastContiguousWithTrunk is the version number of the last version from the branch that is contiguous with the trunk version. In other words, it is the version number of the last version where there has been no @@ -425,28 +414,30 @@ class Branch { ? firstUpdate.version : 0; } else { - // If the entity was both created and updated offline before being sent to - // the server, then we treat the creation as part of the same branch as - // the update(s). The creation doesn't have a branch ID, but we treat it - // as part of the branch anyway. - this.first = entityRoot; - // If the submission for the entity creation was received late and - // processed out of order, then firstUpdate.version === 1. In that case, - // we can't reliably determine which entity version corresponds to the - // entity creation, so we don't treat the creation as part of the branch. - this.length = firstUpdate.version === 1 ? 1 : 2; - this.firstInOrder = this.length === 2; + /* + If the entity was both created and updated offline before being sent to + the server, then there technically isn't a trunk version. On the flip + side, there also isn't a contiguity problem -- except in one special case. + If the submission for the entity creation is sent and processed, but the + submission for the update is not sent at the same time for some reason, + then it's possible for another update to be made between the two. Once the + original update is sent and processed, it will no longer be contiguous + with the entity creation. + + Another special case is if the submission for the entity creation was sent + late and processed out of order. In that case, firstUpdate.version === 1. + There's again no contiguity problem (just an order problem), so + lastContiguousWithTrunk should equal 1. + + The normal case is if firstUpdate.version === 2. + */ this.lastContiguousWithTrunk = firstUpdate.version === 2 ? 2 : 1; } this.id = firstUpdate.branchId; - // The last version from the branch to be processed - this.last = firstUpdate; } add(version) { - this.length += 1; - this.last = version; if (version.baseVersion === this.lastContiguousWithTrunk && version.version === version.baseVersion + 1) this.lastContiguousWithTrunk = version.version; @@ -475,27 +466,18 @@ const getWithConflictDetails = (defs, audits, relevantToConflict) => { const relevantBaseVersions = new Set(); - // build up branches const branches = new Map(); - for (const version of defs) { - const { branchId } = version; - if (branchId != null && version.branchBaseVersion != null) { - const existingBranch = branches.get(branchId); - if (existingBranch == null) { - const newBranch = new Branch(version, defs[0]); - branches.set(branchId, newBranch); - version.branch = newBranch; - // If the entity was created offline, then add the branch to the - // entity creation. - newBranch.first.branch = newBranch; - } else { - existingBranch.add(version); - version.branch = existingBranch; - } - } - } for (const def of defs) { + // build up branches + const { branchId } = def; + if (branchId != null) { + const existingBranch = branches.get(branchId); + if (existingBranch == null) + branches.set(branchId, new Branch(def)); + else + existingBranch.add(def); + } const v = mergeLeft(def.forApi(), { @@ -514,12 +496,10 @@ const getWithConflictDetails = (defs, audits, relevantToConflict) => { v.source = event.source; if (v.version > 1) { // v.root is false here - can use either - let notContiguousWithTrunk = false; - if (v.branchId != null) { - notContiguousWithTrunk = branches.get(v.branchId).lastContiguousWithTrunk < v.baseVersion; - } - - const conflict = v.version !== (v.baseVersion + 1) || notContiguousWithTrunk; + const baseNotContiguousWithTrunk = v.branchId != null && + branches.get(v.branchId).lastContiguousWithTrunk < v.baseVersion; + const conflict = v.version !== (v.baseVersion + 1) || + baseNotContiguousWithTrunk; v.baseDiff = getDiffProp(v.dataReceived, { ...defs[v.baseVersion - 1].data, label: defs[v.baseVersion - 1].label });