audio5js icon indicating copy to clipboard operation
audio5js copied to clipboard

Cannot Unsubscribe from Event from within Handler

Open tf opened this issue 9 years ago • 10 comments

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.

tf avatar Apr 29 '15 16:04 tf

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?

zohararad avatar May 01 '15 21:05 zohararad

Well, in the simplest case the iteration could simply be rewritten to a for-loop, right?

tf avatar May 02 '15 16:05 tf

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 avatar May 02 '15 17:05 zohararad

@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?

belldandu avatar Nov 21 '15 07:11 belldandu

@kamijoutouma this is an edge-case to begin with. Agreed - we'll close of no response from owner in 24h

zohararad avatar Nov 21 '15 13:11 zohararad

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 avatar Nov 23 '15 10:11 tf

@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 avatar Nov 23 '15 14:11 zohararad

@zohararad Sure ill be happy to.

belldandu avatar Nov 23 '15 19:11 belldandu

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.

kosso avatar Aug 02 '16 16:08 kosso

Can you share a code sample of this? It might be a persistent bug in the code I need to address.

zohararad avatar Aug 03 '16 15:08 zohararad