bsorrentino / cordova-broadcaster

Cordova Plugin to allow message exchange between javascript and native (and viceversa)
MIT License
113 stars 53 forks source link

removeEventListener does not remove event key #15

Closed mebibou closed 7 years ago

mebibou commented 7 years ago

When I call successively addEventListener, removeEventListener and addEventListener again, it does not subscribe using the new function passed in. I think it would be more proper to remove the key from the list of events so that it does register a new function.

It would basically mean adding delete me._channels[eventname] here

bsorrentino commented 7 years ago

Hi thanks for feedback

I didn't do because I figured out that channel.subscribe() adds a new subscription, because i wanted to manage multiple subscription at same event.

If I call delete me.channels[eventname] I will not allow such behaviour anymore.

I'll check the `cordova/channel' features

mebibou commented 7 years ago

oh yes correct that's not the best idea. I am trying to do:

var func = function(event) {
  console.log(event);
  window.broadcaster.removeEventListener('test', func);
};
window.broadcaster.addEventListener('test', func);

to do a "one-off" kind of event listener. Maybe you have a better way to do this

bsorrentino commented 7 years ago

i've released version 2.2.0 with fix.

Please, if possible, test it and let me know

qingmengwuhen commented 10 months ago

Maybe it is not a good idea
window.broadcaster._channels = {};

bsorrentino commented 10 months ago

Maybe it is not a good idea window.broadcaster._channels = {};

It is bit drastic solution, but works. I implemented channelDelete for selective removal but window.broadcaster._channels = {}; is like disable plugin