diff --git a/packages/sdk/src/document/document.ts b/packages/sdk/src/document/document.ts index 8bc03e95a..108806b51 100644 --- a/packages/sdk/src/document/document.ts +++ b/packages/sdk/src/document/document.ts @@ -1149,11 +1149,12 @@ export class Document { } /** - * `removeAppliedLocalChanges` removes local changes applied to the server. + * `removePushedLocalChanges` removes local changes that have been applied to + * the server from the local changes. * * @param clientSeq - client sequence number to remove local changes before it */ - private removeAppliedLocalChanges(clientSeq: number) { + private removePushedLocalChanges(clientSeq: number) { while (this.localChanges.length) { const change = this.localChanges[0]; if (change.getID().getClientSeq() > clientSeq) { @@ -1175,6 +1176,7 @@ export class Document { public applyChangePack(pack: ChangePack

): void { const hasSnapshot = pack.hasSnapshot(); + // 01. Apply snapshot or changes to the root object. if (hasSnapshot) { this.applySnapshot( pack.getCheckpoint().getServerSeq(), @@ -1184,20 +1186,18 @@ export class Document { ); } else if (pack.hasChanges()) { this.applyChanges(pack.getChanges(), OpSource.Remote); + this.removePushedLocalChanges(pack.getCheckpoint().getClientSeq()); } - // 02. Remove local changes applied to server. - this.removeAppliedLocalChanges(pack.getCheckpoint().getClientSeq()); - - // 03. Update the checkpoint. + // 02. Update the checkpoint. this.checkpoint = this.checkpoint.forward(pack.getCheckpoint()); - // 04. Do Garbage collection. + // 03. Do Garbage collection. if (!hasSnapshot) { this.garbageCollect(pack.getVersionVector()!); } - // 05. Filter detached client's lamport from version vector + // 04. Filter detached client's lamport from version vector if (!hasSnapshot) { this.filterVersionVector(pack.getVersionVector()!); } @@ -1424,7 +1424,7 @@ export class Document { // drop clone because it is contaminated. this.clone = undefined; - this.removeAppliedLocalChanges(clientSeq); + this.removePushedLocalChanges(clientSeq); // NOTE(hackerwins): If the document has local changes, we need to apply // them after applying the snapshot, as local changes are not included in the snapshot data. @@ -1699,6 +1699,7 @@ export class Document { if (event.type === DocEventType.Snapshot) { const { snapshot, serverSeq, snapshotVector } = event.value; if (!snapshot) return; + // TODO(hackerwins): We need to check the clientSeq of the snapshot. this.applySnapshot( BigInt(serverSeq), converter.hexToVersionVector(snapshotVector),