*Store the listenConnections connection so don't create duplicates incase init/ready is called multiple times
This commit is contained in:
parent
14a71cffd9
commit
290ca18c4b
39
src/index.ts
39
src/index.ts
|
@ -15,6 +15,10 @@ export class SwarmClient extends Client {
|
||||||
private _connectBackoff: any;
|
private _connectBackoff: any;
|
||||||
|
|
||||||
private _ready?: Promise<void>;
|
private _ready?: Promise<void>;
|
||||||
|
private _connectionListener?: [
|
||||||
|
sendUpdate: DataFn,
|
||||||
|
response: Promise<ErrTuple>
|
||||||
|
];
|
||||||
|
|
||||||
private _topics: Set<Uint8Array> = new Set<Uint8Array>();
|
private _topics: Set<Uint8Array> = new Set<Uint8Array>();
|
||||||
private _sockets: Map<number, Socket> = new Map<number, Socket>();
|
private _sockets: Map<number, Socket> = new Map<number, Socket>();
|
||||||
|
@ -87,26 +91,29 @@ export class SwarmClient extends Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _listen() {
|
private async _listen() {
|
||||||
const connect = this.connectModule(
|
if (!this._connectionListener) {
|
||||||
"listenConnections",
|
this._connectionListener = this.connectModule(
|
||||||
{ swarm: this.swarm },
|
"listenConnections",
|
||||||
async (socketId: any) => {
|
{ swarm: this.swarm },
|
||||||
const socket =
|
async (socketId: any) => {
|
||||||
this._sockets.get(socketId) ?? (await createSocket(socketId));
|
const socket =
|
||||||
|
this._sockets.get(socketId) ?? (await createSocket(socketId));
|
||||||
|
|
||||||
socket.on("close", () => {
|
socket.on("close", () => {
|
||||||
this._sockets.delete(socketId);
|
this._sockets.delete(socketId);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!this._sockets.has(socketId)) {
|
if (!this._sockets.has(socketId)) {
|
||||||
this._sockets.set(socketId, socket);
|
this._sockets.set(socketId, socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit("connection", socket);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this.emit("connection", socket);
|
await this._connectionListener[1];
|
||||||
}
|
this._connectionListener = undefined;
|
||||||
);
|
|
||||||
|
|
||||||
await connect[1];
|
|
||||||
this.start();
|
this.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue