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

Receiving message in onMessage after calling dispose #103

Closed
SyedSaifAli opened this issue Apr 6, 2021 · 10 comments
Closed

Receiving message in onMessage after calling dispose #103

SyedSaifAli opened this issue Apr 6, 2021 · 10 comments
Labels
bug Something isn't working

Comments

@SyedSaifAli
Copy link

SyedSaifAli commented Apr 6, 2021

const wppconnect = require('../../dist');

wppconnect
  .create()
  .then((client) => start(client))
  .catch((erro) => {
    console.log(erro);
  });

function start(client) {
  a = client.onMessage((message) => {
    if (message.body === 'Hi' && message.isGroupMsg === false) {
      client
        .sendText(message.from, 'Welcome Wppconnect 🕷')
        .then((result) => {
          console.log('Result: ', result); //return object success
        })
        .catch((erro) => {
          console.error('Error when sending: ', erro); //return object error
        });
    }
  });
  setTimeout(()=>{
    a.then(b=> b.dispose());
  },10000);
}

I used the example code given in the repo and added setTimeout to dispose.

According to the above code.
after 10 seconds, the listener should stop listening to new messages.
But I was still getting the new messages.

@SyedSaifAli SyedSaifAli added the bug Something isn't working label Apr 6, 2021
@edgardmessias
Copy link
Contributor

Thanks @SyedSaifAli , I could identify what was the problem, so I will fix it

@SyedSaifAli
Copy link
Author

Thanks @SyedSaifAli , I could identify what was the problem, so I will fix it

Thank you :) Please let me know once you fix it :)

@edgardmessias
Copy link
Contributor

@SyedSaifAli , could you test the nightly version?

How to install: https://wppconnect-team.github.io/wppconnect/pages/Getting%20Started/installation.html

@edgardmessias
Copy link
Contributor

@SyedSaifAli , I make a little change about the return of onMessage to return the direct object instead of a promise

@SyedSaifAli
Copy link
Author

@SyedSaifAli , I make a little change about the return of onMessage to return the direct object instead of a promise

Is this on the nightly version?

@edgardmessias
Copy link
Contributor

@SyedSaifAli , I make a little change about the return of onMessage to return the direct object instead of a promise

Is this on the nightly version?

Yes

@SyedSaifAli
Copy link
Author

@SyedSaifAli , I make a little change about the return of onMessage to return the direct object instead of a promise

Is this on the nightly version?

Yes

Okay.

@SyedSaifAli
Copy link
Author

@edgardmessias It's working now :D
In this link
I can see that you have moved the listener on and listener off to another common function.
But how does moving to a common function solved this problem? 😅

@edgardmessias
Copy link
Contributor

To dispose an event on Event emitter, both argument must in off method must be the same of "on".
The maid problem was the on function wrapped the call in an arrow function, as a consequence, it is not the same function in "off"

@SyedSaifAli
Copy link
Author

Great 💐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants