Skip to content

Commit c98593f

Browse files
committed
Clean up codes
1 parent 35262cd commit c98593f

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

packages/sdk/src/document/document.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,12 @@ export class Document<T, P extends Indexable = Indexable> {
11491149
}
11501150

11511151
/**
1152-
* `removeAppliedLocalChanges` removes local changes applied to the server.
1152+
* `removePushedLocalChanges` removes local changes that have been applied to
1153+
* the server from the local changes.
11531154
*
11541155
* @param clientSeq - client sequence number to remove local changes before it
11551156
*/
1156-
private removeAppliedLocalChanges(clientSeq: number) {
1157+
private removePushedLocalChanges(clientSeq: number) {
11571158
while (this.localChanges.length) {
11581159
const change = this.localChanges[0];
11591160
if (change.getID().getClientSeq() > clientSeq) {
@@ -1175,6 +1176,7 @@ export class Document<T, P extends Indexable = Indexable> {
11751176
public applyChangePack(pack: ChangePack<P>): void {
11761177
const hasSnapshot = pack.hasSnapshot();
11771178

1179+
// 01. Apply snapshot or changes to the root object.
11781180
if (hasSnapshot) {
11791181
this.applySnapshot(
11801182
pack.getCheckpoint().getServerSeq(),
@@ -1184,20 +1186,18 @@ export class Document<T, P extends Indexable = Indexable> {
11841186
);
11851187
} else if (pack.hasChanges()) {
11861188
this.applyChanges(pack.getChanges(), OpSource.Remote);
1189+
this.removePushedLocalChanges(pack.getCheckpoint().getClientSeq());
11871190
}
11881191

1189-
// 02. Remove local changes applied to server.
1190-
this.removeAppliedLocalChanges(pack.getCheckpoint().getClientSeq());
1191-
1192-
// 03. Update the checkpoint.
1192+
// 02. Update the checkpoint.
11931193
this.checkpoint = this.checkpoint.forward(pack.getCheckpoint());
11941194

1195-
// 04. Do Garbage collection.
1195+
// 03. Do Garbage collection.
11961196
if (!hasSnapshot) {
11971197
this.garbageCollect(pack.getVersionVector()!);
11981198
}
11991199

1200-
// 05. Filter detached client's lamport from version vector
1200+
// 04. Filter detached client's lamport from version vector
12011201
if (!hasSnapshot) {
12021202
this.filterVersionVector(pack.getVersionVector()!);
12031203
}
@@ -1424,7 +1424,7 @@ export class Document<T, P extends Indexable = Indexable> {
14241424
// drop clone because it is contaminated.
14251425
this.clone = undefined;
14261426

1427-
this.removeAppliedLocalChanges(clientSeq);
1427+
this.removePushedLocalChanges(clientSeq);
14281428

14291429
// NOTE(hackerwins): If the document has local changes, we need to apply
14301430
// them after applying the snapshot, as local changes are not included in the snapshot data.
@@ -1699,6 +1699,7 @@ export class Document<T, P extends Indexable = Indexable> {
16991699
if (event.type === DocEventType.Snapshot) {
17001700
const { snapshot, serverSeq, snapshotVector } = event.value;
17011701
if (!snapshot) return;
1702+
// TODO(hackerwins): We need to check the clientSeq of the snapshot.
17021703
this.applySnapshot(
17031704
BigInt(serverSeq),
17041705
converter.hexToVersionVector(snapshotVector),

0 commit comments

Comments
 (0)