Skip to content

Conversation

@Loque18
Copy link

@Loque18 Loque18 commented Jun 15, 2021

hey there, I was using Events module, but I wasn't enabled to remove a callback passing it as an argument, the only way to remove it, was to remove all the events calling Events.off( object, eventName ), so after reviewing the Events module I found the issue, so base in this the correct way to check if two functions are equal is first to parse them as a string, and then compare if the results are equal. So I just modified one line of the module converting the callbacks to string in the if statement (line 65)

here is part of my code if it is needed to replicate the issue

const update = function(e){
        console.log('working');
    }
    
    const {currentSelection} = props.editorData;
    useEffect(
        () => {



            console.log(runner);
            const $inspector = inspectorElem.current;
            if(currentSelection){
                setFormDisabled(false);
                set_initial_inspector($inspector, currentSelection);
                Matter.Events.on(runner, 'afterUpdate', update);

            }
            else{
                setFormDisabled(true);
                clearInspector($inspector);

                if(runner.events != null){
                     Matter.Events.off(runner, 'afterUpdate', update)
                }

            }
        },
        [currentSelection]
    );

@angstyloop
Copy link

An alternative solution to listener management in JS is to extend the EventTarget class from the JS Web API, overriding the addEventListener and removeEventListener methods to maintain a data structure containing the listener function references.

I follow this approach in the Gist below, where I also have a few unit tests. You can run the tests in the browser JS console by copying the code from the Gist to your clipboard, pasting it into the browser JS console, and pressing enter. Please read strange JS from the internet before running it in your JS browser console.

https://gist.github.com/angstyloop/504414aba95b61b98be0db580cb2a3b0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants