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

Infinite loop with CollaborationPlugin when changing state #7095

Open
agscripter opened this issue Jan 25, 2025 · 1 comment
Open

Infinite loop with CollaborationPlugin when changing state #7095

agscripter opened this issue Jan 25, 2025 · 1 comment
Labels
collab Related to Lexical collaboration features

Comments

@agscripter
Copy link

agscripter commented Jan 25, 2025

Hello everyone,

I am trying to show a message to users when the connection status with the WebSocket (using yjs) changes.

This is snippet of my component code:

const [status, setStatus] = useState<ConnectionStatus>(ConnectionStatus.CONNECTING);

const providerFactory = useCallback((id: string, yjsDocMap: Map<string, Doc>): Provider => {
    if (providerRef.current) {
        return providerRef.current as unknown as Provider;
    }
    
    let doc = yjsDocMap.get(id);
    if (!doc) {
        doc = new Doc();
        yjsDocMap.set(id, doc);
    } else {
        doc.load();
    }
    
    const wsProvider = new WebsocketProvider(getWebSocketHost(), id, doc, {
        connect: false,
        params: { accessToken: collaborative?.accessToken ?? '' },
        disableBc: true,
        resyncInterval: 1000
    });
    
    wsProvider.on('status', (event) => {
        setStatus(ConnectionStatus[(event.status as string).toUpperCase()]);
    });
    
    wsProvider.on('connection-error', () => {
        setStatus(ConnectionStatus.ERRORED);
    });
}, []);

And the CollaborationPlugin is also being mount in the same component that I have the useState and the providerFactory.
And the status state is being used below to show a message and to enable/disable the Editor.

However, when the setStatus is invoked (state changes), it seems to trigger the reconnection for the WebsocketProvider, causing an infinite loop, the status goes from Connecting -> Connected -> Disconnected -> Connecting -> Connected, etc.

Anyone knows how can I get it fixed? I basically want to change state accordingly to the connection status.

Thank you !

@etrepum etrepum added the collab Related to Lexical collaboration features label Jan 31, 2025
@etrepum
Copy link
Collaborator

etrepum commented Jan 31, 2025

Without a full repro that is easy to inspect, I don't think anyone will help you solve this. It's likely a violation or misunderstanding of the rules of react (either in your code or @lexical/yjs) but there's just not enough context here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collab Related to Lexical collaboration features
Projects
None yet
Development

No branches or pull requests

2 participants