Skip to content

Commit

Permalink
merged
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopereira12 committed Jul 8, 2024
2 parents 14da637 + 1b60467 commit 8d7b97f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function init() {
let croId = (<HTMLInputElement>document.getElementById("canvasIdInput"))
.value;
try {
await node.subscribeObject(croId);
await node.subscribeObject(croId, true);
// TODO remove the need to click to time for subscribe and fetch

let object: any = node.getObject(croId);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ts-topology",
"description": "The official TypeScript implementation of Topology Protocol",
"version": "0.0.2",
"version": "0.0.20",
"license": "MIT",
"homepage": "https://topology.gg/",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/crdt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@topology-foundation/crdt",
"version": "0.0.2",
"version": "0.0.20",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/network/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@topology-foundation/network",
"version": "0.0.2",
"version": "0.0.20",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
5 changes: 0 additions & 5 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
import { dcutr } from "@libp2p/dcutr";
import { identify } from "@libp2p/identify";
import { EventHandler, PubSub, Stream, StreamHandler } from "@libp2p/interface";
import { mdns } from "@libp2p/mdns";
import { pubsubPeerDiscovery } from "@libp2p/pubsub-peer-discovery";
import { webRTC, webRTCDirect } from "@libp2p/webrtc";
import { webSockets } from "@libp2p/websockets";
Expand Down Expand Up @@ -44,16 +43,12 @@ export class TopologyNetworkNode {
},
},
peerDiscovery: [
mdns({
interval: 10_000,
}),
pubsubPeerDiscovery({
interval: 10_000,
topics: ["topology::discovery"],
}),
bootstrap({
list: [
"/ip4/127.0.0.1/tcp/50000/ws/p2p/Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP",
"/dns4/relay.droak.sh/tcp/443/wss/p2p/Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP",
],
}),
Expand Down
10 changes: 6 additions & 4 deletions packages/node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@topology-foundation/node",
"version": "0.0.2",
"version": "0.0.20",
"license": "MIT",
"repository": {
"type": "git",
Expand Down Expand Up @@ -37,8 +37,10 @@
},
"dependencies": {
"@libp2p/dcutr": "^1.1.0",
"@topology-foundation/crdt": "0.0.2",
"@topology-foundation/network": "0.0.2",
"@topology-foundation/object": "0.0.2"

"@topology-foundation/crdt": "0.0.20",
"@topology-foundation/network": "0.0.20",
"@topology-foundation/object": "0.0.20"

}
}
48 changes: 48 additions & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ export class TopologyNode {
);
this._objectStore.put(object["id"], object);
}
case "object_sync": {
const objectId = uint8ArrayToString(
new Uint8Array(message["data"]),
);
const object = <TopologyObject>this.getObject(objectId);
const object_message = `{
"type": "object_merge",
"data": [${uint8ArrayFromString(JSON.stringify(object))}]
}`;
await this._networkNode.sendMessage(
message["sender"],
[<string>stream.protocol],
object_message,
);
break;
}
case "object_merge": {
const object = JSON.parse(
uint8ArrayToString(new Uint8Array(message["data"])),
);
const local = this._objectStore.get(object["id"]);
if (local) {
local.merge(object);
this._objectStore.put(object["id"], local);
}
}
default: {
return;
}
Expand Down Expand Up @@ -110,6 +136,28 @@ export class TopologyNode {
}
}

async syncObject(objectId: string, peerId = "") {
const message = `{
"type": "object_sync",
"sender": "${this._networkNode.peerId}",
"data": [${uint8ArrayFromString(objectId)}]
}`;

if (peerId === "") {
await this._networkNode.sendGroupMessageRandomPeer(
objectId,
["/topology/message/0.0.1"],
message,
);
} else {
await this._networkNode.sendMessage(
peerId,
["/topology/message/0.0.1"],
message,
);
}
}

getPeers() {
return this._networkNode.getAllPeers();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/object/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@topology-foundation/object",
"version": "0.0.2",
"version": "0.0.20",
"license": "MIT",
"repository": {
"type": "git",
Expand Down

0 comments on commit 8d7b97f

Please sign in to comment.