Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bridge.removeListener not works #69

Open
longsummer opened this issue Jan 7, 2019 · 1 comment
Open

bridge.removeListener not works #69

longsummer opened this issue Jan 7, 2019 · 1 comment

Comments

@longsummer
Copy link

I found the function bridge.removeListener() doesn't work and guess things going wrong because of the function bridge.on(), here is your code:

 'bridge': {
            /**
             * Sets the callback to trigger whenever an event is emitted
             * @param event The name of the event
             * @param callback The callback to trigger, this callback will be given the data passed (if any), and
             * the name of the targeted window and finally the name of the window that triggered/emitted the event
             * @return the handler that add into the event listeners array
             * */
            'on': function(event, callback){
                let id =  windowManager.eventEmitter.listenerCount(event);

                windowManager.eventEmitter.addListener(event, function(event){
                    callback.call(null, event.data, event.target, event.emittedBy);
                });

                return windowManager.eventEmitter.listeners(event)[id];
            },

The function bridge.on() wants to return a handler registered on specific event so that users can remove this listener later using this handler, however, this handler is just a copy of the REAL HANDLES according to NodeJS document:

image

Obviously, using the handler returned by bridge.on() just can't work.

@eserrose
Copy link

My attempt at a workaround.
Add an object listeners that keeps track of the added handlers in bridge. When calling windowManager.eventEmitter.addListener, store the function inside listeners.

I tried returning the handler within listeners, but it gets turned into "remoteFunction". Hence, instead of returning the handler itself, I am returning the index of the handler inside listeners. Then instead of using splice or something to remove it, I set it to null. If you are constantly adding and removing handlers, the object may get too big, but it's a working solution.

'bridge': {
            /**
             * The event listeners
             * */
            listeners : {},

            /**
             * Sets the callback to trigger whenever an event is emitted
             * @param event The name of the event
             * @param callback The callback to trigger, this callback will be given the data passed (if any), and
             * the name of the targeted window and finally the name of the window that triggered/emitted the event
             * @return the handler that add into the event listeners array
             * */
            'on': function(event, callback){
                let id = 0;

                if(this.listeners[event]){
                    id = this.listeners[event].length;
                } else {
                    this.listeners[event] = [null]
                }

                windowManager.eventEmitter.addListener(event, this.listeners[event][id] = function(event){
                    callback.call(null, event.data, event.target, event.emittedBy);
                });

                return id;
            },

            /**
             * Remove a event listener returned by windowManger.bridge.on
             * or windowManager.bridge.addListener
             * @param event The name of the event
             * @param id the index of the listen handler returned by
             *        windowManager.bridge.on
             */
            'removeListener': function(event, id) {
                let handler;

                if(this.listeners[event] && this.listeners[event].length > id){
                    handler = this.listeners[event][id];
                }
                if(handler){
                    windowManager.eventEmitter.removeListener(event, handler);
                    this.listeners[event][id] = undefined;
                }
            },


            'removeListeners': function(event) {
                this.listeners[event] = undefined;
                windowManager.eventEmitter.removeAllListeners(event);
            },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants