Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:11:05+00:00 2026-06-12T13:11:05+00:00

I am developing an API in which I want to be able to know

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:11:08+00:00Added an answer on June 12, 2026 at 1:11 pm

    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.

    /**
     * @fileoverview Adds the ability to get events when an event listener is added or removed.
     * @author Joshua Dwire
     * @requires goog.events
     */
    
    
    goog.provide('sometechie.eventhack');
    goog.provide('sometechie.eventhack.GotListenerEvent');
    goog.provide('sometechie.eventhack.LostListenerEvent');
    goog.require('goog.events');
    
    /**
     * @class Provides a way to get events when an event listener is added or removed
     */
    sometechie.eventhack={}
    /**
     * Generates the type of event that will be fired when a listener of the given type is added.
     *
     * @example goog.events.listen([src], sometechie.eventhack.generateGotListenerEventType([type]), [listener], [opt_capt], [opt_handler]) 
     *
     * @param {string} type Event Type.
     * @returns {string} The type of event that will be fired when a listener of the given type is added.
     */
    sometechie.eventhack.generateGotListenerEventType=function(type){
        return '$sometechie.eventhack.gotlistener@'+type;
    }
    
    /**
     * Generates the type of event that will be fired when the first listener of the given type is added.
     *
     * @example goog.events.listen([src], sometechie.eventhack.generateGotFirstListenerEventType([type]), [listener], [opt_capt], [opt_handler]) 
     *
     * @param {string} type Event Type.
     * @returns {string} The type of event that will be fired when the first listener of the given type is added.
     */
    sometechie.eventhack.generateGotFirstListenerEventType=function(type){
        return '$sometechie.eventhack.gotfirstlistener@'+type;
    }
    
    /**
     * Generates the type of event that will be fired when a listener of the given type is removed.
     *
     * @example goog.events.listen([src], sometechie.eventhack.generateLostListenerEventType([type]), [listener], [opt_capt], [opt_handler]) 
     *
     * @param {string} type Event Type.
     * @returns {string} The type of event that will be fired when a listener of the given type is removed.
     */
    sometechie.eventhack.generateLostListenerEventType=function(type){
        return '$sometechie.eventhack.lostlistener@'+type;
    }
    
    /**
     * Generates the type of event that will be fired when the last listener of the given type is removed.
     *
     * @example goog.events.listen([src], sometechie.eventhack.generateLostLastListenerEventType([type]), [listener], [opt_capt], [opt_handler]) 
     *
     * @param {string} type Event Type.
     * @returns {string} The type of event that will be fired when the last listener of the given type is removed.
     */
    sometechie.eventhack.generateLostLastListenerEventType=function(type){
        return '$sometechie.eventhack.lostlastlistener@'+type;
    }
    
    /**
     * Object representing a eventhack GotListener event.
     *
     * @param {string} type Event type.
     * @param {Object} target
     * @param {string} eventType listener type added.
     * @param {boolean} gotFirst If this was the first listener added (there were no listeners before).
     * @extends {goog.events.Event}
     * @constructor
     */
    sometechie.eventhack.GotListenerEvent = function(type, target, eventType, gotFirst) {
      goog.base(this, type, target);
      this.eventType = eventType;
      this.gotFirst = gotFirst;
    };
    goog.inherits(sometechie.eventhack.GotListenerEvent, goog.events.Event);
    /**
     * The type of event that was added.
     * @type {string}
     */
    sometechie.eventhack.GotListenerEvent.prototype.eventType='';
    /**
     * If this was the first listener added (there were no listeners before).
     * @type {boolean}
     */
    sometechie.eventhack.GotListenerEvent.prototype.gotFirst=false;
    
    
    /**
     * Object representing a eventhack LostListener event.
     *
     * @param {string} type Event type.
     * @param {Object} target
     * @param {string} eventType listener type lost.
     * @param {boolean} lostLast If the last listener was removed (there are no listeners anymore).
     * @extends {goog.events.Event}
     * @constructor
     */
    sometechie.eventhack.LostListenerEvent = function(type, target, eventType,lostLast) {
      goog.base(this, type, target);
      this.eventType = eventType;
      this.lostLast = lostLast;
    };
    goog.inherits(sometechie.eventhack.LostListenerEvent, goog.events.Event);
    /**
     * The type of event that was removed.
     * @type {string}
     */
    sometechie.eventhack.LostListenerEvent.prototype.eventType='';
    /**
     * If the last listener was removed (there are no listeners anymore).
     * @type {boolean}
     */
    sometechie.eventhack.LostListenerEvent.prototype.lostLast=false;
    
    //Capture adding of event listeners
    goog.events.$st_listen_overridden=goog.events.listen;
    goog.events.listen=function(src, type, listener, opt_capt, opt_handler){
        var eventSrc = /** @type {goog.events.EventTarget} */ src;
        var hadListener=false;
        try{
            if(!goog.isArray(type) && !goog.isNull(type))hadListener=goog.events.hasListener(src,type);
        }catch(e){}
        var ret = goog.events.$st_listen_overridden.apply(this,arguments);
        try{
            if(!goog.isArray(type) && !goog.isNull(type)){
                var hasListener=goog.events.hasListener(src,type);
                var gotFirst=!hadListener&&hasListener;
    
                if(gotFirst){
                    goog.events.dispatchEvent(eventSrc,
                        new sometechie.eventhack.GotListenerEvent(
                            sometechie.eventhack.generateGotFirstListenerEventType(type),
                            src,type,true));
                }
    
                goog.events.dispatchEvent(eventSrc,
                    new sometechie.eventhack.GotListenerEvent(
                        sometechie.eventhack.generateGotListenerEventType(type),
                        src,type,gotFirst));
            }
        }catch(e){}
        return ret;
    }
    
    //Capture removing of event listeners
    goog.events.$st_unlistenByKey_overridden=goog.events.unlistenByKey;
    goog.events.unlistenByKey=function(key){
        var src=null,type=null;
        try{
            if (goog.events.listeners_[key]){
                var listener = goog.events.listeners_[key];
                if (!listener.removed) {
                    src = /** @type {goog.events.EventTarget} */ listener.src;
                    type = listener.type;
                }
            }
        }catch(e){}
        var ret = goog.events.$st_unlistenByKey_overridden.apply(this,arguments);
        try{
            if(src!=null&&type!=null){
                var lostLast=!goog.events.hasListener(src,type);
    
                if(lostLast){
                    goog.events.dispatchEvent(src,
                        new sometechie.eventhack.LostListenerEvent(
                            sometechie.eventhack.generateLostLastListenerEventType(type),
                            src,type,true));
                }
    
                goog.events.dispatchEvent(src,
                    new sometechie.eventhack.LostListenerEvent(
                        sometechie.eventhack.generateLostListenerEventType(type),
                        src,type,lostLast));
            }
        }catch(e){}
        return ret;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing some REST API's which are returning data in json format. ASP.net
I am developing a JSON/REST web API, for which I specifically want third party
We are developing an application which accesses an API exposed over some embedded system.
So Im developing a Rails app- primarily serves API which I want to lock
I am developing API using django-tasty-pie and wanted to know which library to implement
I'm developing an API which will also have an authentication/authorization component. Anybody, regardless of
I'm developing an API system which requires this type of authentication. The user will
I'm developing a REST api which needs to support optional filtering of properties based
I'm currently developing the presentation layer of an android app. The api which I'm
I was developing an application which uses the places API through the Javascript Library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.