Skip to content

Commit

Permalink
🐛 Fix JOIN and PART events being emitted before they're fully processed
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePooN committed Oct 19, 2024
1 parent 5547ddc commit 0d8642d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
21 changes: 11 additions & 10 deletions lib/IrcCommands/JoinCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -43,4 +44,4 @@ class JoinCommand extends IrcCommand {
}
}

module.exports = JoinCommand;
module.exports = JoinCommand;
28 changes: 16 additions & 12 deletions lib/IrcCommands/PartCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
14 changes: 5 additions & 9 deletions tests/units/JoinEventChannelUnit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
module.exports = new JoinEventChannelUnit();

0 comments on commit 0d8642d

Please sign in to comment.