Azoy / Sword

Discord library for Swift
https://azoy.github.io/Sword
MIT License
179 stars 52 forks source link

Incorrect removeListener behaviour #41

Closed lantua closed 6 years ago

lantua commented 6 years ago

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 6 years ago

This is resolved in https://github.com/Azoy/Sword/commit/b4d60143fafae9e1066d6f20f8f1356cce2640ca