Skip to content

Commit

Permalink
Clean up codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Nov 5, 2024
1 parent 35262cd commit c98593f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/sdk/src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1149,11 +1149,12 @@ export class Document<T, P extends Indexable = Indexable> {
}

/**
* `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) {
Expand All @@ -1175,6 +1176,7 @@ export class Document<T, P extends Indexable = Indexable> {
public applyChangePack(pack: ChangePack<P>): void {
const hasSnapshot = pack.hasSnapshot();

// 01. Apply snapshot or changes to the root object.
if (hasSnapshot) {
this.applySnapshot(
pack.getCheckpoint().getServerSeq(),
Expand All @@ -1184,20 +1186,18 @@ export class Document<T, P extends Indexable = Indexable> {
);
} 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()!);
}
Expand Down Expand Up @@ -1424,7 +1424,7 @@ export class Document<T, P extends Indexable = Indexable> {
// 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.
Expand Down Expand Up @@ -1699,6 +1699,7 @@ export class Document<T, P extends Indexable = Indexable> {
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),
Expand Down

0 comments on commit c98593f

Please sign in to comment.