From e35b60213355aa88cee55c598c738863d0fce939 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 4 Dec 2022 12:03:36 -0500 Subject: [PATCH] *Add getter for dhtCache --- src/modules/rpc/cache.ts | 41 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/modules/rpc/cache.ts b/src/modules/rpc/cache.ts index 7ebfcef..43e5bd3 100644 --- a/src/modules/rpc/cache.ts +++ b/src/modules/rpc/cache.ts @@ -10,9 +10,28 @@ import crypto from "hypercore-crypto"; import NodeCache from "node-cache"; export class RPCCache extends EventEmitter { - private dhtCache?: DHTCache; private server: RPCServer; + constructor(server: RPCServer) { + super(); + this.server = server; + this._swarm = getSwarm(); + this._dhtCache = new DHTCache(this._swarm, { + protocol: "lumeweb.rpccache", + }); + this._data.on("del", (key: string) => { + try { + this.deleteItem(key); + } catch {} + }); + } + + private _dhtCache?: DHTCache; + + get dhtCache(): DHTCache { + return this._dhtCache as DHTCache; + } + private _swarm?: any; get swarm(): any { @@ -25,20 +44,6 @@ export class RPCCache extends EventEmitter { return this._data; } - constructor(server: RPCServer) { - super(); - this.server = server; - this._swarm = getSwarm(); - this.dhtCache = new DHTCache(this._swarm, { - protocol: "lumeweb.rpccache", - }); - this._data.on("del", (key: string) => { - try { - this.deleteItem(key); - } catch {} - }); - } - public signResponse(item: RPCCacheItem): string { const field = item.value.signedField || "data"; const updated = item.value.updated; @@ -93,18 +98,18 @@ export class RPCCache extends EventEmitter { item.signature = this.signResponse(item); - this.dhtCache?.addItem(queryHash); + this._dhtCache?.addItem(queryHash); this._data.set(queryHash, item); } public deleteItem(queryHash: string): boolean { - const cache = this.dhtCache?.cache; + const cache = this._dhtCache?.cache; if (!cache?.includes(queryHash)) { throw Error("item does not exist"); } - this.dhtCache?.removeItem(queryHash); + this._dhtCache?.removeItem(queryHash); this._data.del(queryHash); return true;