From e4e18a8a55c00887f27e7bb42f682e119049ffd6 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 16 Nov 2022 11:11:57 -0500 Subject: [PATCH] *Add a getter for data --- src/dhtOnlineBase.ts | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/dhtOnlineBase.ts b/src/dhtOnlineBase.ts index a222ef7..332f369 100644 --- a/src/dhtOnlineBase.ts +++ b/src/dhtOnlineBase.ts @@ -15,7 +15,6 @@ export default class DHTOnlineBase extends EventEmitter { private bootstrapped: boolean; private graph: any; private connectedTo: Set; - private data: {}; private encoding: codecs.Codec; 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)); }