diff --git a/packages/crdt/src/cros/AddWinsSet/index.ts b/packages/crdt/src/cros/AddWinsSet/index.ts index c9cac8d7..eb036311 100644 --- a/packages/crdt/src/cros/AddWinsSet/index.ts +++ b/packages/crdt/src/cros/AddWinsSet/index.ts @@ -1,5 +1,9 @@ -import { ActionType, CRO } from "@topology-foundation/object"; -import { Vertex } from "@topology-foundation/object/src"; +import { + ActionType, + type CRO, + type Operation, + type Vertex, +} from "@topology-foundation/object"; enum OperationType { Add = 0, @@ -7,11 +11,6 @@ enum OperationType { Nop = 2, } -interface Operation { - type: OperationType; - value: T; -} - /// AddWinsSet with support for state and op changes export class AddWinsSet implements CRO { operations: Operation[]; @@ -23,13 +22,13 @@ export class AddWinsSet implements CRO { } add(value: T): void { - const op: Operation = { OperationType.Add, value }; + const op: Operation = { type: OperationType.Add, value }; this.operations.push(op); this.state.set(value, (this.state.get(value) || 0) + 1); } remove(value: T): void { - const op = new Operation(OperationType.Remove, value); + const op: Operation = { type: OperationType.Remove, value }; this.operations.push(op); this.add(value); } @@ -64,9 +63,13 @@ export class AddWinsSet implements CRO { .map(([value, _]) => value); } - resolveConflicts(v1: Vertex, v2: Vertex): ActionType { - if (v1.operation.type !== v2.operation.type && v1.operation.value === v2.operation.value) { - return v1.operation.type === OperationType.Add + // in this case is an array of length 2 + resolveConflicts(vertices: Vertex[]): ActionType { + if ( + vertices[0].operation.type !== vertices[1].operation.type && + vertices[0].operation.value === vertices[1].operation.value + ) { + return vertices[0].operation.type === OperationType.Add ? ActionType.DropRight : ActionType.DropLeft; } diff --git a/packages/object/src/hashgraph.ts b/packages/object/src/hashgraph.ts index db40f711..93f10b0d 100644 --- a/packages/object/src/hashgraph.ts +++ b/packages/object/src/hashgraph.ts @@ -1,7 +1,7 @@ import * as crypto from "node:crypto"; type Hash = string; -type Operation = { type: number; value: T | null }; +export type Operation = { type: number; value: T | null }; enum OperationType { NOP = -1, @@ -25,7 +25,7 @@ export interface Vertex { export class HashGraph { nodeId: string; - resolveConflicts: (v1: Vertex, v2: Vertex) => ActionType; + resolveConflicts: (vertices: Vertex[]) => ActionType; vertices: Map> = new Map(); frontier: Set = new Set(); @@ -38,7 +38,7 @@ export class HashGraph { constructor( nodeId: string, - resolveConflicts: (v1: Vertex, v2: Vertex) => ActionType, + resolveConflicts: (vertices: Vertex[]) => ActionType, ) { this.nodeId = nodeId; this.resolveConflicts = resolveConflicts; @@ -155,13 +155,13 @@ export class HashGraph { const moving = order[j]; if (!this.areCausallyRelated(anchor, moving)) { - const op1 = this.vertices.get(anchor); - const op2 = this.vertices.get(moving); + const v1 = this.vertices.get(anchor); + const v2 = this.vertices.get(moving); let action: ActionType; - if (!op1 || !op2) { + if (!v1 || !v2) { action = ActionType.Nop; } else { - action = this.resolveConflicts(op1, op2); + action = this.resolveConflicts([v1, v2]); } switch (action) { diff --git a/packages/object/src/index.ts b/packages/object/src/index.ts index b7e7e6dd..c40e6cb3 100644 --- a/packages/object/src/index.ts +++ b/packages/object/src/index.ts @@ -7,11 +7,7 @@ export * from "./proto/object_pb.js"; export * from "./hashgraph.js"; export interface CRO { - resolveConflicts: ( - op1: hashgraph.Vertex, - op2: hashgraph.Vertex, - ) => hashgraph.ActionType; - merge: (cro: CRO) => void; + resolveConflicts: (vertices: hashgraph.Vertex[]) => hashgraph.ActionType; } /* Creates a new TopologyObject */