Azoy/Sword

Incorrect removeListener behaviour

Closed this issue · 1 comments

Here’s the example

Source:

let bot = Sword(token: token)

let indexA = bot.on(.ready) { _ in print("Resulted: handle A") }
let indexB = bot.on(.ready) { _ in print("Resulted: handle B") }
let indexC = bot.on(.ready) { _ in print("Resulted: handle C") }

bot.removeListener(from: .ready, at: indexA)
bot.removeListener(from: .ready, at: indexB)
print("Expected: handle C")

bot.connect()

Output:

Expected: handle C
Resulted: handle B

This is because index returned by on is for the array-based object.
So when you remove the an item, every indices shift.

First suggestion would be to remove removeListener altogether.
Second would be to replace listener‘s inner Array with Dictionary.

Azoy commented

This is resolved in b4d6014