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

[Bug]: onAdd and onRemove is getting called multiple times after hot-reload #169

Open
anguer opened this issue Apr 20, 2024 · 1 comment

Comments

@anguer
Copy link

anguer commented Apr 20, 2024

Bug description

When invoking removeAllListeners to clean up event listeners, the event listeners for onAdd and onRemove are not cleared, resulting in multiple invocations.

Optional: Minimal reproduction

export const Provider = ({ children }: { children: ReactNode }) => {
  const [currentRoom, setCurrentRoom] = useState<Room<State> | null>(null);

  useEffect(() => {
    if (!currentRoom) {
      return;
    }

    log.info('registerRoomListeners', currentRoom.roomId);
    currentRoom.onLeave(onRoomLeave);
    currentRoom.onError(onRoomError);
    // currentRoom.onStateChange(onStateChange);
    currentRoom.onMessage('__playground_message_types', onPlaygroundMessage);
    // FIXME: onAdd and onRemove is getting called multiple times after hot-reload
    currentRoom.state.players.onAdd(onPlayerEnter);
    currentRoom.state.players.onRemove(onPlayerLeave);
    currentRoom.state.viewers.onAdd(onViewerEnter);
    currentRoom.state.viewers.onRemove(onViewerLeave);

    return () => {
      if (currentRoom) {
        log.info('unregisterRoomListeners', currentRoom.roomId);
        currentRoom.removeAllListeners();
      }
    };
  }, [
    currentRoom,
    onRoomLeave,
    onRoomError,
    onPlaygroundMessage,
    onPlayerEnter,
    onPlayerLeave,
    onViewerEnter,
    onViewerLeave,
  ]);

  return <>{children}</>;
};
@endel
Copy link
Member

endel commented Apr 20, 2024

This will be fixed on colyseus/colyseus#709

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