*Change setData to a setter

This commit is contained in:
Derrick Hammer 2022-11-16 11:14:44 -05:00
parent e4e18a8a55
commit 0016903687
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 11 additions and 11 deletions

View File

@ -36,6 +36,14 @@ export default class DHTOnlineBase extends EventEmitter {
return this._data; return this._data;
} }
set data(value: {}) {
this._data = value;
this._setPeer(this.id, value);
this._broadcastData();
}
private _online: string[]; private _online: string[];
get online(): string[] { get online(): string[] {
@ -50,14 +58,6 @@ export default class DHTOnlineBase extends EventEmitter {
return this.graph.node.get(this._maybeHexify(id)); 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) { protected onAddPeer(id: Buffer) {
const stringId = id.toString("hex"); const stringId = id.toString("hex");
if (this.connectedTo.has(stringId)) { if (this.connectedTo.has(stringId)) {

View File

@ -23,7 +23,7 @@ export default class DHTOnline extends DHTOnlineBase {
this.flood.send(peer, Buffer.from("hello"), 0) this.flood.send(peer, Buffer.from("hello"), 0)
); );
this.setData(data); this.data = data;
[...this.swarm.peers.values()].forEach(this.handlePeerAdd.bind(this)); [...this.swarm.peers.values()].forEach(this.handlePeerAdd.bind(this));
} }

View File

@ -22,8 +22,8 @@ test("Basic presence test / data propagation", (t) => {
const p1 = new DHTOnline(peer1); const p1 = new DHTOnline(peer1);
const p2 = new DHTOnline(peer2); const p2 = new DHTOnline(peer2);
p1.setData({ message: "Hello" }); p1.data = { message: "Hello" };
p2.setData({ message: "World!" }); p2.data = { message: "World!" };
t.ok(p1.id, "Generated id 1"); t.ok(p1.id, "Generated id 1");
t.ok(p2.id, "Generated id 2"); t.ok(p2.id, "Generated id 2");