Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Dec 10, 2024
1 parent 4c1e2a7 commit b302e29
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/sdk/src/document/change/change_id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,23 @@ export class ChangeID {
public syncClocks(other: ChangeID): ChangeID {
const lamport =
other.lamport > this.lamport ? other.lamport + 1n : this.lamport + 1n;
const maxVersionVector = this.versionVector.max(other.versionVector);

// NOTE(chacha912): Handle changes from legacy SDK (v0.5.2 or below) which have empty version vectors.
// Add lamport to version vector to include this change's information.
let otherVV = other.versionVector;
if (otherVV.size() === 0) {
otherVV = otherVV.deepcopy();
otherVV.set(other.actor, other.lamport);
}

const maxVersionVector = this.versionVector.max(otherVV);
const newID = new ChangeID(
this.clientSeq,
lamport,
this.actor,
maxVersionVector,
);
newID.versionVector.set(this.actor, lamport);
if (other.versionVector.size() === 0) {
newID.versionVector.set(other.actor, other.lamport);
}
return newID;
}

Expand All @@ -107,9 +112,13 @@ export class ChangeID {
public setClocks(otherLamport: bigint, vector: VersionVector): ChangeID {
const lamport =
otherLamport > this.lamport ? otherLamport + 1n : this.lamport + 1n;

// NOTE(chacha912): Snapshot's version vector from server contains initialActorID.
// Remove it as it's not an actual participating client.
vector.unset(InitialActorID);

const maxVersionVector = this.versionVector.max(vector);
maxVersionVector.set(this.actor, lamport);
maxVersionVector.unset(InitialActorID);

return ChangeID.of(this.clientSeq, lamport, this.actor, maxVersionVector);
}
Expand Down

0 comments on commit b302e29

Please sign in to comment.