*Track peer closed handlers outside of protomux with a map

This commit is contained in:
Derrick Hammer 2022-11-27 03:34:26 -05:00
parent 81191ed7ce
commit ce1854de83
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 12 additions and 4 deletions

View File

@ -18,6 +18,8 @@ const PROTOCOL = "lumeweb.flood";
export const FLOOD_SYMBOL = Symbol.for(PROTOCOL);
const closedMap = new Map();
export default class DHTFlood extends EventEmitter {
private id: Buffer;
private ttl: number;
@ -91,22 +93,28 @@ export default class DHTFlood extends EventEmitter {
let chan: any;
const self = this;
if (!mux.opened({ protocol: this.protocol })) {
chan = mux.createChannel({
protocol: this.protocol,
async onopen() {
self.emit("peer-open", peer);
},
async ondestroy() {
self.emit("peer-remove", peer);
},
});
if (chan) {
peer[FLOOD_SYMBOL] = chan;
}
}
if (!closedMap.has(peer)) {
const close = () => {
self.emit("peer-remove", peer);
peer.removeListener("close", close);
closedMap.delete(peer);
};
peer.on("close", close);
}
chan = peer[FLOOD_SYMBOL];
if (!chan) {