I am developing an API in which I want to be able to know when an event listener on an object is added or removed. The reason is that some of the events I am firing will require me to continually poll an object for updates, and I don’t want to have to poll the object if nothing is listening for the event. I am polling the html5 media player and other players for buffering updates, so eliminating the need to poll is not an option. I also don’t want the users to have to call a function to initiate the polling when adding or removing the event listener.
I have developed a solution where I intercept the adding of event listeners before passing the call on to the Closure Library, but it is a very hacky way of doing things, so I’d like to find a better way. I’ll post my way below, but I’d love to find a better way to do this.
As I said above, I know this is a very hacky way of doing things, but it is the only way I have come up with. Feel free to remove the
sometechie.namespace references and/or use any or all of this code in any project you are working on.