Skip to content

Commit

Permalink
Add ServerSeq into ChangeInfo (#833)
Browse files Browse the repository at this point in the history
Co-authored-by: Yourim Cha <[email protected]>
  • Loading branch information
hackerwins and chacha912 authored May 30, 2024
1 parent bab2d82 commit 0a421ee
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
20 changes: 14 additions & 6 deletions src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,16 @@ function toChangePack(pack: ChangePack<Indexable>): PbChangePack {
* `fromChangeID` converts the given Protobuf format to model format.
*/
function fromChangeID(pbChangeID: PbChangeID): ChangeID {
let serverSeq: Long | undefined;
if (pbChangeID.serverSeq) {
serverSeq = Long.fromString(pbChangeID.serverSeq, true);
}

return ChangeID.of(
pbChangeID.clientSeq,
Long.fromString(pbChangeID.lamport, true),
toHexString(pbChangeID.actorId),
serverSeq,
);
}

Expand Down Expand Up @@ -1182,13 +1188,15 @@ function fromOperation(pbOperation: PbOperation): Operation | undefined {
const attributes = new Map();
const attributesToRemove = pbTreeStyleOperation.attributesToRemove;
const createdAtMapByActor = new Map();
Object.entries(pbTreeStyleOperation!.createdAtMapByActor).forEach(
([key, value]) => {
createdAtMapByActor.set(key, fromTimeTicket(value));
},
);
if (pbTreeStyleOperation?.createdAtMapByActor) {
Object.entries(pbTreeStyleOperation!.createdAtMapByActor).forEach(
([key, value]) => {
createdAtMapByActor.set(key, fromTimeTicket(value));
},
);
}

if (attributesToRemove.length > 0) {
if (attributesToRemove?.length > 0) {
return TreeStyleOperation.createTreeRemoveStyleOperation(
fromTimeTicket(pbTreeStyleOperation!.parentCreatedAt)!,
fromTreePos(pbTreeStyleOperation!.from!),
Expand Down
1 change: 1 addition & 0 deletions src/devtools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const unsubsByDocKey = new Map<string, Array<() => void>>();
* IndexedDB will be used.
*/
const transactionEventsByDocKey = new Map<string, Array<TransactionEvent>>();
(window as any).transactionEventsByDocKey = transactionEventsByDocKey;

/**
* `sendToPanel` sends a message to the devtools panel.
Expand Down
29 changes: 25 additions & 4 deletions src/document/change/change_id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,28 @@ export class ChangeID {
private lamport: Long;
private actor: ActorID;

constructor(clientSeq: number, lamport: Long, actor: ActorID) {
constructor(
clientSeq: number,
lamport: Long,
actor: ActorID,
serverSeq?: Long,
) {
this.clientSeq = clientSeq;
this.serverSeq = serverSeq;
this.lamport = lamport;
this.actor = actor;
}

/**
* `of` creates a new instance of ChangeID.
*/
public static of(clientSeq: number, lamport: Long, actor: ActorID): ChangeID {
return new ChangeID(clientSeq, lamport, actor);
public static of(
clientSeq: number,
lamport: Long,
actor: ActorID,
serverSeq?: Long,
): ChangeID {
return new ChangeID(clientSeq, lamport, actor, serverSeq);
}

/**
Expand Down Expand Up @@ -77,7 +88,7 @@ export class ChangeID {
* `setActor` sets the given actor.
*/
public setActor(actorID: ActorID): ChangeID {
return new ChangeID(this.clientSeq, this.lamport, actorID);
return new ChangeID(this.clientSeq, this.lamport, actorID, this.serverSeq);
}

/**
Expand All @@ -87,6 +98,16 @@ export class ChangeID {
return this.clientSeq;
}

/**
* `getServerSeq` returns the server sequence of this ID.
*/
public getServerSeq(): string {
if (this.serverSeq) {
return this.serverSeq.toString();
}
return '';
}

/**
* `getLamport` returns the lamport clock of this ID.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ export interface ChangeInfo<T = OperationInfo> {
message: string;
operations: Array<T>;
actor: ActorID;
clientSeq: number;
serverSeq: string;
}

/**
Expand Down Expand Up @@ -704,6 +706,8 @@ export class Document<T, P extends Indexable = Indexable> {
message: change.getMessage() || '',
operations: opInfos,
actor: actorID,
clientSeq: change.getID().getClientSeq(),
serverSeq: change.getID().getServerSeq(),
},
rawChange: this.isEnableDevtools() ? change.toStruct() : undefined,
});
Expand Down Expand Up @@ -1370,6 +1374,8 @@ export class Document<T, P extends Indexable = Indexable> {
source,
value: {
actor: actorID,
clientSeq: change.getID().getClientSeq(),
serverSeq: change.getID().getServerSeq(),
message: change.getMessage() || '',
operations: opInfos,
},
Expand All @@ -1380,6 +1386,8 @@ export class Document<T, P extends Indexable = Indexable> {
source,
value: {
actor: actorID,
clientSeq: change.getID().getClientSeq(),
serverSeq: change.getID().getServerSeq(),
message: change.getMessage() || '',
operations: opInfos,
},
Expand Down Expand Up @@ -1785,6 +1793,8 @@ export class Document<T, P extends Indexable = Indexable> {
message: change.getMessage() || '',
operations: opInfos,
actor: actorID,
clientSeq: change.getID().getClientSeq(),
serverSeq: change.getID().getServerSeq(),
},
rawChange: this.isEnableDevtools() ? change.toStruct() : undefined,
});
Expand Down Expand Up @@ -1876,6 +1886,8 @@ export class Document<T, P extends Indexable = Indexable> {
message: change.getMessage() || '',
operations: opInfos,
actor: actorID,
clientSeq: change.getID().getClientSeq(),
serverSeq: change.getID().getServerSeq(),
},
rawChange: this.isEnableDevtools() ? change.toStruct() : undefined,
});
Expand Down

0 comments on commit 0a421ee

Please sign in to comment.