diff --git a/src/dhtOnlineBase.ts b/src/dhtOnlineBase.ts index 332f369..d759a8f 100644 --- a/src/dhtOnlineBase.ts +++ b/src/dhtOnlineBase.ts @@ -36,6 +36,14 @@ export default class DHTOnlineBase extends EventEmitter { return this._data; } + set data(value: {}) { + this._data = value; + + this._setPeer(this.id, value); + + this._broadcastData(); + } + private _online: string[]; get online(): string[] { @@ -50,14 +58,6 @@ export default class DHTOnlineBase extends EventEmitter { return this.graph.node.get(this._maybeHexify(id)); } - public setData(data: any) { - this._data = data; - - this._setPeer(this.id, data); - - this._broadcastData(); - } - protected onAddPeer(id: Buffer) { const stringId = id.toString("hex"); if (this.connectedTo.has(stringId)) { diff --git a/src/index.ts b/src/index.ts index 83395e4..0c8a0de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,7 @@ export default class DHTOnline extends DHTOnlineBase { this.flood.send(peer, Buffer.from("hello"), 0) ); - this.setData(data); + this.data = data; [...this.swarm.peers.values()].forEach(this.handlePeerAdd.bind(this)); } diff --git a/test.js b/test.js index 34ff966..1011236 100644 --- a/test.js +++ b/test.js @@ -22,8 +22,8 @@ test("Basic presence test / data propagation", (t) => { const p1 = new DHTOnline(peer1); const p2 = new DHTOnline(peer2); - p1.setData({ message: "Hello" }); - p2.setData({ message: "World!" }); + p1.data = { message: "Hello" }; + p2.data = { message: "World!" }; t.ok(p1.id, "Generated id 1"); t.ok(p2.id, "Generated id 2");