Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove operations array from the DRP #292

Merged
merged 20 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"**/doc/*",
"**/*_pb.js",
"**/*_pb.ts",
"**/bundle/*"
"**/bundle/*",
"coverage/*"
d-roak marked this conversation as resolved.
Show resolved Hide resolved
]
},
"linter": {
Expand Down
2 changes: 1 addition & 1 deletion examples/canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function paint_pixel(pixel: HTMLDivElement) {
random_int(256),
];
canvasDRP.paint([x, y], painting);
const [r, g, b] = canvasDRP.pixel(x, y).color();
const [r, g, b] = canvasDRP.query_pixel(x, y).color();
pixel.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}

Expand Down
20 changes: 2 additions & 18 deletions examples/canvas/src/objects/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import { Pixel } from "./pixel";

export class Canvas implements DRP {
operations: string[] = ["splash", "paint"];
semanticsType: SemanticsType = SemanticsType.pair;

width: number;
Expand All @@ -26,18 +25,6 @@ export class Canvas implements DRP {
offset: [number, number],
size: [number, number],
rgb: [number, number, number],
): void {
this._splash(offset, size, rgb);
}

paint(offset: [number, number], rgb: [number, number, number]): void {
this._paint(offset, rgb);
}

private _splash(
offset: [number, number],
size: [number, number],
rgb: [number, number, number],
): void {
if (offset[0] < 0 || this.width < offset[0]) return;
if (offset[1] < 0 || this.height < offset[1]) return;
Expand All @@ -49,17 +36,14 @@ export class Canvas implements DRP {
}
}

private _paint(
offset: [number, number],
rgb: [number, number, number],
): void {
paint(offset: [number, number], rgb: [number, number, number]): void {
if (offset[0] < 0 || this.canvas.length < offset[0]) return;
if (offset[1] < 0 || this.canvas[offset[0]].length < offset[1]) return;

this.canvas[offset[0]][offset[1]].paint(rgb);
}

pixel(x: number, y: number): Pixel {
query_pixel(x: number, y: number): Pixel {
return this.canvas[x][y];
}

Expand Down
2 changes: 1 addition & 1 deletion examples/chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const render = () => {
element_objectPeers.innerHTML = `[${objectPeers.join(", ")}]`;

if (!chatDRP) return;
const chat = chatDRP.getMessages();
const chat = chatDRP.query_messages();
const element_chat = <HTMLDivElement>document.getElementById("chat");
element_chat.innerHTML = "";

Expand Down
11 changes: 1 addition & 10 deletions examples/chat/src/objects/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from "@ts-drp/object";

export class Chat implements DRP {
operations: string[] = ["addMessage"];
semanticsType: SemanticsType = SemanticsType.pair;
// store messages as strings in the format (timestamp, message, peerId)
messages: Set<string>;
Expand All @@ -16,18 +15,10 @@ export class Chat implements DRP {
}

addMessage(timestamp: string, message: string, peerId: string): void {
this._addMessage(timestamp, message, peerId);
}

private _addMessage(
timestamp: string,
message: string,
peerId: string,
): void {
this.messages.add(`(${timestamp}, ${message}, ${peerId})`);
}

getMessages(): Set<string> {
query_messages(): Set<string> {
return this.messages;
}

Expand Down
4 changes: 2 additions & 2 deletions examples/grid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const render = () => {
: `Your frens in GRID: [${objectPeers.map((peer) => `<strong style="color: ${getColorForPeerId(peer)};">${formatPeerId(peer)}</strong>`).join(", ")}]`;

if (!gridDRP) return;
const users = gridDRP.getUsers();
const users = gridDRP.query_users();
const element_grid = <HTMLDivElement>document.getElementById("grid");
element_grid.innerHTML = "";

Expand Down Expand Up @@ -119,7 +119,7 @@ const render = () => {

for (const userColorString of users) {
const [id, color] = userColorString.split(":");
const position = gridDRP.getUserPosition(userColorString);
const position = gridDRP.query_userPosition(userColorString);

if (position) {
const div = document.createElement("div");
Expand Down
13 changes: 2 additions & 11 deletions examples/grid/src/objects/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from "@ts-drp/object";

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

Expand All @@ -16,19 +15,11 @@ export class Grid implements DRP {
}

addUser(userId: string, color: string): void {
this._addUser(userId, color);
}

private _addUser(userId: string, color: string): void {
const userColorString = `${userId}:${color}`;
this.positions.set(userColorString, { x: 0, y: 0 });
}

moveUser(userId: string, direction: string): void {
this._moveUser(userId, direction);
}

private _moveUser(userId: string, direction: string): void {
const userColorString = [...this.positions.keys()].find((u) =>
u.startsWith(`${userId}:`),
);
Expand All @@ -53,11 +44,11 @@ export class Grid implements DRP {
}
}

getUsers(): string[] {
query_users(): string[] {
return [...this.positions.keys()];
}

getUserPosition(
query_userPosition(
userColorString: string,
): { x: number; y: number } | undefined {
const position = this.positions.get(userColorString);
Expand Down
13 changes: 6 additions & 7 deletions packages/blueprints/src/ACL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export enum ACLConflictResolution {
}

export class ACL implements IACL, DRP {
operations: string[] = ["grant", "revoke"];
semanticsType = SemanticsType.pair;

private _conflictResolution: ACLConflictResolution;
Expand All @@ -35,7 +34,7 @@ export class ACL implements IACL, DRP {
}

grant(senderId: string, peerId: string, publicKey: string): void {
if (!this.isAdmin(senderId)) {
if (!this.query_isAdmin(senderId)) {
throw new Error("Only admin nodes can grant permissions.");
}
this._grant(peerId, publicKey);
Expand All @@ -46,26 +45,26 @@ export class ACL implements IACL, DRP {
}

revoke(senderId: string, peerId: string): void {
if (!this.isAdmin(senderId)) {
if (!this.query_isAdmin(senderId)) {
throw new Error("Only admin nodes can revoke permissions.");
}
if (this.isAdmin(peerId)) {
if (this.query_isAdmin(peerId)) {
throw new Error(
"Cannot revoke permissions from a node with admin privileges.",
);
}
this._revoke(peerId);
}

isAdmin(peerId: string): boolean {
query_isAdmin(peerId: string): boolean {
return this._admins.has(peerId);
}

isWriter(peerId: string): boolean {
query_isWriter(peerId: string): boolean {
return this._writers.has(peerId);
}

getPeerKey(peerId: string): string | undefined {
query_getPeerKey(peerId: string): string | undefined {
return this._writers.get(peerId);
}

Expand Down
17 changes: 4 additions & 13 deletions packages/blueprints/src/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,26 @@ import {
} from "@ts-drp/object";

export class AddWinsSet<T> implements DRP {
operations: string[] = ["add", "remove"];
state: Map<T, boolean>;
semanticsType = SemanticsType.pair;

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

private _add(value: T): void {
if (!this.state.get(value)) this.state.set(value, true);
}

add(value: T): void {
this._add(value);
}

private _remove(value: T): void {
if (this.state.get(value)) this.state.set(value, false);
if (!this.state.get(value)) this.state.set(value, true);
}

remove(value: T): void {
this._remove(value);
if (this.state.get(value)) this.state.set(value, false);
}

contains(value: T): boolean {
query_contains(value: T): boolean {
return this.state.get(value) === true;
}

values(): T[] {
query_getValues(): T[] {
return Array.from(this.state.entries())
.filter(([_, exists]) => exists)
.map(([value, _]) => value);
Expand Down
22 changes: 7 additions & 15 deletions packages/blueprints/src/AddWinsSetWithACL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,19 @@ export class AddWinsSetWithACL<T> implements DRP {
this.state = new Map<T, boolean>();
}

private _add(value: T): void {
if (!this.state.get(value)) this.state.set(value, true);
}

add(value: T): void {
this._add(value);
}

private _remove(value: T): void {
if (this.state.get(value)) this.state.set(value, false);
if (!this.state.get(value)) this.state.set(value, true);
}

remove(value: T): void {
this._remove(value);
if (this.state.get(value)) this.state.set(value, false);
}

contains(value: T): boolean {
query_contains(value: T): boolean {
return this.state.get(value) === true;
}

values(): T[] {
query_getValues(): T[] {
return Array.from(this.state.entries())
.filter(([_, exists]) => exists)
.map(([value, _]) => value);
Expand All @@ -55,15 +47,15 @@ export class AddWinsSetWithACL<T> implements DRP {
return { action: ActionType.Nop };

if (
this.acl?.operations.includes(vertices[0].operation.type) &&
this.acl?.operations.includes(vertices[0].operation.type)
["grant", "revoke"].includes(vertices[0].operation.type) &&
["grant", "revoke"].includes(vertices[1].operation.type)
trungnotchung marked this conversation as resolved.
Show resolved Hide resolved
) {
return this.acl.resolveConflicts(vertices);
}

if (
this.operations.includes(vertices[0].operation.type) &&
this.operations.includes(vertices[0].operation.type)
this.operations.includes(vertices[1].operation.type)
) {
return vertices[0].operation.type === "add"
? { action: ActionType.DropRight }
Expand Down
17 changes: 4 additions & 13 deletions packages/blueprints/src/PseudoRandomWinsSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,26 @@ function computeHash(s: string): number {
The winning operation is chosen using a pseudo-random number generator.
*/
export class PseudoRandomWinsSet<T> implements DRP {
operations: string[] = ["add", "remove"];
state: Map<T, boolean>;
semanticsType = SemanticsType.multiple;

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

private _add(value: T): void {
if (!this.state.get(value)) this.state.set(value, true);
}

add(value: T): void {
this._add(value);
}

private _remove(value: T): void {
if (this.state.get(value)) this.state.set(value, false);
if (!this.state.get(value)) this.state.set(value, true);
}

remove(value: T): void {
this._remove(value);
if (this.state.get(value)) this.state.set(value, false);
}

contains(value: T): boolean {
query_contains(value: T): boolean {
return this.state.get(value) === true;
}

values(): T[] {
query_getValues(): T[] {
return Array.from(this.state.entries())
.filter(([_, exists]) => exists)
.map(([value, _]) => value);
Expand Down
12 changes: 6 additions & 6 deletions packages/blueprints/tests/AddWinsSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Add", () => {
drp.add(1);
let set = drp.values();
let set = drp.query_getValues();
expect(set).toEqual([1]);

drp.add(2);
set = drp.values();
set = drp.query_getValues();
expect(set).toEqual([1, 2]);
});

test("Test: Add and Remove", () => {
drp.add(1);
let set = drp.values();
let set = drp.query_getValues();
expect(set).toEqual([1]);

drp.add(2);
set = drp.values();
set = drp.query_getValues();
expect(set).toEqual([1, 2]);

drp.remove(1);
set = drp.values();
expect(drp.contains(1)).toBe(false);
set = drp.query_getValues();
expect(drp.query_contains(1)).toBe(false);
expect(set).toEqual([2]);
});
});
Loading
Loading