Skip to content

Commit

Permalink
more changes to the chat; added fn in topology obj
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Aug 15, 2024
1 parent 41cc0da commit 63632e5
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 19 deletions.
6 changes: 3 additions & 3 deletions examples/chat/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
import { Chat } from "./objects/chat";
import { addMessage, Chat } from "./objects/chat";

export const handleChatMessages = (chat: ChatI, e: any) => {
if (e.detail.msg.topic === "topology::discovery") return;
Expand All @@ -18,13 +18,13 @@ export const handleChatMessages = (chat: ChatI, e: any) => {
}
};

function handleObjectUpdate(chat: ChatI, fn: string) {
function handleObjectUpdate(chat: Chat, fn: string) {
// 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 {
chat.addMessage(args[0], args[1], args[2]);
addMessage(chat, args[0], args[1], args[2])
} catch (e) {
console.error(e);
}
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 @@ -2,7 +2,7 @@ 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 { GSet } from "@topology-foundation/crdt";
import { GSet } from "@topology-foundation/crdt";
import { newTopologyObject } from "@topology-foundation/object";

const node = new TopologyNode();
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/proto/messages_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@ export async function newTopologyObject(peerId: string, path: string, id?: strin
.digest("hex"),
abi: abi ?? "",
bytecode: bytecode ?? new Uint8Array(),
operations: []
}
}

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
});

return obj;
}

async function run() {
// TODO: just for testing wasm compilation with tsx, should be deleted
let obj = await newTopologyObject("peerId", "/Users/droak/code/topology/ts-topology/examples/chat/src/objects/chat.ts", "", "");
console.log(obj);
}
run();
// run();
11 changes: 9 additions & 2 deletions packages/object/src/proto/object.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ syntax = "proto3";
package topology.object;

message TopologyObject {
message Operation {
string nonce = 1;
optional string fn = 2;
repeated string args = 3;
};

string id = 1;
string abi = 2;
bytes bytecode = 3;
optional string abi = 2;
optional bytes bytecode = 3;
repeated Operation operations = 4;
}
135 changes: 124 additions & 11 deletions packages/object/src/proto/object_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63632e5

Please sign in to comment.