Skip to content

Commit

Permalink
handlers for updates in the example side
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Jun 20, 2024
1 parent a30fd2e commit 7c0f67f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
43 changes: 43 additions & 0 deletions examples/canvas/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
import { ICanvas } from "./objects/canvas";

// TODO: this should be superseded by wasm and main ts-topology library
export const handleCanvasMessages = (canvas: ICanvas, e: any) => {
if (e.detail.msg.topic === "_peer-discovery._p2p._pubsub") return;
const input = uint8ArrayToString(e.detail.msg.data);
const message = JSON.parse(input);
switch (message["type"]) {
case "object_update": {
const fn = uint8ArrayToString(new Uint8Array(message["data"]));
handleObjectUpdate(canvas, fn);
break;
}
default: {
break;
}
}
};

function handleObjectUpdate(canvas: ICanvas, fn: string) {
// In this case we only have paint
// `paint(${node.getPeerId()}, [${[x, y]}], [${painting}])`
let args = fn.replace("paint(", "").replace(")", "").split(", ");
let offset_p = args[1]
.replace("[", "")
.replace("]", "")
.split(",")
.map((s) => parseInt(s, 10));
const offset: [number, number] = [offset_p[0], offset_p[1]];
let rgb_p = args[2]
.replace("[", "")
.replace("]", "")
.split(",")
.map((s) => parseInt(s, 10));
const rgb: [number, number, number] = [rgb_p[0], rgb_p[1], rgb_p[2]];

try {
canvas.paint(args[0], offset, rgb);
} catch (e) {
console.error(e);
}
}
8 changes: 7 additions & 1 deletion examples/canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TopologyNode } from "@topology-foundation/node";
import { Canvas, ICanvas } from "./objects/canvas";
import { Pixel } from "./objects/pixel";
import { GCounter } from "@topology-foundation/crdt";
import { handleCanvasMessages } from "./handlers";

const node = new TopologyNode();
let canvasCRO: ICanvas;
Expand Down Expand Up @@ -45,13 +46,18 @@ async function paint_pixel(pixel: HTMLDivElement) {

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

async function init() {
await node.start();

node.addCustomGroupMessageHandler((e) => {
handleCanvasMessages(canvasCRO, e);
if (canvasCRO) render();
});

let create_button = <HTMLButtonElement>document.getElementById("create");
create_button.addEventListener("click", () => {
canvasCRO = new Canvas(node.getPeerId(), 5, 10);
Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export class TopologyNode {

this._networkNode.addGroupMessageHandler((e) => {
if (e.detail.msg.topic === "_peer-discovery._p2p._pubsub") return;
console.log(e, e.detail);

// TODO: add base handler here after July demo
// send the events to the app handler
// const message = JSON.parse(new TextDecoder().decode(e.detail.data));
});
Expand Down

0 comments on commit 7c0f67f

Please sign in to comment.