Nested PointerDown listener bug


Nesting listeners seems to work weirdly: if I want to listen to a scene’s pointerDown, and after that, listening again on this event, both are triggered

I fixed it with a timeout = 0 but don’t seems to be the wanted behavior…

Here’s a PG to reproduce it:

That’s an interesting issue. It’s by design. The observable is iterating over an array of observers and triggers them one after the other. if you add one element to it, it will be triggered because it is added to the end of the array.

One solution to it is to use insertFirst - which will add the observer to the beginning of the array, but that depends on your use case, as it might not be what you expect it to do.
Another solution is, as you pointed out, using setTimeout.

A discussion can be made as to the pattern and whether or not it makes sense. It can be solved at the API level (for example - addOnNextFrame or something similar), but this is the first time we enounter that so I am not sure it is needed.
Can I ask what is your use case here?

1 Like

Thank you for the explanation, the solution of insertFirst can be a good solution for me, but the option is not available in addOnce(…) function, only in add(…) and i’m using the first one
setTimeout is also ok for me
I dont think it’s necessary to modify it at API level, I’ve just reported this weird comportment is case it was a bug ^^

My use case (but not entering into details because it coult be long to explain):

  • onPointerDown i’m creating 2 sphères
  • the second sphere is dragging with cursor on move and is definetively placed onPointerUp

But the same process can be done with 1 click = 1st sphere, 2d click = 2d sphere
So on pointer up I check if the distance between the two spheres < 1 (2 clicks scenario) and listen again pointerDown to place the 2d sphere
Here the bug append:

  • Someone make a dbl click (because boomers or very effective testers :joy:)
  • onPointerDown > create a 2d sphere
  • Distance = 0, listen to PointerDown to place a new sphere
  • pointerDown triggered immediatly > create a new sphere > distance = 0 …

And browser tab crash because of infinte loop ^^

I don’t know it my example is clear but no problem, I have many ways to solve it :wink:
As I said, I just reported in case it was a “real” bug ^^

Thank you for your help !

1 Like