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

Altcha firing "verified" state multiple times #4

Closed
roivasxxx opened this issue Sep 26, 2024 · 5 comments
Closed

Altcha firing "verified" state multiple times #4

roivasxxx opened this issue Sep 26, 2024 · 5 comments

Comments

@roivasxxx
Copy link

Hello, I forked the repo and I was playing around a little. I've noticed this thing, where if we pass a memoized function to the widget, which is used in the dependency array of the component, and we update some state (count in my example), this function depends on, and then we try to verify via the Altcha widget, it fires the "verified" event state twice (+calls the function twice, first with the initial state value, then the newest state value). I guess the reason for this behaviour is that the initial listener is not removed (?) and then the listener fires for both the initial listener and the newest listener.
Is the reason for this just me not understanding how to properly remove the event listener?

How to replicate:

  1. clone the forked repo
  2. update the state via the increment button (might take a couple of clicks)
  3. click to validate altcha
  4. observe the console - it should log the "verified" event twice -> calling the memoized function twice

I added a regular document click listener to see if it would actually get removed properly - seems that it does - since it actually only logs once (??).

Could anyone help me get this to work? This is just a simplified example of what my app should be doing (call a memoized function once it gets verified)

@ovx
Copy link
Contributor

ovx commented Sep 26, 2024

Hi, I think this is because of react's strict mode (https://react.dev/reference/react/StrictMode), if you try disable the strict mode, it should work fine. I'm not sure why the listener isn't removed properly, any help here is welcome.

@roivasxxx
Copy link
Author

Hello, yeah I forgot to mention that removing strict mode or well.. just doing a prod build (so no strict mode) actually does help. I'm just really curious as to why the listener would not get removed with strict mode.
Thought that it might just be React screwing up web components (made a simple checkbox wc that emits an event once checked, tried a wc with shadow dom/without - listener was properly added/removed with the same setup).. So I am really lost as to what is actually going on..
Also checking the event listeners of the element in the browser console also clearly shows that there is one event listener...

Forgot to mention that this happens on 0.9.0 (same as in the example) and 1.0.0

@ovx
Copy link
Contributor

ovx commented Oct 2, 2024

If it works with a custom web component it might be something related to how Svelte's internal events or build itself, but removing event listeners works fine using vanilla JS. Will look into it.

@ovx
Copy link
Contributor

ovx commented Oct 10, 2024

So it turns out this is but in Svelte, which does not remove event listeners properly. I created a PR for svelte and will release a fixed build soon.

sveltejs/svelte#13556 (comment)

@ovx
Copy link
Contributor

ovx commented Oct 11, 2024

Issues fixed in the version 1.0.5.

@ovx ovx closed this as completed Oct 11, 2024
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