*Add a getter for data

This commit is contained in:
Derrick Hammer 2022-11-16 11:11:57 -05:00
parent 866c91c0c6
commit e4e18a8a55
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 24 additions and 19 deletions

View File

@ -15,7 +15,6 @@ export default class DHTOnlineBase extends EventEmitter {
private bootstrapped: boolean;
private graph: any;
private connectedTo: Set<any>;
private data: {};
private encoding: codecs.Codec<any>;
constructor(id: Buffer, { encoding = DEFAULT_ENCODING } = {}) {
@ -26,11 +25,17 @@ export default class DHTOnlineBase extends EventEmitter {
this.bootstrapped = false;
this.graph = new DiGraph();
this.connectedTo = new Set();
this.data = {};
this._data = {};
this.encoding = codecs(encoding || DEFAULT_ENCODING);
this._online = [this._maybeHexify(this.id)];
}
private _data: {};
get data(): {} {
return this._data;
}
private _online: string[];
get online(): string[] {
@ -46,29 +51,13 @@ export default class DHTOnlineBase extends EventEmitter {
}
public setData(data: any) {
this.data = data;
this._data = data;
this._setPeer(this.id, data);
this._broadcastData();
}
private _broadcastData() {
const rawData = this.data;
if (!Object.keys(rawData).length) {
return;
}
const data = this.encoding.encode(rawData);
this.broadcast(
Message.toBinary(
Message.create({
type: Type.STATE,
data,
})
)
);
}
protected onAddPeer(id: Buffer) {
const stringId = id.toString("hex");
if (this.connectedTo.has(stringId)) {
@ -168,6 +157,22 @@ export default class DHTOnlineBase extends EventEmitter {
}
}
private _broadcastData() {
const rawData = this._data;
if (!Object.keys(rawData).length) {
return;
}
const data = this.encoding.encode(rawData);
this.broadcast(
Message.toBinary(
Message.create({
type: Type.STATE,
data,
})
)
);
}
private _hasSeenPeer(id: Buffer | string) {
return this.graph.hasNode(this._maybeHexify(id));
}