Skip to content

Commit

Permalink
making chat working
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Aug 22, 2024
1 parent cd0bf59 commit 636aef7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 43 deletions.
30 changes: 5 additions & 25 deletions examples/chat/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import { TopologyObject } from "@topology-foundation/object";
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
import { TopologyObject_Operation } from "@topology-foundation/object";
import { addMessage, Chat } from "./objects/chat";

export const handleChatMessages = (chat: Chat, e: any) => {
console.log(e);
// const input = TopologyObject.decode(e.detail.msg.data);

console.log("Received message!: ", input);
const message = {};
console.log("Received message!: ", message);
switch (message["type"]) {
case "object_update": {
const fn = uint8ArrayToString(new Uint8Array(message["data"]));
handleObjectUpdate(chat, fn);
break;
}
default: {
break;
}
}
};

function handleObjectUpdate(chat: Chat, fn: string) {
export function handleObjectOps(chat: Chat, ops: TopologyObject_Operation[]) {
// In this case we only have addMessage
// `addMessage(${timestamp}, ${message}, ${node.getPeerId()})`
let args = fn.replace("addMessage(", "").replace(")", "").split(", ");
console.log("Received message: ", args);
try {
addMessage(chat, args[0], args[1], args[2]);
for (const op of ops) {
addMessage(chat, op.args[0], op.args[1], op.args[2]);
}
} catch (e) {
console.error(e);
}
Expand Down
14 changes: 9 additions & 5 deletions examples/chat/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TopologyNode } from "@topology-foundation/node";
import * as topology from "@topology-foundation/node";
import { Chat, addMessage, getMessages } from "./objects/chat";
import { handleChatMessages } from "./handlers";
import { handleObjectOps } from "./handlers";
import { GSet } from "@topology-foundation/crdt";
import { newTopologyObject, TopologyObject } from "@topology-foundation/object";

Expand Down Expand Up @@ -96,15 +95,17 @@ async function main() {
"/tmp/chat.ts",
);

// message handler for the CRO
node.addCustomGroupMessageHandler(topologyObject.id, (e) => {
// on create/connect
if (topologyObject)
objectPeers = node.networkNode.getGroupPeers(topologyObject.id);
handleChatMessages(chatCRO, e);
render();
});

node.objectStore.subscribe(topologyObject.id, (_, obj) => {
handleObjectOps(chatCRO, obj.operations);
});

(<HTMLButtonElement>document.getElementById("chatId")).innerHTML =
topologyObject.id;

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.
render();
Expand Down Expand Up @@ -135,9 +136,12 @@ async function main() {
// on create/connect
if (topologyObject)
objectPeers = node.networkNode.getGroupPeers(topologyObject.id);
handleChatMessages(chatCRO, e);
render();
});

node.objectStore.subscribe(topologyObject.id, (_, obj) => {
handleObjectOps(chatCRO, obj.operations);
});
});

let button_fetch = <HTMLButtonElement>(
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TopologyNetworkNode,
TopologyNetworkNodeConfig,
} from "@topology-foundation/network";
import { TopologyObjectStore, TopologyObjectStoreCallback } from "./store/index.js";
import { TopologyObjectStore } from "./store/index.js";
import { topologyMessagesHandler } from "./handlers.js";
import { OPERATIONS, executeObjectOperation } from "./operations.js";
import { TopologyObject } from "@topology-foundation/object";
Expand Down
5 changes: 4 additions & 1 deletion packages/node/src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ function updateObject(node: TopologyNode, data: Uint8Array) {
});
}

object.operations.push(...object_operations.operations);
for (const op of object_operations.operations) {
if (object.operations.some((o) => o.nonce === op.nonce)) continue;
object.operations.push(op);
}
node.objectStore.put(object.id, object);

const message = Message.create({
Expand Down
33 changes: 22 additions & 11 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@ import { compileWasm } from "./wasm/compiler.js";
export * from "./proto/object_pb.js";

/* Creates a new TopologyObject */
export async function newTopologyObject(peerId: string, path: string, id?: string, abi?: string): Promise<TopologyObject> {
export async function newTopologyObject(
peerId: string,
path: string,
id?: string,
abi?: string,
): Promise<TopologyObject> {
// const bytecode = await compileWasm(path);
const bytecode = new Uint8Array();
return {
id: id ?? crypto
.createHash("sha256")
.update(abi ?? "")
.update(peerId)
.update(Math.floor(Math.random() * Number.MAX_VALUE).toString())
.digest("hex"),
id:
id ??
crypto
.createHash("sha256")
.update(abi ?? "")
.update(peerId)
.update(Math.floor(Math.random() * Number.MAX_VALUE).toString())
.digest("hex"),
abi: abi ?? "",
bytecode: bytecode ?? new Uint8Array(),
operations: []
}
operations: [],
};
}

export async function callFn(obj: TopologyObject, fn: string, args: string[]): Promise<TopologyObject> {
export async function callFn(
obj: TopologyObject,
fn: string,
args: string[],
): Promise<TopologyObject> {
obj.operations.push({
nonce: Math.floor(Math.random() * Number.MAX_VALUE).toString(),
fn: fn,
args: args
args: args,
});
return obj;
}

0 comments on commit 636aef7

Please sign in to comment.