Client: move socket connection out of the constructor

It will make it easier to write tests for what used to be in
the connect() method
This commit is contained in:
Val Lorentz 2023-03-14 21:24:06 +01:00
parent 76098d7e76
commit a049a01aeb
5 changed files with 29 additions and 11 deletions

View file

@ -180,8 +180,16 @@ class Client {
this.registerPushSubscription(session, session.pushSubscription, true);
}
});
}
(client.config.networks || []).forEach((network) => client.connect(network, true));
connect() {
const client = this;
if (client.networks.length !== 0) {
throw new Error(`${client.name} is already connected`);
}
(client.config.networks || []).forEach((network) => client.connectToNetwork(network, true));
// Networks are stored directly in the client object
// We don't need to keep it in the config object
@ -192,7 +200,7 @@ class Client {
// Networks are created instantly, but to reduce server load on startup
// We randomize the IRC connections and channel log loading
let delay = manager.clients.length * 500;
let delay = client.manager.clients.length * 500;
client.networks.forEach((network) => {
setTimeout(() => {
network.channels.forEach((channel) => channel.loadMessages(client, network));
@ -205,7 +213,7 @@ class Client {
delay += 1000 + Math.floor(Math.random() * 1000);
});
client.fileHash = manager.getDataToSave(client).newHash;
client.fileHash = client.manager.getDataToSave(client).newHash;
}
}
@ -242,12 +250,10 @@ class Client {
return false;
}
connect(args: Record<string, any>, isStartup = false) {
networkFromConfig(args: Record<string, any>): Network {
const client = this;
let channels: Chan[] = [];
// Get channel id for lobby before creating other channels for nicer ids
const lobbyChannelId = client.idChan++;
let channels: Chan[] = [];
if (Array.isArray(args.channels)) {
let badName = false;
@ -295,7 +301,7 @@ class Client {
}
// TODO; better typing for args
const network = new Network({
return new Network({
uuid: args.uuid,
name: String(
args.name || (Config.values.lockNetwork ? Config.values.defaults.name : "") || ""
@ -323,6 +329,15 @@ class Client {
proxyUsername: String(args.proxyUsername || ""),
proxyPassword: String(args.proxyPassword || ""),
});
}
connectToNetwork(args: Record<string, any>, isStartup = false) {
const client = this;
// Get channel id for lobby before creating other channels for nicer ids
const lobbyChannelId = client.idChan++;
const network = this.networkFromConfig(args);
// Set network lobby channel id
network.getLobby().id = lobbyChannelId;
@ -363,7 +378,7 @@ class Client {
if (!isStartup) {
client.save();
channels.forEach((channel) => channel.loadMessages(client, network));
network.channels.forEach((channel) => channel.loadMessages(client, network));
}
}

View file

@ -145,6 +145,7 @@ class ClientManager {
}
} else {
client = new Client(this, name, userConfig);
client.connect();
this.clients.push(client);
}

View file

@ -39,7 +39,7 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
}
const host = args[0];
this.connect({host, port, tls});
this.connectToNetwork({host, port, tls});
return true;
};

View file

@ -485,7 +485,7 @@ function initializeClient(
data.commands = null;
data.ignoreList = null;
client.connect(data);
client.connectToNetwork(data);
}
});
@ -948,6 +948,7 @@ function performAuthentication(this: Socket, data) {
if (Config.values.public) {
client = new Client(manager!);
client.connect();
manager!.clients.push(client);
socket.on("disconnect", function () {

View file

@ -27,6 +27,7 @@ describe("Custom highlights", function () {
},
} as any
);
client.connect();
logInfoStub.restore();
expect(userLoadedLog).to.equal("User test loaded\n");