From 636aef75e519166492933a70ead2e53b7ffe7f45 Mon Sep 17 00:00:00 2001 From: droak Date: Thu, 22 Aug 2024 14:25:06 +0900 Subject: [PATCH] making chat working --- examples/chat/src/handlers.ts | 30 +++++------------------------- examples/chat/src/index.ts | 14 +++++++++----- packages/node/src/index.ts | 2 +- packages/node/src/operations.ts | 5 ++++- packages/object/src/index.ts | 33 ++++++++++++++++++++++----------- 5 files changed, 41 insertions(+), 43 deletions(-) diff --git a/examples/chat/src/handlers.ts b/examples/chat/src/handlers.ts index 7126fd33..66985fe1 100644 --- a/examples/chat/src/handlers.ts +++ b/examples/chat/src/handlers.ts @@ -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); } diff --git a/examples/chat/src/index.ts b/examples/chat/src/index.ts index 99465db8..ef83e91e 100644 --- a/examples/chat/src/index.ts +++ b/examples/chat/src/index.ts @@ -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"; @@ -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); + }); + (document.getElementById("chatId")).innerHTML = topologyObject.id; render(); @@ -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 = ( diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index 1b6ad2dd..968d63ab 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -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"; diff --git a/packages/node/src/operations.ts b/packages/node/src/operations.ts index aa08ae44..4cc8e76c 100644 --- a/packages/node/src/operations.ts +++ b/packages/node/src/operations.ts @@ -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({ diff --git a/packages/object/src/index.ts b/packages/object/src/index.ts index e72bc819..afbab8c8 100644 --- a/packages/object/src/index.ts +++ b/packages/object/src/index.ts @@ -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 { +export async function newTopologyObject( + peerId: string, + path: string, + id?: string, + abi?: string, +): Promise { // 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 { +export async function callFn( + obj: TopologyObject, + fn: string, + args: string[], +): Promise { obj.operations.push({ nonce: Math.floor(Math.random() * Number.MAX_VALUE).toString(), fn: fn, - args: args + args: args, }); return obj; }