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

useQuery is not stable #50

Open
DanAmador opened this issue Feb 17, 2025 · 4 comments
Open

useQuery is not stable #50

DanAmador opened this issue Feb 17, 2025 · 4 comments

Comments

@DanAmador
Copy link

Good day.

I recently found out that a useQuery used in a hook I was trying to debug kept being triggered even though the objects weren't changing which caused an infinite loop used in a useEffect

export const Object3DTrait = trait({ ref: new Object3D() });
export const SelectableTrait = trait();
export const SelectedTrait = trait();
  const selectedEntities = useQuery(Object3DTrait, SelectableTrait, SelectedTrait);
  const selectedEntityModels = useMemo((): Object3D[] => {
    const selection = selectedEntities
      .map((entity: Entity) => entity.get(Object3DTrait)?.ref)
      .filter(Boolean) as Object3D[];
    return selection;
  }, [selectedEntities]);

I currently found a work around by creating a stable immutable query hook

export function useStableQuery<T extends QueryParameter[]>(...parameters: T): Entity[] {
  const world = useWorld();
  const [entities, setEntities] = useState<Entity[]>(() => [...world.query(...parameters)]);

  useEffect(() => {

    const update = () => {
      const newResults = [...world.query(...parameters)];
      setEntities(newResults);
    };


    update();


    const unsubAdd = world.onAdd(parameters, () => {
      update();
    });

    const unsubRemove = world.onRemove(parameters, () => {
      update();
    });

    return () => {
      unsubAdd();
      unsubRemove();
    };

  }, [world, ...parameters]);

  return entities;
}
@krispya
Copy link
Member

krispya commented Feb 17, 2025

Thanks for reporting! I am going to try to reproduce but if you are able to help by creating a sandbox it would speed things up.

@krispya
Copy link
Member

krispya commented Feb 17, 2025

To be honest, I can't recall why I wrote useQuery to guard against generating new arrays so much when that is likely not what is causing GC issues. Could you PR this and I'll review from there?

@DanAmador
Copy link
Author

Hello there again.

By PR this do you mean replace the useQuery with my current implementation or should I add the useStableQuery ?

I'll probably get some time tomorrow to try and replicate a minimum sandbox and send it to you.

@krispya
Copy link
Member

krispya commented Feb 20, 2025

Replace useQuery with your version. And then I can try to get a test working once you can show me how to replicate.

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