*Track peer closed handlers outside of protomux with a map
This commit is contained in:
parent
81191ed7ce
commit
ce1854de83
16
src/index.ts
16
src/index.ts
|
@ -18,6 +18,8 @@ const PROTOCOL = "lumeweb.flood";
|
||||||
|
|
||||||
export const FLOOD_SYMBOL = Symbol.for(PROTOCOL);
|
export const FLOOD_SYMBOL = Symbol.for(PROTOCOL);
|
||||||
|
|
||||||
|
const closedMap = new Map();
|
||||||
|
|
||||||
export default class DHTFlood extends EventEmitter {
|
export default class DHTFlood extends EventEmitter {
|
||||||
private id: Buffer;
|
private id: Buffer;
|
||||||
private ttl: number;
|
private ttl: number;
|
||||||
|
@ -91,22 +93,28 @@ export default class DHTFlood extends EventEmitter {
|
||||||
let chan: any;
|
let chan: any;
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
if (!mux.opened({ protocol: this.protocol })) {
|
if (!mux.opened({ protocol: this.protocol })) {
|
||||||
chan = mux.createChannel({
|
chan = mux.createChannel({
|
||||||
protocol: this.protocol,
|
protocol: this.protocol,
|
||||||
async onopen() {
|
async onopen() {
|
||||||
self.emit("peer-open", peer);
|
self.emit("peer-open", peer);
|
||||||
},
|
},
|
||||||
async ondestroy() {
|
|
||||||
self.emit("peer-remove", peer);
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
if (chan) {
|
if (chan) {
|
||||||
peer[FLOOD_SYMBOL] = 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];
|
chan = peer[FLOOD_SYMBOL];
|
||||||
|
|
||||||
if (!chan) {
|
if (!chan) {
|
||||||
|
|
Loading…
Reference in New Issue