Skip to content

Commit

Permalink
chore: restructuring (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak authored Jul 21, 2024
1 parent 6275d99 commit b4ba1ef
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 4,258 deletions.
19 changes: 11 additions & 8 deletions examples/canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ async function paint_pixel(pixel: HTMLDivElement) {
random_int(256),
random_int(256),
];
canvasCRO.paint(node.getPeerId(), [x, y], painting);
canvasCRO.paint(node.networkNode.peerId, [x, y], painting);
const [r, g, b] = canvasCRO.pixel(x, y).color();
pixel.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;

node.updateObject(
canvasCRO,
`paint(${node.getPeerId()}, [${[x, y]}], [${painting}])`,
`paint(${node.networkNode.peerId}, [${[x, y]}], [${painting}])`,
);
}

Expand All @@ -72,17 +72,17 @@ async function init() {

node.addCustomGroupMessageHandler((e) => {
handleCanvasMessages(canvasCRO, e);
peers = node.getPeers();
discoveryPeers = node.getPeersPerGroup("topology::discovery");
peers = node.networkNode.getAllPeers();
discoveryPeers = node.networkNode.getGroupPeers("topology::discovery");
if (canvasCRO) {
objectPeers = node.getPeersPerGroup(canvasCRO.getObjectId());
objectPeers = node.networkNode.getGroupPeers(canvasCRO.getObjectId());
}
render();
});

let create_button = <HTMLButtonElement>document.getElementById("create");
create_button.addEventListener("click", () => {
canvasCRO = new Canvas(node.getPeerId(), 5, 10);
canvasCRO = new Canvas(node.networkNode.peerId, 5, 10);
node.createObject(canvasCRO);

(<HTMLSpanElement>document.getElementById("canvasId")).innerText =
Expand All @@ -104,11 +104,14 @@ async function init() {
y["red"] = Object.assign(new GCounter({}), y["red"]);
y["green"] = Object.assign(new GCounter({}), y["green"]);
y["blue"] = Object.assign(new GCounter({}), y["blue"]);
return Object.assign(new Pixel(node.getPeerId()), y);
return Object.assign(new Pixel(node.networkNode.peerId), y);
}),
);

canvasCRO = Object.assign(new Canvas(node.getPeerId(), 0, 0), object);
canvasCRO = Object.assign(
new Canvas(node.networkNode.peerId, 0, 0),
object,
);

(<HTMLSpanElement>document.getElementById("canvasId")).innerText = croId;
render();
Expand Down
18 changes: 9 additions & 9 deletions examples/canvas/src/objects/pixel.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { GCounter, IGCounter } from "@topology-foundation/crdt";
import { GCounter } from "@topology-foundation/crdt";
import { TopologyObject } from "@topology-foundation/object";

export interface IPixel extends TopologyObject {
red: IGCounter;
green: IGCounter;
blue: IGCounter;
red: GCounter;
green: GCounter;
blue: GCounter;
color(): [number, number, number];
paint(nodeId: string, rgb: [number, number, number]): void;
counters(): [IGCounter, IGCounter, IGCounter];
counters(): [GCounter, GCounter, GCounter];
merge(peerPixel: IPixel): void;
}

export class Pixel extends TopologyObject implements IPixel {
red: IGCounter;
green: IGCounter;
blue: IGCounter;
red: GCounter;
green: GCounter;
blue: GCounter;

constructor(peerId: string) {
super(peerId);
Expand All @@ -37,7 +37,7 @@ export class Pixel extends TopologyObject implements IPixel {
this.blue.increment(nodeId, rgb[2]);
}

counters(): [IGCounter, IGCounter, IGCounter] {
counters(): [GCounter, GCounter, GCounter] {
return [this.red, this.green, this.blue];
}

Expand Down
Loading

0 comments on commit b4ba1ef

Please sign in to comment.