mirror of
https://github.com/thelounge/thelounge
synced 2024-11-25 05:20:21 +00:00
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:
parent
76098d7e76
commit
a049a01aeb
5 changed files with 29 additions and 11 deletions
|
@ -180,8 +180,16 @@ class Client {
|
||||||
this.registerPushSubscription(session, session.pushSubscription, true);
|
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
|
// Networks are stored directly in the client object
|
||||||
// We don't need to keep it in the config 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
|
// Networks are created instantly, but to reduce server load on startup
|
||||||
// We randomize the IRC connections and channel log loading
|
// 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) => {
|
client.networks.forEach((network) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
network.channels.forEach((channel) => channel.loadMessages(client, network));
|
network.channels.forEach((channel) => channel.loadMessages(client, network));
|
||||||
|
@ -205,7 +213,7 @@ class Client {
|
||||||
delay += 1000 + Math.floor(Math.random() * 1000);
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(args: Record<string, any>, isStartup = false) {
|
networkFromConfig(args: Record<string, any>): Network {
|
||||||
const client = this;
|
const client = this;
|
||||||
let channels: Chan[] = [];
|
|
||||||
|
|
||||||
// Get channel id for lobby before creating other channels for nicer ids
|
let channels: Chan[] = [];
|
||||||
const lobbyChannelId = client.idChan++;
|
|
||||||
|
|
||||||
if (Array.isArray(args.channels)) {
|
if (Array.isArray(args.channels)) {
|
||||||
let badName = false;
|
let badName = false;
|
||||||
|
@ -295,7 +301,7 @@ class Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO; better typing for args
|
// TODO; better typing for args
|
||||||
const network = new Network({
|
return new Network({
|
||||||
uuid: args.uuid,
|
uuid: args.uuid,
|
||||||
name: String(
|
name: String(
|
||||||
args.name || (Config.values.lockNetwork ? Config.values.defaults.name : "") || ""
|
args.name || (Config.values.lockNetwork ? Config.values.defaults.name : "") || ""
|
||||||
|
@ -323,6 +329,15 @@ class Client {
|
||||||
proxyUsername: String(args.proxyUsername || ""),
|
proxyUsername: String(args.proxyUsername || ""),
|
||||||
proxyPassword: String(args.proxyPassword || ""),
|
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
|
// Set network lobby channel id
|
||||||
network.getLobby().id = lobbyChannelId;
|
network.getLobby().id = lobbyChannelId;
|
||||||
|
@ -363,7 +378,7 @@ class Client {
|
||||||
|
|
||||||
if (!isStartup) {
|
if (!isStartup) {
|
||||||
client.save();
|
client.save();
|
||||||
channels.forEach((channel) => channel.loadMessages(client, network));
|
network.channels.forEach((channel) => channel.loadMessages(client, network));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -145,6 +145,7 @@ class ClientManager {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
client = new Client(this, name, userConfig);
|
client = new Client(this, name, userConfig);
|
||||||
|
client.connect();
|
||||||
this.clients.push(client);
|
this.clients.push(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ const input: PluginInputHandler = function (network, chan, cmd, args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const host = args[0];
|
const host = args[0];
|
||||||
this.connect({host, port, tls});
|
this.connectToNetwork({host, port, tls});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
|
@ -485,7 +485,7 @@ function initializeClient(
|
||||||
data.commands = null;
|
data.commands = null;
|
||||||
data.ignoreList = null;
|
data.ignoreList = null;
|
||||||
|
|
||||||
client.connect(data);
|
client.connectToNetwork(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -948,6 +948,7 @@ function performAuthentication(this: Socket, data) {
|
||||||
|
|
||||||
if (Config.values.public) {
|
if (Config.values.public) {
|
||||||
client = new Client(manager!);
|
client = new Client(manager!);
|
||||||
|
client.connect();
|
||||||
manager!.clients.push(client);
|
manager!.clients.push(client);
|
||||||
|
|
||||||
socket.on("disconnect", function () {
|
socket.on("disconnect", function () {
|
||||||
|
|
|
@ -27,6 +27,7 @@ describe("Custom highlights", function () {
|
||||||
},
|
},
|
||||||
} as any
|
} as any
|
||||||
);
|
);
|
||||||
|
client.connect();
|
||||||
logInfoStub.restore();
|
logInfoStub.restore();
|
||||||
expect(userLoadedLog).to.equal("User test loaded\n");
|
expect(userLoadedLog).to.equal("User test loaded\n");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue