Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Oct 27, 2021
1 parent 14488b3 commit 4f0b6a0
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) {
* A map of open listeners for TALKREQ messages that have been set. Used to ensure event listeners
* are cleared when the expected response is returned or the timeout period expires
*/
private talkReqListeners: Map<string, (...args: [string, ENR, ITalkRespMessage, null]) => void>;
private talkReqListeners: Map<bigint, (...args: [string, ENR, ITalkRespMessage, null]) => void>;
/**
* Default constructor.
* @param sessionService the service managing sessions underneath.
Expand Down Expand Up @@ -310,25 +310,25 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) {
return await new Promise((resolve, reject) => {
const msg = createTalkRequestMessage(payload, protocol);
const responseTimeout = setTimeout(() => {
const event = this.talkReqListeners.get(msg.id.toString());
const event = this.talkReqListeners.get(msg.id);
if (event) {
this.removeListener("talkRespReceived", event);
this.talkReqListeners.delete(msg.id.toString());
this.talkReqListeners.delete(msg.id);
}
reject("Request timed out");
}, timeout);
const listener = (srcId: string, enr: ENR | null, res: ITalkRespMessage): void => {
if (res.id === msg.id) {
const event = this.talkReqListeners.get(msg.id.toString());
const event = this.talkReqListeners.get(msg.id);
if (event) {
this.removeListener("talkRespReceived", event);
this.talkReqListeners.delete(msg.id.toString());
this.talkReqListeners.delete(msg.id);
}
clearTimeout(responseTimeout);
resolve(res.response);
}
};
this.talkReqListeners.set(msg.id.toString(), listener);
this.talkReqListeners.set(msg.id, listener);
this.on("talkRespReceived", listener);

for (const node of this.kadValues()) {
Expand All @@ -354,25 +354,25 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) {
return await new Promise((resolve, reject) => {
const msg = createTalkRequestMessage(payload, protocol);
const responseTimeout = setTimeout(() => {
const event = this.talkReqListeners.get(msg.id.toString());
const event = this.talkReqListeners.get(msg.id);
if (event) {
this.removeListener("talkRespReceived", event);
this.talkReqListeners.delete(msg.id.toString());
this.talkReqListeners.delete(msg.id);
}
reject("Request timed out");
}, timeout);
const listener = (srcId: string, enr: ENR | null, res: ITalkRespMessage): void => {
if (res.id === msg.id) {
clearTimeout(responseTimeout);
resolve(res.response);
const event = this.talkReqListeners.get(msg.id.toString());
const event = this.talkReqListeners.get(msg.id);
if (event) {
this.removeListener("talkRespReceived", event);
this.talkReqListeners.delete(msg.id.toString());
this.talkReqListeners.delete(msg.id);
}
}
};
this.talkReqListeners.set(msg.id.toString(), listener);
this.talkReqListeners.set(msg.id, listener);

this.on("talkRespReceived", listener);
const sendStatus = this.sendRequest(dstId, msg);
Expand Down

0 comments on commit 4f0b6a0

Please sign in to comment.