Skip to content

Commit

Permalink
update example grid
Browse files Browse the repository at this point in the history
  • Loading branch information
trungnotchung committed Dec 9, 2024
1 parent f60e742 commit 2377ec1
Showing 1 changed file with 3 additions and 25 deletions.
28 changes: 3 additions & 25 deletions examples/grid/src/objects/grid.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {
ActionType,
type DRP,
type Operation,
BaseDRP,
type ResolveConflictsType,
SemanticsType,
type Vertex,
} from "@ts-drp/object";

export class Grid implements DRP {
export class Grid extends BaseDRP {
operations: string[] = ["addUser", "moveUser"];
semanticsType: SemanticsType = SemanticsType.pair;
positions: Map<string, { x: number; y: number }>;

constructor() {
super();
this.positions = new Map<string, { x: number; y: number }>();
}

Expand Down Expand Up @@ -71,28 +71,6 @@ export class Grid implements DRP {
resolveConflicts(vertices: Vertex[]): ResolveConflictsType {
return { action: ActionType.Nop };
}

mergeCallback(operations: Operation[]): void {
// reset this.positions
this.positions = new Map<string, { x: number; y: number }>();

// apply operations to this.positions
for (const op of operations) {
if (!op.value) continue;
switch (op.type) {
case "addUser": {
const [userId, color] = op.value;
this._addUser(userId, color);
break;
}
case "moveUser": {
const [userId, direction] = op.value;
this._moveUser(userId, direction);
break;
}
}
}
}
}

export function createGrid(): Grid {
Expand Down

0 comments on commit 2377ec1

Please sign in to comment.