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

Unable to remove listener created by .map() function #1314

Open
jasoncht1023 opened this issue Mar 25, 2023 · 2 comments
Open

Unable to remove listener created by .map() function #1314

jasoncht1023 opened this issue Mar 25, 2023 · 2 comments

Comments

@jasoncht1023
Copy link

I have been trying to develop a message function between users with GUN. The messages are stored in gunDB and displayed on the page when a peer account is selected. Here is what I'm trying to do:

<html>
<body>
    <select name="recipients" id="recipients" onchange="displayMessage()">
         <option value="alice">Alice</option>
         <option value="bob">Bob</option>
    </select>
    
    <p id="chatbox"></p>
<script>
    async function displayMessage() {
        let peer = document.getElementById("recipients").value;
        let messageList = "";
        await gun.get('chat').get(peer).map().once(async (data, key) => {            
            messageList += data + "<br>"
        });   
        document.getElementById("chatbox").innerHTML = messageList;
    }
</script>
</body>
</html>

For example, when the user selects Alice in the select box, the messages between the user and Alice will be displayed. Any subsequent message updates will also be shown as the data in the node is subscribed by the gun.get('chat').get(peer).map().once(cb) function. However, when the user switches to Bob and then back to Alice, gun.get('chat').get(peer).map().once(cb) (where peer is Alice) is called twice in total and the listener for message updates is created twice. Therefore, when there are new messages sent, the messages will be shown twice instead of only once.

Is there a feature to stop .map() function to subscribe to data updates in a node? I've tried to create a variable prevPeer to store the previous peer and call gun.get('chat').get(prevPeer).off() at the beginning of displayMessage() but nothing has changed.

@amark
Copy link
Owner

amark commented May 11, 2023

chain.off() should remove all listeners at that chain & underneath, rather aggressively. .on(data, key, msg, event){ event.off() should remove that one individual listener. It seems like neither of these are working? I wonder if it is because .map() creates a type of special sub-chain that is not accessible to chain.off() tho it should be... someone willing to debug against https://github.com/amark/gun/blob/master/gun.js#L1290-L1295 ?

@pwFoo
Copy link

pwFoo commented May 30, 2023

Is it related to the map callback bug #1315?
Maybe fix it / rewrite map() would be the solution?

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

3 participants