Skip to content

Commit

Permalink
create baseDRP
Browse files Browse the repository at this point in the history
  • Loading branch information
trungnotchung committed Dec 6, 2024
1 parent 28a26ae commit c552816
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
21 changes: 4 additions & 17 deletions packages/blueprints/src/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import {
ActionType,
type DRP,
type Operation,
BaseDRP,
type ResolveConflictsType,
SemanticsType,
type Vertex,
} from "@ts-drp/object";

export class AddWinsSet<T> implements DRP {
operations: string[] = ["add", "remove"];
export class AddWinsSet<T> extends BaseDRP {
operations = ["add", "remove"];
state: Map<T, boolean>;
semanticsType = SemanticsType.pair;
// biome-ignore lint: attributes can be anything
[key: string]: any;

constructor() {
super();
this.state = new Map<T, boolean>();
}

Expand Down Expand Up @@ -59,15 +57,4 @@ export class AddWinsSet<T> implements DRP {
}
return { action: ActionType.Nop };
}

// biome-ignore lint: attributes can be anything
updateAttribute(key: string, value: any): void {
if (!(key in this)) {
throw new Error(`Key '${String(key)}' does not exist in this object.`);
}
if (typeof this[key] === "function") {
throw new Error(`Cannot update method '${key}' using updateAttribute.`);
}
this[key] = value;
}
}
19 changes: 3 additions & 16 deletions packages/blueprints/src/PseudoRandomWinsSet/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Smush32 } from "@thi.ng/random";
import {
ActionType,
type DRP,
BaseDRP,
type Hash,
type Operation,
type ResolveConflictsType,
SemanticsType,
type Vertex,
Expand All @@ -26,14 +25,13 @@ function computeHash(s: string): number {
An arbitrary number of concurrent operations can be reduced to a single operation.
The winning operation is chosen using a pseudo-random number generator.
*/
export class PseudoRandomWinsSet<T> implements DRP {
export class PseudoRandomWinsSet<T> extends BaseDRP {
operations: string[] = ["add", "remove"];
state: Map<T, boolean>;
semanticsType = SemanticsType.multiple;
// biome-ignore lint: attributes can be anything
[key: string]: any;

constructor() {
super();
this.state = new Map<T, boolean>();
}

Expand Down Expand Up @@ -72,15 +70,4 @@ export class PseudoRandomWinsSet<T> implements DRP {
hashes.splice(chosen, 1);
return { action: ActionType.Drop, vertices: hashes };
}

// biome-ignore lint: attributes can be anything
updateAttribute(key: string, value: any): void {
if (!(key in this)) {
throw new Error(`Key '${String(key)}' does not exist in this object.`);
}
if (typeof this[key] === "function") {
throw new Error(`Cannot update method '${key}' using updateAttribute.`);
}
this[key] = value;
}
}
19 changes: 19 additions & 0 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ export interface DRP {
updateAttribute(key: string, value: any): void;
}

export abstract class BaseDRP implements DRP {
abstract operations: string[];
abstract semanticsType: SemanticsType;
abstract resolveConflicts(vertices: Vertex[]): ResolveConflictsType;
// biome-ignore lint: attributes can be anything
[key: string]: any;

// biome-ignore lint: attributes can be anything
updateAttribute(key: string, value: any): void {
if (!(key in this)) {
throw new Error(`Key '${String(key)}' does not exist in this object.`);
}
if (typeof this[key] === "function") {
throw new Error(`Cannot update method '${key}' using updateAttribute.`);
}
this[key] = value;
}
}

type DRPState = {
// biome-ignore lint: attributes can be anything
state: Map<string, any>;
Expand Down

0 comments on commit c552816

Please sign in to comment.