audio5js
audio5js copied to clipboard
Cannot Unsubscribe from Event from within Handler
Calling Pubsub#off
is no-op while Pubsub#trigger
is running:
function handler() {
if (...) {
player.off('progress', handler)
}
})
player.on('progress', handler)
This happens since trigger
empties the channels
array while processing event handlers. So the handler does not appear to be registered while trigger is running.
In most cases you can probably just use one
instead. Still, it's a gotcha.
I agree that this is an edge-case that can be easily solved by using one
instead of on
. Do you have any suggestions for an elegant solution to this?
Well, in the simplest case the iteration could simply be rewritten to a for
-loop, right?
Correct, but then removing the events marked as once
becomes a bit of a hassle. I'm happy to accept a PR if you want to send one
@zohararad @tf from my experience with javascript i'm pretty sure that if you do one or once it will return the same value it returned the first time every time it see's the event 'progress'. If this is what you are aiming for then i'm all for it. But Otherwise if no response from the owner of this issue is made within 24-48 hours can this be closed?
@kamijoutouma this is an edge-case to begin with. Agreed - we'll close of no response from owner in 24h
I still think this is not the desired behavior. I don't have time to provide a fix myself right now though. So if you think this does not have to be addressed, feel free to close.
@tf I agree it's not the desired behaviour, but given that I too have little time to maintain the library, and we're talking about an edge case, I'm going to leave it open for now and treat it as low priority.
I'll try and allocate some time to investigate a solution. @kamijoutouma please feel free to pitch in if you have the time.
@zohararad Sure ill be happy to.
I had issues unsubscribing using off()
The only way I got around it was to flush the play channels arrays for each event.
eg: yourAudio5Player.channels.play = [];
seems to remove the event listener for play
.
Can you share a code sample of this? It might be a persistent bug in the code I need to address.