From 0d8642d66321827415d7575d6b02fe2ef44c2214 Mon Sep 17 00:00:00 2001 From: ThePooN Date: Sat, 19 Oct 2024 02:23:20 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20JOIN=20and=20PART=20events?= =?UTF-8?q?=20being=20emitted=20before=20they're=20fully=20processed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/IrcCommands/JoinCommand.js | 21 +++++++++++---------- lib/IrcCommands/PartCommand.js | 28 ++++++++++++++++------------ tests/units/JoinEventChannelUnit.js | 14 +++++--------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/lib/IrcCommands/JoinCommand.js b/lib/IrcCommands/JoinCommand.js index 7319cb7..b0e3d99 100644 --- a/lib/IrcCommands/JoinCommand.js +++ b/lib/IrcCommands/JoinCommand.js @@ -14,26 +14,27 @@ class JoinCommand extends IrcCommand { /** * Emits the JOIN event - * + * * @fires BanchoClient#JOIN - * @param {BanchoClient} client + * @param {BanchoClient} client * @param {BanchoUser} user - * @param {BanchoChannel} channel + * @param {BanchoChannel} channel */ emit(client, user, channel) { - /** - * Fired when a user has joined a channel - * @event BanchoClient#JOIN - * @type {BanchoChannelMember} - */ const member = new BanchoChannelMember(client, channel, user.ircUsername); channel.channelMembers.set(user.ircUsername, member); - client.emit("JOIN", member); if(user.isClient()) { channel.joined = true; if(channel.joinCallback != null) channel.joinCallback(); } + + /** + * Fired when a user has joined a channel + * @event BanchoClient#JOIN + * @type {BanchoChannelMember} + */ + client.emit("JOIN", member); } handleCommand(client, command, splits) { @@ -43,4 +44,4 @@ class JoinCommand extends IrcCommand { } } -module.exports = JoinCommand; \ No newline at end of file +module.exports = JoinCommand; diff --git a/lib/IrcCommands/PartCommand.js b/lib/IrcCommands/PartCommand.js index 1b45c9c..4474a4b 100644 --- a/lib/IrcCommands/PartCommand.js +++ b/lib/IrcCommands/PartCommand.js @@ -13,27 +13,31 @@ class PartCommand extends JoinCommand { /** * Emits the PART event - * + * * @fires BanchoClient#PART - * @param {BanchoClient} client - * @param {BanchoUser} user - * @param {BanchoChannel} channel + * @param {BanchoClient} client + * @param {BanchoUser} user + * @param {BanchoChannel} channel */ emit(client, user, channel) { - /** - * Fired when a user has left a channel - * @event BanchoClient#PART - * @type {BanchoChannelMember} - */ - if(channel.channelMembers.has(user.ircUsername)) - client.emit("PART", channel.channelMembers.get(user.ircUsername)); + const member = channel.channelMembers.get(user.ircUsername); + if(user.isClient()) { channel.joined = false; channel.channelMembers.clear(); if(channel.partCallback != null) channel.partCallback(); + } else { + channel.channelMembers.delete(user.ircUsername); } - channel.channelMembers.delete(user.ircUsername); + + /** + * Fired when a user has left a channel + * @event BanchoClient#PART + * @type {BanchoChannelMember} + */ + if(member) + client.emit("PART", member); } } diff --git a/tests/units/JoinEventChannelUnit.js b/tests/units/JoinEventChannelUnit.js index 0d88f96..caeb646 100644 --- a/tests/units/JoinEventChannelUnit.js +++ b/tests/units/JoinEventChannelUnit.js @@ -10,38 +10,34 @@ class JoinEventChannelUnit extends TestUnit { run() { return new Promise((resolve, reject) => { const channel = this.client.getChannel("#french"); - let returned = false; this.client.on("JOIN", (obj) => { if(obj.user.isClient() && obj.channel == channel) { this.fulFillGoal(TestGoals.JoinEvent); - channel.leave(channel); this.client.on("PART", (obj) => { if(obj.user.isClient() && obj.channel == channel) { this.client.removeAllListeners("JOIN"); this.client.removeAllListeners("nochannel"); + this.client.removeAllListeners("PART"); this.fulFillGoal(TestGoals.PartEvent); - returned = true; resolve(); } }); + channel.leave(channel); } }); this.client.on("nochannel", (errorChannel) => { if(errorChannel == channel) { this.client.removeAllListeners("JOIN"); this.client.removeAllListeners("nochannel"); - returned = true; + this.client.removeAllListeners("PART"); reject(new Error("No such channel: "+errorChannel.name)); } }); channel.join(); - setTimeout(() => { - if(!returned) - reject(new Error("Didn't join after timeout!")); - }, 10000); + setTimeout(() => reject(new Error("Didn't join and leave after timeout!")), 10000); }); } } -module.exports = new JoinEventChannelUnit(); \ No newline at end of file +module.exports = new JoinEventChannelUnit();