Closed lantua closed 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.
index
on
First suggestion would be to remove removeListener altogether. Second would be to replace listener‘s inner Array with Dictionary.
removeListener
listener
This is resolved in https://github.com/Azoy/Sword/commit/b4d60143fafae9e1066d6f20f8f1356cce2640ca
Here’s the example
Source:
Output:
This is because
index
returned byon
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 replacelistener
‘s inner Array with Dictionary.